ui

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package ui manages the terminal and logs output for zedpm.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StringWidth

func StringWidth(str string) int

StringWidth will calculate the width of the string, taking control sequences into account as well as graphemes and their sizes.

func TruncateString

func TruncateString(
	line string,
	width int,
	ellipsis string,
) string

TruncateString will take a string and shorten it so that it's visible length matches the given value. It will also make the last visible part of the string the value given as the ellipsis (which may be empty).

Types

type Characters

type Characters struct {
	// contains filtered or unexported fields
}

Characters is a scanner that returns character groups for a terminal widgetLogLine one at a time. Each group represents either some visible element (a grapheme or space or 0-width component of a unicode string) or a complete terminal escape sequence. Only a subset of CSI escapes are supported (i.e., only including those that may be used by the code of zedpm or plugins).

func NewCharacters

func NewCharacters(str string) *Characters

NewCharacters creates a new Characters iterator.

func (*Characters) Bytes

func (c *Characters) Bytes() []byte

Bytes returns a byte slice which corresponds to the current grapheme cluster or escape sequence.

func (*Characters) IsEscape

func (c *Characters) IsEscape() bool

IsEscape returns true if the current cluster of bytes is an escape sequence.

func (*Characters) Next

func (c *Characters) Next() bool

Next moves to the next grapheme or terminal escape sequence. This returns false if no more graphemes or escape sequence remain.

func (*Characters) Positions

func (c *Characters) Positions() (int, int)

Positions returns the start and end locations of the current escape seqeuence or grapheme cluster in the string.

func (*Characters) Runes

func (c *Characters) Runes() []rune

Runes returns a rune slice which corresponds to the current grapheme cluster or escape sequence.

func (*Characters) String

func (c *Characters) String() string

String returns a string which corresponse to the current grapheme cluster or terminal escape sequence.

func (*Characters) Width

func (c *Characters) Width() int

Width returns the visible width of the current grapheme or terminal sequence (terminal sequences are always treated as zero-width).

type Progress

type Progress struct {
	// contains filtered or unexported fields
}

func NewProgress

func NewProgress(tty *os.File) *Progress

func (*Progress) Close

func (p *Progress) Close()

Close should always be called when finished with the progress widgetLogLine. When state is used, this will close all widgets and move the cursor to the final widgetLogLine.

func (*Progress) HasPhases

func (p *Progress) HasPhases() bool

func (*Progress) Log

func (p *Progress) Log(
	name,
	level,
	message string,
	args ...any,
)

Log will process a log widgetLogLine, typically by writing it to the screen. If widgets are present, it will add the widgetLogLine to the appropriate widget. It will also handle a number of special @<name> fields:

@task        - name of the task to log this with
@operation   - the operation the task is performing
@action      - an action key to identify some persistent state
@actionFlags - flags to modify how the log is displayed (e.g., "spin" and
               "autospin")
@outcome     - outcome is the final outcome of an action
@tick        - tick will cause a widgetLogLine with an associated "spin" flag to move

func (*Progress) RegisterTask

func (p *Progress) RegisterTask(name, title string)

func (*Progress) SetPhases

func (p *Progress) SetPhases(phases []string)

func (*Progress) StartPhase

func (p *Progress) StartPhase(phase int, taskCount int)

func (*Progress) TaskWidgetSize

func (p *Progress) TaskWidgetSize() int

func (*Progress) UpdateProgress

func (p *Progress) UpdateProgress()

type ProgressAdapter

type ProgressAdapter struct {
	// contains filtered or unexported fields
}

ProgressAdapter sends logs to the progress.

func NewSinkAdapter

func NewSinkAdapter(progress *Progress, minLevel hclog.Level) *ProgressAdapter

func (*ProgressAdapter) Accept

func (p *ProgressAdapter) Accept(
	name string,
	level hclog.Level,
	msg string,
	args ...any,
)

type ProgressWriter

type ProgressWriter struct {
	// contains filtered or unexported fields
}

func NewWriter

func NewWriter(name, level string, progress *Progress) *ProgressWriter

func (*ProgressWriter) Write

func (w *ProgressWriter) Write(p []byte) (int, error)

type State

type State struct {
	// contains filtered or unexported fields
}

State tracks the low-level state of output and writes that state to the terminal.

func NewState

func NewState(term *Terminal, capacity int) *State

NewState creates a new State object attached to the given terminal with room for capacity widgets, initially. Capacity will expand as needed.

State manages the terminal in two basic sections separated by a boundary widgetLogLine. Above the boundary widgetLogLine is a log. Below is zero or more widgets which are drawn and redrawn with every change.

func (*State) AddFlags

func (s *State) AddFlags(id WidgetID, action string, flags []string)

AddFlags adds the given flags on a line of a widget.

func (*State) AddWidget

func (s *State) AddWidget(n int) WidgetID

AddWidget creates a new widget with the given row height and then resizes and redraws the State on the terminal. The returned WidgetID should be used to make any changes to this widget going forward.

