Documentation ¶
Overview ¶
Package tui is a library for building user interfaces for the terminal.
Widgets ¶
Widgets are the main building blocks of any user interface. They allow us to present information and interact with our application. It receives keyboard and mouse events from the terminal and draws a representation of itself.
lbl := tui.NewLabel("Hello, World!")
Layouts ¶
Widgets are structured using layouts. Layouts are powerful tools that let you position your widgets without having to specify their exact coordinates.
box := tui.NewVBox( tui.NewLabel("Press the button to continue ..."), tui.NewButton("Continue"), )
Here, the VBox will ensure that the Button will be placed underneath the Label. There are currently three layouts to choose from; VBox, HBox and Grid.
Size policies ¶
Sizing of widgets is controlled by its SizePolicy. For now, you can read more about how size policies work in the Qt docs:
Index ¶
- Constants
- Variables
- type Alignment
- type Box
- func (b *Box) Alignment() Alignment
- func (b *Box) Append(w Widget)
- func (b *Box) Draw(p *Painter)
- func (b *Box) Insert(i int, w Widget)
- func (b *Box) IsFocused() bool
- func (b *Box) MinSizeHint() image.Point
- func (b *Box) OnKeyEvent(ev KeyEvent)
- func (b *Box) Prepend(w Widget)
- func (b *Box) Resize(size image.Point)
- func (b *Box) SetBorder(enabled bool)
- func (b *Box) SetTitle(title string)
- func (b *Box) SizeHint() image.Point
- type Button
- type Color
- type Entry
- type FocusChain
- type Grid
- func (g *Grid) AppendRow(row ...Widget)
- func (g *Grid) Draw(p *Painter)
- func (g *Grid) MinSizeHint() image.Point
- func (g *Grid) OnKeyEvent(ev KeyEvent)
- func (g *Grid) Resize(size image.Point)
- func (g *Grid) SetBorder(enabled bool)
- func (g *Grid) SetCell(pos image.Point, w Widget)
- func (g *Grid) SetColumnStretch(col, stretch int)
- func (g *Grid) SetRowStretch(row, stretch int)
- func (g *Grid) SizeHint() image.Point
- type Key
- type KeyEvent
- type Label
- type List
- func (l *List) AddItems(items ...string)
- func (l *List) Draw(p *Painter)
- func (l *List) Length() int
- func (l *List) OnItemActivated(fn func(*List))
- func (l *List) OnKeyEvent(ev KeyEvent)
- func (l *List) OnSelectionChanged(fn func(*List))
- func (l *List) RemoveItem(i int)
- func (l *List) RemoveItems()
- func (l *List) Select(i int)
- func (l *List) Selected() int
- func (l *List) SelectedItem() string
- func (l *List) SetSelected(i int)
- func (l *List) SizeHint() image.Point
- type ModMask
- type MouseEvent
- type Padder
- func (p *Padder) Draw(painter *Painter)
- func (p *Padder) IsFocused() bool
- func (p *Padder) MinSizeHint() image.Point
- func (p *Padder) OnKeyEvent(ev KeyEvent)
- func (p *Padder) Resize(size image.Point)
- func (p *Padder) SetFocused(f bool)
- func (p *Padder) Size() image.Point
- func (p *Padder) SizeHint() image.Point
- func (p *Padder) SizePolicy() (SizePolicy, SizePolicy)
- type Painter
- func (p *Painter) Begin()
- func (p *Painter) DrawCursor(x, y int)
- func (p *Painter) DrawHorizontalLine(x1, x2, y int)
- func (p *Painter) DrawRect(x, y, w, h int)
- func (p *Painter) DrawRune(x, y int, r rune)
- func (p *Painter) DrawText(x, y int, text string)
- func (p *Painter) DrawVerticalLine(x, y1, y2 int)
- func (p *Painter) End()
- func (p *Painter) FillRect(x, y, w, h int)
- func (p *Painter) Repaint(w Widget)
- func (p *Painter) Restore()
- func (p *Painter) RestoreStyle()
- func (p *Painter) SetStyle(s Style)
- func (p *Painter) Translate(x, y int)
- func (p *Painter) WithMask(r image.Rectangle, fn func(*Painter))
- func (p *Painter) WithStyle(n string, fn func(*Painter))
- type Progress
- type SimpleFocusChain
- type SizePolicy
- type Spacer
- type StatusBar
- type Style
- type Surface
- type Table
- type TextEdit
- type Theme
- type UI
- type Widget
- type WidgetBase
- func (w *WidgetBase) Draw(p *Painter)
- func (w *WidgetBase) IsFocused() bool
- func (w *WidgetBase) MinSizeHint() image.Point
- func (w *WidgetBase) OnKeyEvent(ev KeyEvent)
- func (w *WidgetBase) Resize(size image.Point)
- func (w *WidgetBase) SetFocused(f bool)
- func (w *WidgetBase) SetSizePolicy(h, v SizePolicy)
- func (w *WidgetBase) Size() image.Point
- func (w *WidgetBase) SizeHint() image.Point
- func (w *WidgetBase) SizePolicy() (SizePolicy, SizePolicy)
Constants ¶
const ( KeyBackspace = KeyBS KeyTab = KeyTAB KeyEsc = KeyESC KeyEscape = KeyESC KeyEnter = KeyCR KeyBackspace2 = KeyDEL )
These are aliases for other keys.
Variables ¶
var DefaultFocusChain = &SimpleFocusChain{ widgets: make([]Widget, 0), }
DefaultFocusChain is the default focus chain.
Functions ¶
This section is empty.
Types ¶
type Alignment ¶
type Alignment int
Alignment is used to set the direction in which widgets are laid out.
type Box ¶
type Box struct { WidgetBase // contains filtered or unexported fields }
Box is a layout for placing widgets either horizontally or vertically. If horizontally, all widgets will have the same height. If vertically, they will all have the same width.
func (*Box) MinSizeHint ¶
MinSizeHint returns the minimum size hint for the layout.
func (*Box) OnKeyEvent ¶
OnKeyEvent handles an event and propagates it to all children.
func (*Box) Resize ¶
Resize recursively updates the size of the Box and all the widgets it contains. This is a potentially expensive operation and should be invoked with restraint.
Resize is called by the layout engine and is not intended to be used by end users.
type Button ¶
type Button struct { WidgetBase // contains filtered or unexported fields }
Button is a widget that can be activated to perform some action, or to answer a question.
func (*Button) OnActivated ¶
OnActivated allows a custom function to be run whenever the button is activated.
func (*Button) OnKeyEvent ¶
OnKeyEvent handles keys events.
type Entry ¶
type Entry struct { WidgetBase // contains filtered or unexported fields }
Entry is a one-line text editor. It lets the user supply the application with text, e.g., to input user and password information.
func (*Entry) OnChanged ¶
OnChanged sets a function to be run whenever the content of the entry has been changed.
func (*Entry) OnSubmit ¶
OnSubmit sets a function to be run whenever the user submits the entry (by pressing KeyEnter).
type FocusChain ¶
type FocusChain interface { FocusNext(w Widget) Widget FocusPrev(w Widget) Widget FocusDefault() Widget }
FocusChain enables custom focus traversal when Tab or Backtab is pressed.
type Grid ¶
type Grid struct { WidgetBase // contains filtered or unexported fields }
Grid is a widget that lays out widgets in a grid.
func (*Grid) MinSizeHint ¶
MinSizeHint returns the minimum size hint for the grid.
func (*Grid) Resize ¶
Resize recursively updates the size of the Grid and all the widgets it contains. This is a potentially expensive operation and should be invoked with restraint.
Resize is called by the layout engine and is not intended to be used by end users.
func (*Grid) SetColumnStretch ¶
SetColumnStretch sets the stretch factor for a given column. If stretch > 0, the column will expand to fill up available space. If multiple columns have a stretch factor > 0, stretch determines how much space the column get in respect to the others. E.g. by setting SetColumnStretch(0, 1) and SetColumnStretch(1, 2), the second column will fill up twice as much space as the first one.
func (*Grid) SetRowStretch ¶
SetRowStretch sets the stretch factor for a given row. For more on stretch factors, see SetColumnStretch.
type Key ¶
type Key int16
Key represents both normal and special keys. For normal letters, KeyRune is used together with the Rune field in the KeyEvent.
const ( KeyRune Key = iota + 256 KeyUp KeyDown KeyRight KeyLeft KeyUpLeft KeyUpRight KeyDownLeft KeyDownRight KeyCenter KeyPgUp KeyPgDn KeyHome KeyEnd KeyInsert KeyDelete KeyHelp KeyExit KeyClear KeyCancel KeyPrint KeyPause KeyBacktab KeyF1 KeyF2 KeyF3 KeyF4 KeyF5 KeyF6 KeyF7 KeyF8 KeyF9 KeyF10 KeyF11 KeyF12 KeyF13 KeyF14 KeyF15 KeyF16 KeyF17 KeyF18 KeyF19 KeyF20 KeyF21 KeyF22 KeyF23 KeyF24 KeyF25 KeyF26 KeyF27 KeyF28 KeyF29 KeyF30 KeyF31 KeyF32 KeyF33 KeyF34 KeyF35 KeyF36 KeyF37 KeyF38 KeyF39 KeyF40 KeyF41 KeyF42 KeyF43 KeyF44 KeyF45 KeyF46 KeyF47 KeyF48 KeyF49 KeyF50 KeyF51 KeyF52 KeyF53 KeyF54 KeyF55 KeyF56 KeyF57 KeyF58 KeyF59 KeyF60 KeyF61 KeyF62 KeyF63 KeyF64 )
These are named keys that can be handled.
const ( KeyCtrlSpace Key = iota KeyCtrlA KeyCtrlB KeyCtrlC KeyCtrlD KeyCtrlE KeyCtrlF KeyCtrlG KeyCtrlH KeyCtrlI KeyCtrlJ KeyCtrlK KeyCtrlL KeyCtrlM KeyCtrlN KeyCtrlO KeyCtrlP KeyCtrlQ KeyCtrlR KeyCtrlS KeyCtrlT KeyCtrlU KeyCtrlV KeyCtrlW KeyCtrlX KeyCtrlY KeyCtrlZ KeyCtrlLeftSq // Escape KeyCtrlBackslash KeyCtrlRightSq KeyCtrlCarat KeyCtrlUnderscore )
These are the supported control keys.
const ( KeyNUL Key = iota KeySOH KeySTX KeyETX KeyEOT KeyENQ KeyACK KeyBEL KeyBS KeyTAB KeyLF KeyVT KeyFF KeyCR KeySO KeySI KeyDLE KeyDC1 KeyDC2 KeyDC3 KeyDC4 KeyNAK KeySYN KeyETB KeyCAN KeyEM KeySUB KeyESC KeyFS KeyGS KeyRS KeyUS KeyDEL Key = 0x7F )
These are the defined ASCII values for key codes.
type Label ¶
type Label struct { WidgetBase // contains filtered or unexported fields }
Label is a widget to display read-only text.
func (*Label) MinSizeHint ¶
MinSizeHint returns the minimum size the widget is allowed to be.
func (*Label) SetStyleName ¶
func (*Label) SetWordWrap ¶
SetWordWrap sets whether text content should be wrapped.
type List ¶
type List struct { WidgetBase // contains filtered or unexported fields }
List is a widget for displaying and selecting items.
func (*List) OnItemActivated ¶
func (*List) OnKeyEvent ¶
OnKeyEvent handles terminal events.
func (*List) OnSelectionChanged ¶
func (*List) RemoveItem ¶
func (*List) RemoveItems ¶
func (l *List) RemoveItems()
func (*List) SelectedItem ¶
func (*List) SetSelected ¶
type MouseEvent ¶
MouseEvent represents the event where a mouse button was pressed or released.
type Padder ¶
type Padder struct {
// contains filtered or unexported fields
}
Padder is a widget to fill out space.
func (*Padder) MinSizeHint ¶
MinSizeHint returns the minimum size the widget is allowed to be.
func (*Padder) OnKeyEvent ¶
func (*Padder) SetFocused ¶
SetFocused set the focus on the widget.
func (*Padder) SizePolicy ¶
func (p *Padder) SizePolicy() (SizePolicy, SizePolicy)
SizePolicy returns the default layout behavior.
type Painter ¶
type Painter struct {
// contains filtered or unexported fields
}
Painter provides operations to paint on a surface.
func NewPainter ¶
NewPainter returns a new instance of Painter.
func (*Painter) DrawCursor ¶
func (*Painter) DrawHorizontalLine ¶
func (*Painter) DrawVerticalLine ¶
func (*Painter) Restore ¶
func (p *Painter) Restore()
Restore pops the latest transform from the stack.
func (*Painter) RestoreStyle ¶
func (p *Painter) RestoreStyle()
type Progress ¶
type Progress struct { WidgetBase // contains filtered or unexported fields }
Progress is a widget to display a progress bar.
func (*Progress) MinSizeHint ¶
MinSizeHint returns the minimum size the widget is allowed to be.
func (*Progress) SetCurrent ¶
type SimpleFocusChain ¶
type SimpleFocusChain struct {
// contains filtered or unexported fields
}
SimpleFocusChain represents a ring of widgets where focus is loops to the first widget when it reaches the end.
func (*SimpleFocusChain) FocusDefault ¶
func (c *SimpleFocusChain) FocusDefault() Widget
FocusDefault returns the default widget for when there is no widget currently focused.
func (*SimpleFocusChain) FocusNext ¶
func (c *SimpleFocusChain) FocusNext(current Widget) Widget
FocusNext returns the widget in the ring that is after the given widget.
func (*SimpleFocusChain) FocusPrev ¶
func (c *SimpleFocusChain) FocusPrev(current Widget) Widget
FocusPrev returns the widget in the ring that is before the given widget.
func (*SimpleFocusChain) Set ¶
func (c *SimpleFocusChain) Set(ws ...Widget)
Set sets the widgets in the focus chain. Widgets will received focus in the order widgets were passed.
type SizePolicy ¶
type SizePolicy int
const ( Preferred SizePolicy = iota Minimum Maximum Expanding )
type Spacer ¶
type Spacer struct {
WidgetBase
}
Spacer is a widget to fill out space.
func (*Spacer) MinSizeHint ¶
MinSizeHint returns the minimum size the widget is allowed to be.
func (*Spacer) SizePolicy ¶
func (s *Spacer) SizePolicy() (SizePolicy, SizePolicy)
SizePolicy returns the default layout behavior.
type StatusBar ¶
type StatusBar struct { WidgetBase // contains filtered or unexported fields }
StatusBar is a widget to display status information.
func (*StatusBar) SetPermanentText ¶
func (*StatusBar) SizePolicy ¶
func (b *StatusBar) SizePolicy() (SizePolicy, SizePolicy)
SizePolicy returns the default layout behavior.
type Surface ¶
type Surface interface { SetCell(x, y int, ch rune, s Style) SetCursor(x, y int) HideCursor() Begin() End() Size() image.Point }
Surface defines a surface that can be painted on.
type Table ¶
type Table struct { *Grid // contains filtered or unexported fields }
Table is a widget that lays out widgets in a table.
func (*Table) OnItemActivated ¶
OnItemActivated sets the function that is called when an item was activated.
func (*Table) OnKeyEvent ¶
OnKeyEvent handles an event and propagates it to all children.
func (*Table) OnSelectionChanged ¶
OnSelectionChanged sets the function that is called when an item was selected.
func (*Table) SetSelected ¶
SetSelected changes the currently selected item.
type TextEdit ¶
type TextEdit struct { WidgetBase // contains filtered or unexported fields }
TextEdit is a multi-line text editor.
func (*TextEdit) OnKeyEvent ¶
OnKeyEvent handles terminal events.
func (*TextEdit) OnTextChanged ¶
OnTextChanged sets a function to be run whenever the text content of the widget has been changed.
type UI ¶
type UI interface { SetWidget(w Widget) SetTheme(p *Theme) SetKeybinding(seq string, fn func()) SetFocusChain(ch FocusChain) Run() error Update(fn func()) Quit() }
type Widget ¶
type Widget interface { Draw(p *Painter) MinSizeHint() image.Point Size() image.Point SizeHint() image.Point SizePolicy() (SizePolicy, SizePolicy) Resize(size image.Point) OnKeyEvent(ev KeyEvent) SetFocused(bool) IsFocused() bool }
Widget defines common operations on widgets.
type WidgetBase ¶
type WidgetBase struct {
// contains filtered or unexported fields
}
func (*WidgetBase) Draw ¶
func (w *WidgetBase) Draw(p *Painter)
func (*WidgetBase) IsFocused ¶
func (w *WidgetBase) IsFocused() bool
func (*WidgetBase) MinSizeHint ¶
func (w *WidgetBase) MinSizeHint() image.Point
func (*WidgetBase) OnKeyEvent ¶
func (w *WidgetBase) OnKeyEvent(ev KeyEvent)
func (*WidgetBase) Resize ¶
func (w *WidgetBase) Resize(size image.Point)
func (*WidgetBase) SetFocused ¶
func (w *WidgetBase) SetFocused(f bool)
func (*WidgetBase) SetSizePolicy ¶
func (w *WidgetBase) SetSizePolicy(h, v SizePolicy)
func (*WidgetBase) Size ¶
func (w *WidgetBase) Size() image.Point
func (*WidgetBase) SizeHint ¶
func (w *WidgetBase) SizeHint() image.Point
func (*WidgetBase) SizePolicy ¶
func (w *WidgetBase) SizePolicy() (SizePolicy, SizePolicy)