tui

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2019 License: MIT Imports: 12 Imported by: 1

README

tui: Terminal UI for Go

Build Status GoDoc Go Report Card License MIT

A UI library for terminal applications.

tui (pronounced tooey) provides a higher-level programming model for building rich terminal applications. It lets you build layout-based user interfaces that (should) gracefully handle resizing for you.

IMPORTANT: tui-go is still in an experimental phase so please don't use it for anything other than experiments, yet.

Update: I created tui-go as an experiment because I wanted a simpler way of creating terminal-based user interfaces. It has since then become a project, with all the work that comes with it. While it's been really fun, unfortunately I'm no longer able to maintain this project.

Since I started working on tui-go, a number of similar projects have popped up. One that I think shows great promise is rivo/tview, which embodies much of what I envisioned for tui-go. I highly recommend trying it out!

Thanks all of you who have contributed and supported tui-go!

Screenshot

Installation

go get github.com/marcusolsson/tui-go

Usage

package main

import "github.com/marcusolsson/tui-go"

func main() {
	box := tui.NewVBox(
		tui.NewLabel("tui-go"),
	)

	ui, err := tui.New(box)
	if err != nil {
		panic(err)
	}
	ui.SetKeybinding("Esc", func() { ui.Quit() })

	if err := ui.Run(); err != nil {
		panic(err)
	}
}

Getting started

If you want to know what it is like to build terminal applications with tui-go, check out some of the examples.

Documentation is available at godoc.org.

Make sure you check out some of the projects using tui-go.

Once you've gotten started developing your first application with tui-go, you might be interested in learning about common patterns or how you can debug your applications.

tui-go is mainly influenced by Qt and offers a similar programming model that has been adapted to Go and the terminal.

For an overview of the alternatives for writing terminal user interfaces, check out this article by AppliedGo.

License

tui-go is released under the MIT License.

Contact

If you're interested in chatting with users and contributors, join #tui-go on the Gophers Slack. If you're not already a part of the Slack workspace, you can join here. If you prefer a lower-bandwidth interface, see this article on connecting to Slack via IRC or XMPP.

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:

http://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum

Index

Constants

View Source
const (
	KeyBackspace  = KeyBS
	KeyTab        = KeyTAB
	KeyEsc        = KeyESC
	KeyEscape     = KeyESC
	KeyEnter      = KeyCR
	KeyBackspace2 = KeyDEL
)

These are aliases for other keys.

Variables

View Source
var DefaultFocusChain = &SimpleFocusChain{
	widgets: make([]Widget, 0),
}

DefaultFocusChain is the default focus chain.

View Source
var DefaultTheme = &Theme{
	styles: map[string]Style{
		"list.item.selected":  {Reverse: DecorationOn},
		"table.cell.selected": {Reverse: DecorationOn},
		"button.focused":      {Reverse: DecorationOn},
	},
}

DefaultTheme is a theme with reasonable defaults.

Functions

func SetLogger

func SetLogger(l Logger)

SetLogger sets the logger that is used in tui.

Types

type Alignment

type Alignment int

Alignment is used to set the direction in which widgets are laid out.

const (
	Horizontal Alignment = iota
	Vertical
)

Available alignment options.

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 NewHBox

func NewHBox(c ...Widget) *Box

NewHBox returns a new horizontally aligned Box.

func NewVBox

func NewVBox(c ...Widget) *Box

NewVBox returns a new vertically aligned Box.

func (*Box) Alignment

func (b *Box) Alignment() Alignment

Alignment returns the current alignment of the Box.

func (*Box) Append

func (b *Box) Append(w Widget)

Append adds the given widget at the end of the Box.

func (*Box) Draw

func (b *Box) Draw(p *Painter)

Draw recursively draws the widgets it contains.

func (*Box) Insert

func (b *Box) Insert(i int, w Widget)

Insert adds the widget into the Box at a given index.

func (*Box) IsFocused

func (b *Box) IsFocused() bool

IsFocused return true if one of the children is focused.

func (*Box) Length

func (b *Box) Length() int

Length returns the number of items in the box.

func (*Box) MinSizeHint

func (b *Box) MinSizeHint() image.Point

MinSizeHint returns the minimum size hint for the layout.

func (*Box) OnKeyEvent

func (b *Box) OnKeyEvent(ev KeyEvent)

OnKeyEvent handles an event and propagates it to all children.

func (*Box) Prepend

func (b *Box) Prepend(w Widget)

Prepend adds the given widget at the start of the Box.

func (*Box) Remove

func (b *Box) Remove(i int)

Remove deletes the widget from the Box at a given index.

func (*Box) Resize

func (b *Box) Resize(size image.Point)

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.

func (*Box) SetBorder

func (b *Box) SetBorder(enabled bool)

SetBorder sets whether the border is visible or not.

func (*Box) SetTitle

func (b *Box) SetTitle(title string)

SetTitle sets the title of the box.

func (*Box) SizeHint

func (b *Box) SizeHint() image.Point

SizeHint returns the recommended size hint for the layout.

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 NewButton

func NewButton(text string) *Button

NewButton returns a new Button with the given text as the label.

func (*Button) Draw

func (b *Button) Draw(p *Painter)

Draw draws the button.

func (*Button) OnActivated

func (b *Button) OnActivated(fn func(b *Button))

OnActivated allows a custom function to be run whenever the button is activated.

func (*Button) OnKeyEvent

func (b *Button) OnKeyEvent(ev KeyEvent)

OnKeyEvent handles keys events.

func (*Button) SizeHint

func (b *Button) SizeHint() image.Point

SizeHint returns the recommended size hint for the button.

type Color

type Color int32

Color represents a color.

const (
	ColorDefault Color = iota
	ColorBlack
	ColorWhite
	ColorRed
	ColorGreen
	ColorBlue
	ColorCyan
	ColorMagenta
	ColorYellow
)

Common colors.

type Decoration

type Decoration int

Decoration represents a bold/underline/etc. state

const (
	DecorationInherit Decoration = iota
	DecorationOn
	DecorationOff
)

Decoration modes: Inherit from parent widget, explicitly on, or explicitly off.

type EchoMode

type EchoMode int

EchoMode is used to determine the visibility of Entry text.

const (
	// EchoModeNormal displays the characters as they're being entered.
	EchoModeNormal EchoMode = iota

	// EchoModeNoEcho disables text display. This is useful for when the length
	// of the password should be kept secret.
	EchoModeNoEcho

	// EchoModePassword replaces all characters with asterisks.
	EchoModePassword
)

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 NewEntry

func NewEntry() *Entry

NewEntry returns a new Entry.

func (*Entry) Draw

func (e *Entry) Draw(p *Painter)

Draw draws the entry.

func (*Entry) OnChanged

func (e *Entry) OnChanged(fn func(entry *Entry))

OnChanged sets a function to be run whenever the content of the entry has been changed.

func (*Entry) OnKeyEvent

func (e *Entry) OnKeyEvent(ev KeyEvent)

OnKeyEvent handles key events.

func (*Entry) OnSubmit

func (e *Entry) OnSubmit(fn func(entry *Entry))

OnSubmit sets a function to be run whenever the user submits the entry (by pressing KeyEnter).

func (*Entry) SetEchoMode

func (e *Entry) SetEchoMode(m EchoMode)

SetEchoMode sets the echo mode of the entry.

func (*Entry) SetText

func (e *Entry) SetText(text string)

SetText sets the text content of the entry.

func (*Entry) SizeHint

func (e *Entry) SizeHint() image.Point

SizeHint returns the recommended size hint for the entry.

func (*Entry) Text

func (e *Entry) Text() string

Text returns the text content of the entry.

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 NewGrid

func NewGrid(cols, rows int) *Grid

NewGrid returns a new Grid.

func (*Grid) AppendRow

func (g *Grid) AppendRow(row ...Widget)

AppendRow adds a new row at the end.

func (*Grid) Draw

func (g *Grid) Draw(p *Painter)

Draw draws the grid.

func (*Grid) MinSizeHint

func (g *Grid) MinSizeHint() image.Point

MinSizeHint returns the minimum size hint for the grid.

func (*Grid) OnKeyEvent

func (g *Grid) OnKeyEvent(ev KeyEvent)

OnKeyEvent handles key events.

func (*Grid) RemoveRow

func (g *Grid) RemoveRow(index int)

RemoveRow removes the row ( at index ) from the grid

func (*Grid) RemoveRows

func (g *Grid) RemoveRows()

RemoveRows will remove all the rows in grid

func (*Grid) Resize

func (g *Grid) Resize(size image.Point)

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) SetBorder

func (g *Grid) SetBorder(enabled bool)

SetBorder sets whether the border is visible or not.

func (*Grid) SetCell

func (g *Grid) SetCell(pos image.Point, w Widget)

SetCell sets or replaces the contents of a cell.

func (*Grid) SetColumnStretch

func (g *Grid) SetColumnStretch(col, stretch int)

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

func (g *Grid) SetRowStretch(row, stretch int)

SetRowStretch sets the stretch factor for a given row. For more on stretch factors, see SetColumnStretch.

func (*Grid) SizeHint

func (g *Grid) SizeHint() image.Point

SizeHint returns the recommended size hint for the grid.

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 KeyEvent

type KeyEvent struct {
	Key       Key
	Rune      rune
	Modifiers ModMask
}

KeyEvent represents a key press.

func (*KeyEvent) Name

func (ev *KeyEvent) Name() string

Name returns a user-friendly description of the key press.

type Label

type Label struct {
	WidgetBase
	// contains filtered or unexported fields
}

Label is a widget to display read-only text.

func NewLabel

func NewLabel(text string) *Label

NewLabel returns a new Label.

func (*Label) Draw

func (l *Label) Draw(p *Painter)

Draw draws the label.

func (*Label) MinSizeHint

func (l *Label) MinSizeHint() image.Point

MinSizeHint returns the minimum size the widget is allowed to be.

func (*Label) Resize

func (l *Label) Resize(size image.Point)

Resize changes the size of the Widget.

func (*Label) SetStyleName

func (l *Label) SetStyleName(style string)

SetStyleName sets the identifier used for custom styling.

func (*Label) SetText

func (l *Label) SetText(text string)

SetText sets the text content of the label.

func (*Label) SetWordWrap

func (l *Label) SetWordWrap(enabled bool)

SetWordWrap sets whether text content should be wrapped.

func (*Label) SizeHint

func (l *Label) SizeHint() image.Point

SizeHint returns the recommended size for the label.

func (*Label) Text

func (l *Label) Text() string

Text returns the text content of the label.

type List

type List struct {
	WidgetBase
	// contains filtered or unexported fields
}

List is a widget for displaying and selecting items.

func NewList

func NewList() *List

NewList returns a new List with no selection.

func (*List) AddItems

func (l *List) AddItems(items ...string)

AddItems appends items to the end of the list.

func (*List) Draw

func (l *List) Draw(p *Painter)

Draw draws the list.

func (*List) Length

func (l *List) Length() int

Length returns the number of items in the list.

func (*List) OnItemActivated

func (l *List) OnItemActivated(fn func(*List))

OnItemActivated gets called when activated (through pressing KeyEnter).

func (*List) OnKeyEvent

func (l *List) OnKeyEvent(ev KeyEvent)

OnKeyEvent handles terminal events.

func (*List) OnSelectionChanged

func (l *List) OnSelectionChanged(fn func(*List))

OnSelectionChanged gets called whenever a new item is selected.

func (*List) RemoveItem

func (l *List) RemoveItem(i int)

RemoveItem removes the item at the given position.

func (*List) RemoveItems

func (l *List) RemoveItems()

RemoveItems clears all the items from the list.

func (*List) Select

func (l *List) Select(i int)

Select calls SetSelected and the OnSelectionChanged function.

func (*List) Selected

func (l *List) Selected() int

Selected returns the index of the currently selected item.

func (*List) SelectedItem

func (l *List) SelectedItem() string

SelectedItem returns the currently selected item.

func (*List) SetSelected

func (l *List) SetSelected(i int)

SetSelected sets the currently selected item.

func (*List) SizeHint

func (l *List) SizeHint() image.Point

SizeHint returns the recommended size for the list.

type Logger

type Logger interface {
	Printf(format string, args ...interface{})
}

Logger provides a interface for the standard logger.

type ModMask

type ModMask int16

ModMask is a mask of modifier keys.

const (
	ModShift ModMask = 1 << iota
	ModCtrl
	ModAlt
	ModMeta
	ModNone ModMask = 0
)

Modifiers that can be sent with a KeyEvent or a MouseEvent.

type MouseEvent

type MouseEvent struct {
	Pos image.Point
}

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. It adds empty space of a specified size to the outside of its contained Widget.

func NewPadder

func NewPadder(x, y int, w Widget) *Padder

NewPadder returns a new Padder. The enclosed Widget is given horizontal margin of x on the right and x on the left, and a vertical margin of y on the top and y on the bottom.

func (*Padder) Draw

func (p *Padder) Draw(painter *Painter)

Draw draws the padded widget.

func (*Padder) IsFocused

func (p *Padder) IsFocused() bool

IsFocused returns true if the widget is focused.

func (*Padder) MinSizeHint

func (p *Padder) MinSizeHint() image.Point

MinSizeHint returns the minimum size the widget is allowed to be.

func (*Padder) OnKeyEvent

func (p *Padder) OnKeyEvent(ev KeyEvent)

OnKeyEvent handles key events.

func (*Padder) Resize

func (p *Padder) Resize(size image.Point)

Resize updates the size of the padded widget.

func (*Padder) SetFocused

func (p *Padder) SetFocused(f bool)

SetFocused set the focus on the widget.

func (*Padder) Size

func (p *Padder) Size() image.Point

Size returns the size of the padded widget.

func (*Padder) SizeHint

func (p *Padder) SizeHint() image.Point

SizeHint returns the recommended size for the padded 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

func NewPainter(s Surface, p *Theme) *Painter

NewPainter returns a new instance of Painter.

func (*Painter) Begin

func (p *Painter) Begin()

Begin prepares the surface for painting.

func (*Painter) DrawCursor

func (p *Painter) DrawCursor(x, y int)

DrawCursor draws the cursor at the given position.

func (*Painter) DrawHorizontalLine

func (p *Painter) DrawHorizontalLine(x1, x2, y int)

DrawHorizontalLine paints a horizontal line using box characters.

func (*Painter) DrawRect

func (p *Painter) DrawRect(x, y, w, h int)

DrawRect paints a rectangle using box characters.

func (*Painter) DrawRune

func (p *Painter) DrawRune(x, y int, r rune)

DrawRune paints a rune at the given coordinate.

func (*Painter) DrawText

func (p *Painter) DrawText(x, y int, text string)

DrawText paints a string starting at the given coordinate.

func (*Painter) DrawVerticalLine

func (p *Painter) DrawVerticalLine(x, y1, y2 int)

DrawVerticalLine paints a vertical line using box characters.

func (*Painter) End

func (p *Painter) End()

End finalizes any painting that has been made.

func (*Painter) FillRect

func (p *Painter) FillRect(x, y, w, h int)

FillRect clears a rectangular area with whitespace.

func (*Painter) Repaint

func (p *Painter) Repaint(w Widget)

Repaint clears the surface, draws the scene and flushes it.

func (*Painter) Restore

func (p *Painter) Restore()

Restore pops the latest transform from the stack.

func (*Painter) SetStyle

func (p *Painter) SetStyle(s Style)

SetStyle sets the style used when painting.

func (*Painter) Translate

func (p *Painter) Translate(x, y int)

Translate pushes a new translation transform to the stack.

func (*Painter) WithMask

func (p *Painter) WithMask(r image.Rectangle, fn func(*Painter))

WithMask masks a painter to restrict painting within the given rectangle.

func (*Painter) WithStyle

func (p *Painter) WithStyle(n string, fn func(*Painter))

WithStyle executes the provided function with the named Style applied on top of the current one.

type Progress

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

Progress is a widget to display a progress bar.

func NewProgress

func NewProgress(max int) *Progress

NewProgress returns a new Progress.

func (*Progress) Draw

func (p *Progress) Draw(painter *Painter)

Draw draws the progress bar.

func (*Progress) MinSizeHint

func (p *Progress) MinSizeHint() image.Point

MinSizeHint returns the minimum size the widget is allowed to be.

func (*Progress) SetCurrent

func (p *Progress) SetCurrent(c int)

SetCurrent sets the current progress.

func (*Progress) SetMax

func (p *Progress) SetMax(m int)

SetMax sets the maximum progress.

func (*Progress) SizeHint

func (p *Progress) SizeHint() image.Point

SizeHint returns the recommended size for the progress bar.

type RuneBuffer

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

RuneBuffer provides readline functionality for text widgets.

func (*RuneBuffer) Backspace

func (r *RuneBuffer) Backspace()

Backspace deletes the rune left of the cursor.

func (*RuneBuffer) CursorPos

func (r *RuneBuffer) CursorPos() image.Point

CursorPos returns the coordinate for the cursor for a given width.

func (*RuneBuffer) Delete

func (r *RuneBuffer) Delete()

Delete deletes the rune at the current cursor position.

func (*RuneBuffer) Kill

func (r *RuneBuffer) Kill()

Kill deletes all runes from the cursor until the end of the line.

func (*RuneBuffer) Len

func (r *RuneBuffer) Len() int

Len returns the number of runes in the buffer.

func (*RuneBuffer) MoveBackward

func (r *RuneBuffer) MoveBackward()

MoveBackward moves the cursor back by one rune.

func (*RuneBuffer) MoveForward

func (r *RuneBuffer) MoveForward()

MoveForward moves the cursor forward by one rune.

func (*RuneBuffer) MoveToLineEnd

func (r *RuneBuffer) MoveToLineEnd()

MoveToLineEnd moves the cursor to the end of the current line.

func (*RuneBuffer) MoveToLineStart

func (r *RuneBuffer) MoveToLineStart()

MoveToLineStart moves the cursor to the start of the current line.

func (*RuneBuffer) Pos

func (r *RuneBuffer) Pos() int

Pos returns the current index in the buffer.

func (*RuneBuffer) Runes

func (r *RuneBuffer) Runes() []rune

Runes return the buffer

func (*RuneBuffer) Set

func (r *RuneBuffer) Set(buf []rune)

Set the buffer and the index at the end of the buffer.

func (*RuneBuffer) SetMaxWidth

func (r *RuneBuffer) SetMaxWidth(w int)

SetMaxWidth sets the maximum text width.

func (*RuneBuffer) SetWithIdx

func (r *RuneBuffer) SetWithIdx(idx int, buf []rune)

SetWithIdx set the the buffer with a given index.

func (*RuneBuffer) SplitByLine

func (r *RuneBuffer) SplitByLine() []string

SplitByLine returns the lines for a given width.

func (*RuneBuffer) String

func (r *RuneBuffer) String() string

func (*RuneBuffer) Width

func (r *RuneBuffer) Width() int

Width returns the width of the rune buffer, taking into account for CJK.

func (*RuneBuffer) WriteRune

func (r *RuneBuffer) WriteRune(s rune)

WriteRune appends a rune to the buffer.

func (*RuneBuffer) WriteRunes

func (r *RuneBuffer) WriteRunes(s []rune)

WriteRunes appends runes to the buffer.

type ScrollArea

type ScrollArea struct {
	WidgetBase

	Widget Widget
	// contains filtered or unexported fields
}

ScrollArea is a widget to fill out space.

func NewScrollArea

func NewScrollArea(w Widget) *ScrollArea

NewScrollArea returns a new ScrollArea.

func (*ScrollArea) Draw

func (s *ScrollArea) Draw(p *Painter)

Draw draws the scroll area.

func (*ScrollArea) MinSizeHint

func (s *ScrollArea) MinSizeHint() image.Point

MinSizeHint returns the minimum size the widget is allowed to be.

func (*ScrollArea) Resize

func (s *ScrollArea) Resize(size image.Point)

Resize resizes the scroll area and the underlying widget.

func (*ScrollArea) Scroll

func (s *ScrollArea) Scroll(dx, dy int)

Scroll shifts the views over the content.

func (*ScrollArea) ScrollToBottom

func (s *ScrollArea) ScrollToBottom()

ScrollToBottom ensures the bottom-most part of the scroll area is visible.

func (*ScrollArea) ScrollToTop

func (s *ScrollArea) ScrollToTop()

ScrollToTop resets the vertical scroll position.

func (*ScrollArea) SetAutoscrollToBottom

func (s *ScrollArea) SetAutoscrollToBottom(autoscroll bool)

SetAutoscrollToBottom makes sure the content is scrolled to bottom on resize.

func (*ScrollArea) SizeHint

func (s *ScrollArea) SizeHint() image.Point

SizeHint returns the size hint of the underlying widget.

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

SizePolicy determines the space occupied by a widget.

const (
	// Preferred interprets the size hint as the preferred size.
	Preferred SizePolicy = iota
	// Minimum allows the widget to shrink down to the size hint.
	Minimum
	// Maximum allows the widget to grow up to the size hint.
	Maximum
	// Expanding makes the widget expand to the available space.
	Expanding
)

type Spacer

type Spacer struct {
	WidgetBase
}

Spacer is a widget to fill out space.

func NewSpacer

func NewSpacer() *Spacer

NewSpacer returns a new Spacer.

func (*Spacer) MinSizeHint

func (s *Spacer) MinSizeHint() image.Point

MinSizeHint returns the minimum size the widget is allowed to be.

func (*Spacer) SizeHint

func (s *Spacer) SizeHint() image.Point

SizeHint returns the recommended size for the spacer.

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 NewStatusBar

func NewStatusBar(text string) *StatusBar

NewStatusBar returns a new StatusBar.

func (*StatusBar) Draw

func (b *StatusBar) Draw(p *Painter)

Draw draws the status bar.

func (*StatusBar) SetPermanentText

func (b *StatusBar) SetPermanentText(text string)

SetPermanentText sets the permanent text of the status bar.

func (*StatusBar) SetText

func (b *StatusBar) SetText(text string)

SetText sets the text content of the status bar.

func (*StatusBar) SizeHint

func (b *StatusBar) SizeHint() image.Point

SizeHint returns the recommended size for the status bar.

func (*StatusBar) SizePolicy

func (b *StatusBar) SizePolicy() (SizePolicy, SizePolicy)

SizePolicy returns the default layout behavior.

type Style

type Style struct {
	Fg Color
	Bg Color

	Reverse   Decoration
	Bold      Decoration
	Underline Decoration
}

Style determines how a cell should be painted. The zero value uses default from

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 NewTable

func NewTable(cols, rows int) *Table

NewTable returns a new Table.

func (*Table) Draw

func (t *Table) Draw(p *Painter)

Draw draws the table.

func (*Table) OnItemActivated

func (t *Table) OnItemActivated(fn func(*Table))

OnItemActivated sets the function that is called when an item was activated.

func (*Table) OnKeyEvent

func (t *Table) OnKeyEvent(ev KeyEvent)

OnKeyEvent handles an event and propagates it to all children.

func (*Table) OnSelectionChanged

func (t *Table) OnSelectionChanged(fn func(*Table))

OnSelectionChanged sets the function that is called when an item was selected.

func (*Table) RemoveRow

func (t *Table) RemoveRow(index int)

RemoveRow removes specific row from the table

func (*Table) RemoveRows

func (t *Table) RemoveRows()

RemoveRows removes all the rows added to the table.

func (*Table) Select

func (t *Table) Select(i int)

Select calls SetSelected and the OnSelectionChanged function.

func (*Table) Selected

func (t *Table) Selected() int

Selected returns the index of the currently selected item.

func (*Table) SetSelected

func (t *Table) SetSelected(i int)

SetSelected changes the currently selected item.

type TestSurface

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

A TestSurface implements the Surface interface with local buffers, and provides accessors to check the output of a draw operation on the Surface.

func NewTestSurface

func NewTestSurface(w, h int) *TestSurface

NewTestSurface returns a new TestSurface.

func (*TestSurface) Begin

func (s *TestSurface) Begin()

Begin resets the state of the TestSurface, clearing all cells. It must be called before drawing the Surface.

func (*TestSurface) BgColors

func (s *TestSurface) BgColors() string

BgColors renders the TestSurface's background colors, using the digits 0-7 for painted cells, and the empty character for unpainted cells.

func (*TestSurface) Decorations

func (s *TestSurface) Decorations() string

Decorations renders the TestSurface's decorations (Reverse, Bold, Underline) using a bitmask:

Reverse: 1
Bold: 2
Underline: 4

func (*TestSurface) End

func (s *TestSurface) End()

End indicates the surface has been painted on, and can be rendered. It's a no-op for TestSurface.

func (*TestSurface) FgColors

func (s *TestSurface) FgColors() string

FgColors renders the TestSurface's foreground colors, using the digits 0-7 for painted cells, and the empty character for unpainted cells.

func (*TestSurface) HideCursor

func (s *TestSurface) HideCursor()

HideCursor removes the cursor from the display.

func (*TestSurface) SetCell

func (s *TestSurface) SetCell(x, y int, ch rune, style Style)

SetCell sets the contents of the addressed cell.

func (*TestSurface) SetCursor

func (s *TestSurface) SetCursor(x, y int)

SetCursor moves the Surface's cursor to the specified position.

func (*TestSurface) Size

func (s *TestSurface) Size() image.Point

Size returns the dimensions of the surface.

func (*TestSurface) String

func (s *TestSurface) String() string