func (*State) Close

func (s *State) Close()

Close should always be called before program termination or when the State object is about to give up control of the terminal. This will erase all the widgets and move the cursor to where the boundary widgetLogLine was, just below the end of the log.

func (*State) DeleteWidget

func (s *State) DeleteWidget(id WidgetID)

DeleteWidget removes the widget with the given WidgetID. Then it resizes and redraws the State on the terminal.

func (*State) IncTick

func (s *State) IncTick(id WidgetID, action string)

IncTick increments the tick count on a line of a widget.

func (*State) Log

func (s *State) Log(line string)

Log will write a log to the top-log.

func (*State) LogWidget

func (s *State) LogWidget(id WidgetID, line string)

LogWidget will write a log to the widget identified by the given WidgetID. If no such widget exists or the NoWidget constant is passed, the log is recorded above the boundary without writing anything to the state below.

func (*State) MovementsToBoundary

func (s *State) MovementsToBoundary() int

MovementsToBoundary states how many cursor movements are required to move the cursor from the bottom of the on-screen State to the boundary widgetLogLine.

func (*State) Redraw

func (s *State) Redraw()

Redraw triggers a redraw. This should never be necessary to call directly.

func (*State) Set

func (s *State) Set(id WidgetID, n int, line string, flags ...string)

Set will set the value of a specific widget widgetLogLine to the given value. If the widget given by the WidgetID does not exist, this method does nothing. If flags are given, the flags are immediately added to the line.

func (*State) SetActionKey

func (s *State) SetActionKey(id WidgetID, n int, action string)

SetActionKey assigns an action key to a line in a widget.

func (*State) SetOutcome

func (s *State) SetOutcome(id WidgetID, action string, outcome log.Outcome)

SetOutcome assigns an outcome to a line in a widget.

func (*State) SetStatus

func (s *State) SetStatus(
	id WidgetID,
	n int,
	name string,
	icon statusIcon,
	op string,
)

SetStatus will set the status of a specific widget widgetLine. This does nothing if the widget with the given ID does not exist. This will then redraw the widget.

func (*State) SetTitle

func (s *State) SetTitle(id WidgetID, title string)

SetTitle sets the title widgetLogLine to use on a widget.

func (*State) Title

func (s *State) Title(id WidgetID) string

Title will return the title string for the widget, if one has been set by SetTitle.

type Terminal

type Terminal struct {
	// contains filtered or unexported fields
}

Terminal is a very simple tool for writing to and manipulating the terminal.

func NewTerminal

func NewTerminal(tty *os.File) *Terminal

NewTerminal creates a Terminal object for the given terminal and returns a pointer to that object.

func (*Terminal) AddLines

func (t *Terminal) AddLines(n int)

AddLines will move the cursor down n lines.

func (*Terminal) ClearLine

func (t *Terminal) ClearLine()

ClearLine will clear the row the cursor is currently on.

func (*Terminal) ClearLines

func (t *Terminal) ClearLines(n int)

ClearLines will clear n lines below the row the cursor is currently on.

func (*Terminal) Height

func (t *Terminal) Height() int

Height returns the number of rows on the current TTY. Returns 0 if this is not a TTY.

func (*Terminal) IsTTY

func (t *Terminal) IsTTY() bool

IsTTY returns true if the screen is a TTY. If IsTTY is false, the only feature that will do anything is WriteLine. All other methods become no-ops. In fact, even WriteLine does less: it will no longer clear lines.

func (*Terminal) MoveUp

func (t *Terminal) MoveUp(n int)

MoveUp will move the cursor up n rows on-screen.

func (*Terminal) Println

func (t *Terminal) Println(line string)

Println will write a single widgetLogLine to the screen. This will blank any existing data on the current widgetLogLine before writing and will move the cursor down one widgetLogLine afterward.

func (*Terminal) SetEllipsis

func (t *Terminal) SetEllipsis(ellipsis string)

SetEllipsis sets a string to add at the end of a truncated widgetLogLine. The default is to have no such string and just terminate the widgetLogLine at the end of the on-screen widgetLogLine.

func (*Terminal) Width

func (t *Terminal) Width() int

Width returns the number of coumns in the current TTY. Returns 0 if this is not a TTY.

func (*Terminal) WriteLine

func (t *Terminal) WriteLine(line string)

WriteLine will write a single widgetLogLine to the screen. If the given widgetLogLine contains a newline, it will be replaced by U+2424 (SYMBOL FOR NEWLINE) on screen. This will also truncate the widgetLogLine so it is not longer than the terminal width. This will blank any existing data on the current widgetLogLine before writing and will move the cursor down one widgetLogLine afterward.

type WidgetID

type WidgetID int

WidgetID allows the caller to address individual widgets.

const NoWidget WidgetID = -1

NoWidget is the WidgetID to use when an operation (see State.LogWidget) needs to be performed without referring to a widget.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL