ui

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2019 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AlignLeft = iota
	AlignCenter
	AlignRight
)

Variables

View Source
var (

	// TabSize is the number of spaces with which a tab character will be replaced.
	TabSize = 4
)

Functions

func ANSIWriter

func ANSIWriter(writer io.Writer) io.Writer

ANSIWriter returns an io.Writer which translates any ANSI escape codes written to it into tview color tags. Other escape codes don't have an effect and are simply removed. The translated text is written to the provided writer.

func TranslateANSI

func TranslateANSI(text string) string

TranslateANSI replaces ANSI escape sequences found in the provided string with tview's color tags and returns the resulting string.

Types

type TextView

type TextView struct {
	sync.Mutex
	*tview.Box
	// contains filtered or unexported fields
}

TextView is a box which displays text. It implements the io.Writer interface so you can stream text to it. This does not trigger a redraw automatically but if a handler is installed via SetChangedFunc(), you can cause it to be redrawn. (See SetChangedFunc() for more details.)

Navigation

If the text view is scrollable (the default), text is kept in a buffer which may be larger than the screen and can be navigated similarly to Vim:

  • h, left arrow: Move left.
  • l, right arrow: Move right.
  • j, down arrow: Move down.
  • k, up arrow: Move up.
  • g, home: Move to the top.
  • G, end: Move to the bottom.
  • Ctrl-F, page down: Move down by one page.
  • Ctrl-B, page up: Move up by one page.

If the text is not scrollable, any text above the top visible line is discarded.

Use SetInputCapture() to override or modify keyboard input.

Colors

If dynamic colors are enabled via SetDynamicColors(), text color can be changed dynamically by embedding color strings in square brackets. This works the same way as anywhere else. Please see the package documentation for more information.

Regions and Highlights

If regions are enabled via SetRegions(), you can define text regions within the text and assign region IDs to them. Text regions start with region tags. Region tags are square brackets that contain a region ID in double quotes, for example:

We define a ["rg"]region[""] here.

A text region ends with the next region tag. Tags with no region ID ([""]) don't start new regions. They can therefore be used to mark the end of a region. Region IDs must satisfy the following regular expression:

[a-zA-Z0-9_,;: \-\.]+

Regions can be highlighted by calling the Highlight() function with one or more region IDs. This can be used to display search results, for example.

The ScrollToHighlight() function can be used to jump to the currently highlighted region once when the text view is drawn the next time.

See https://github.com/rivo/tview/wiki/TextView for an example.

func NewTextView

func NewTextView() *TextView

NewTextView returns a new text view.

func (*TextView) Clear

func (t *TextView) Clear() *TextView

Clear removes all text from the buffer.

func (*TextView) Draw

func (t *TextView) Draw(screen tcell.Screen)

Draw draws this primitive onto the screen.

func (*TextView) Focus

func (t *TextView) Focus(delegate func(p tview.Primitive))

Focus is called when this primitive receives focus.

func (*TextView) GetHighlights

func (t *TextView) GetHighlights() (regionIDs []string)

GetHighlights returns the IDs of all currently highlighted regions.

func (*TextView) GetRegionText

func (t *TextView) GetRegionText(regionID string) string

GetRegionText returns the text of the region with the given ID. If dynamic colors are enabled, color tags are stripped from the text. Newlines are always returned as '\n' runes.

If the region does not exist or if regions are turned off, an empty string is returned.

func (*TextView) GetScrollOffset

func (t *TextView) GetScrollOffset() (row, column int)

GetScrollOffset returns the number of rows and columns that are skipped at the top left corner when the text view has been scrolled.

func (*TextView) GetText

func (t *TextView) GetText(stripTags bool) string

GetText returns the current text of this text view. If "stripTags" is set to true, any region/color tags are stripped from the text.

func (*TextView) HasFocus

func (t *TextView) HasFocus() bool

HasFocus returns whether or not this primitive has focus.

func (*TextView) Highlight

func (t *TextView) Highlight(regionIDs ...string) *TextView

Highlight specifies which regions should be highlighted. See class description for details on regions. Empty region strings are ignored.

Text in highlighted regions will be drawn inverted, i.e. with their background and foreground colors swapped.

Calling this function will remove any previous highlights. To remove all highlights, call this function without any arguments.

func (*TextView) InputHandler

func (t *TextView) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive))

InputHandler returns the handler for this primitive.

func (*TextView) ScrollTo