String returns the characters written to the TestSurface.

type TextEdit

type TextEdit struct {
	WidgetBase
	// contains filtered or unexported fields
}

TextEdit is a multi-line text editor.

func NewTextEdit

func NewTextEdit() *TextEdit

NewTextEdit returns a new TextEdit.

func (*TextEdit) Draw

func (e *TextEdit) Draw(p *Painter)

Draw draws the entry.

func (*TextEdit) OnKeyEvent

func (e *TextEdit) OnKeyEvent(ev KeyEvent)

OnKeyEvent handles key events.

func (*TextEdit) OnTextChanged

func (e *TextEdit) OnTextChanged(fn func(entry *TextEdit))

OnTextChanged sets a function to be run whenever the text content of the widget has been changed.

func (*TextEdit) SetText

func (e *TextEdit) SetText(text string)

SetText sets the text content of the entry.

func (*TextEdit) SetWordWrap

func (e *TextEdit) SetWordWrap(enabled bool)

SetWordWrap sets whether the text should wrap or not.

func (*TextEdit) SizeHint

func (e *TextEdit) SizeHint() image.Point

SizeHint returns the recommended size for the entry.

func (*TextEdit) Text

func (e *TextEdit) Text() string

Text returns the text content of the entry.

type Theme

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

Theme defines the styles for a set of identifiers.

func NewTheme

func NewTheme() *Theme

NewTheme return an empty theme.

func (*Theme) HasStyle

func (p *Theme) HasStyle(name string) bool

HasStyle returns whether an identifier is associated with an identifier.

func (*Theme) SetStyle

func (p *Theme) SetStyle(n string, i Style)

SetStyle sets a style for a given identifier.

func (*Theme) Style

func (p *Theme) Style(name string) Style

Style returns the style associated with an identifier. If there is no Style associated with the name, it returns a default Style.

type UI

type UI interface {
	// SetWidget sets the root widget of the UI.
	SetWidget(w Widget)
	// SetTheme sets the current theme of the UI.
	SetTheme(p *Theme)
	// SetKeybinding sets the callback for when a key sequence is pressed.
	SetKeybinding(seq string, fn func())
	// ClearKeybindings removes all previous set keybindings.
	ClearKeybindings()
	// SetFocusChain sets a chain of widgets that determines focus order.
	SetFocusChain(ch FocusChain)
	// Run starts the UI goroutine and blocks either Quit was called or an error occurred.
	Run() error
	// Update schedules work in the UI thread and await its completion.
	// Note that calling Update from the UI thread will result in deadlock.
	Update(fn func())
	// Quit shuts down the UI goroutine.
	Quit()
	// Repaint the UI
	Repaint()
}

UI defines the operations needed by the underlying engine.

func New

func New(root Widget) (UI, error)

New returns a new UI with a root widget.

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
}

WidgetBase defines base attributes and operations for all widgets.

func (*WidgetBase) Draw

func (w *WidgetBase) Draw(p *Painter)

Draw is an empty operation to fulfill the Widget interface.

func (*WidgetBase) IsFocused

func (w *WidgetBase) IsFocused() bool

IsFocused returns whether the widget is focused.

func (*WidgetBase) MinSizeHint

func (w *WidgetBase) MinSizeHint() image.Point

MinSizeHint returns the size below which the widget cannot shrink.

func (*WidgetBase) OnKeyEvent

func (w *WidgetBase) OnKeyEvent(ev KeyEvent)

OnKeyEvent is an empty operation to fulfill the Widget interface.

func (*WidgetBase) Resize

func (w *WidgetBase) Resize(size image.Point)

Resize sets the size of the widget.

func (*WidgetBase) SetFocused

func (w *WidgetBase) SetFocused(f bool)

SetFocused focuses the widget.

func (*WidgetBase) SetSizePolicy

func (w *WidgetBase) SetSizePolicy(h, v SizePolicy)

SetSizePolicy sets the size policy for horizontal and vertical directions.

func (*WidgetBase) Size

func (w *WidgetBase) Size() image.Point

Size returns the current size of the widget.

func (*WidgetBase) SizeHint

func (w *WidgetBase) SizeHint() image.Point

SizeHint returns the size hint of the widget.

func (*WidgetBase) SizePolicy

func (w *WidgetBase) SizePolicy() (SizePolicy, SizePolicy)

SizePolicy returns the current size policy.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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