func (t *TextView) ScrollTo(row, column int) *TextView

ScrollTo scrolls to the specified row and column (both starting with 0).

func (*TextView) ScrollToBeginning

func (t *TextView) ScrollToBeginning() *TextView

ScrollToBeginning scrolls to the top left corner of the text if the text view is scrollable.

func (*TextView) ScrollToEnd

func (t *TextView) ScrollToEnd() *TextView

ScrollToEnd scrolls to the bottom left corner of the text if the text view is scrollable. Adding new rows to the end of the text view will cause it to scroll with the new data.

func (*TextView) ScrollToHighlight

func (t *TextView) ScrollToHighlight() *TextView

ScrollToHighlight will cause the visible area to be scrolled so that the highlighted regions appear in the visible area of the text view. This repositioning happens the next time the text view is drawn. It happens only once so you will need to call this function repeatedly to always keep highlighted regions in view.

Nothing happens if there are no highlighted regions or if the text view is not scrollable.

func (*TextView) SetChangedFunc

func (t *TextView) SetChangedFunc(handler func()) *TextView

SetChangedFunc sets a handler function which is called when the text of the text view has changed. This is useful when text is written to this io.Writer in a separate goroutine. This does not automatically cause the screen to be refreshed so you may want to use the "changed" handler to redraw the screen.

Note that to avoid race conditions or deadlocks, there are a few rules you should follow:

  • You can call Application.Draw() from this handler.
  • You can call TextView.HasFocus() from this handler.
  • During the execution of this handler, access to any other variables from this primitive or any other primitive should be queued using Application.QueueUpdate().

See package description for details on dealing with concurrency.

func (*TextView) SetDoneFunc

func (t *TextView) SetDoneFunc(handler func(key tcell.Key)) *TextView

SetDoneFunc sets a handler which is called when the user presses on the following keys: Escape, Enter, Tab, Backtab. The key is passed to the handler.

func (*TextView) SetDynamicColors

func (t *TextView) SetDynamicColors(dynamic bool) *TextView

SetDynamicColors sets the flag that allows the text color to be changed dynamically. See class description for details.

func (*TextView) SetRegions

func (t *TextView) SetRegions(regions bool) *TextView

SetRegions sets the flag that allows to define regions in the text. See class description for details.

func (*TextView) SetScrollable

func (t *TextView) SetScrollable(scrollable bool) *TextView

SetScrollable sets the flag that decides whether or not the text view is scrollable. If true, text is kept in a buffer and can be navigated.

func (*TextView) SetText

func (t *TextView) SetText(text string) *TextView

SetText sets the text of this text view to the provided string. Previously contained text will be removed.

func (*TextView) SetTextAlign

func (t *TextView) SetTextAlign(align int) *TextView

SetTextAlign sets the text alignment within the text view. This must be either AlignLeft, AlignCenter, or AlignRight.

func (*TextView) SetTextColor

func (t *TextView) SetTextColor(color tcell.Color) *TextView

SetTextColor sets the initial color of the text (which can be changed dynamically by sending color strings in square brackets to the text view if dynamic colors are enabled).

func (*TextView) SetWordWrap

func (t *TextView) SetWordWrap(wrapOnWords bool) *TextView

SetWordWrap sets the flag that, if true and if the "wrap" flag is also true (see SetWrap()), wraps the line at spaces or after punctuation marks. Note that trailing spaces will not be printed.

This flag is ignored if the "wrap" flag is false.

func (*TextView) SetWrap

func (t *TextView) SetWrap(wrap bool) *TextView

SetWrap sets the flag that, if true, leads to lines that are longer than the available width being wrapped onto the next line. If false, any characters beyond the available width are not displayed.

func (*TextView) Write

func (t *TextView) Write(p []byte) (n int, err error)

Write lets us implement the io.Writer interface. Tab characters will be replaced with TabSize space characters. A "\n" or "\r\n" will be interpreted as a new line.

type UI

type UI struct {
	printer.SimplePrinter
	// contains filtered or unexported fields
}

func NewUI

func NewUI(config UIConfig) *UI

func (*UI) Create

func (ui *UI) Create()

func (*UI) Input

func (ui *UI) Input() <-chan string

func (*UI) Run

func (ui *UI) Run()

func (*UI) Stop

func (ui *UI) Stop()

type UIConfig

type UIConfig struct {
	AmbiguousWidth string `flag:"|auto|二义性字符宽度"`
}

Jump to

Keyboard shortcuts

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