ttk

package module
v0.0.0-...-45a9b93 Latest Latest
Warning

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

Go to latest
Published: May 7, 2020 License: ISC Imports: 8 Imported by: 2

README

ttk

Documentation

[GoDoc] (http://godoc.org/github.com/companyzero/ttk)

Full go doc style documentation for the project can be viewed online without installing this package by using the GoDoc site here: http://godoc.org/github.com/companyzero/ttk

You can also view the documentation locally once the package is installed with the godoc tool by running godoc -http=":6060" and pointing your browser to http://localhost:6060/pkg/github.com/companyzero/ttk

Installation

$ go get -u github.com/companyzero/ttk

License

Package ttk is licensed under the copyfree ISC License.

Documentation

Overview

Copyright (c) 2016 Company 0, LLC. Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Index

Constants

View Source
const (
	// see http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
	ANSIFg = 30
	ANSIBg = 40

	AttrNA        = -1
	AttrReset     = 0
	AttrBold      = 1
	AttrUnderline = 3
	AttrReverse   = 7

	ColorBlack   = 0
	ColorRed     = 1
	ColorGreen   = 2
	ColorYellow  = 3
	ColorBlue    = 4
	ColorMagenta = 5
	ColorCyan    = 6
	ColorWhite   = 7
)
View Source
const (
	WidgetEdit = "edit"
)

WidgetEdit uniquely identifies the edit widget.

View Source
const (
	WidgetLabel = "label"
)

WidgetLabel uniquely identifies the label widget.

View Source
const (
	WidgetList = "list"
)

WidgetList uniquely identifies the list widget.

Variables

View Source
var (
	ErrNotEscSequence    = errors.New("not an escape sequence")
	ErrInvalidColor      = errors.New("invalid parameters for sequence")
	ErrInvalidAttribute  = errors.New("invalid attribute")
	ErrInvalidForeground = errors.New("invalid foreground")
	ErrInvalidBackground = errors.New("invalid background")
)
View Source
var (
	// ErrAlreadyInitialized is used on reentrant calls of Init.
	ErrAlreadyInitialized = errors.New("terminal already initialized")
)
View Source
var (
	// ErrWidgetNotRegistered is generated when a NewWidget call is made
	// and the widget was not registered.  Widgets must be registered so
	// that applications may define their own and fully participate in ttk
	// rules.
	ErrWidgetNotRegistered = errors.New("widget not registered")
)

Functions

func Color

func Color(at, fg, bg int) (string, error)

Color creates an ANSI compatible escape sequence that encodes colors and attributes.

func Deinit

func Deinit()

Deinit switches the terminal back to cooked mode and it terminates managed window mode. Init must be called again if a switch is required again. Deinit shall be called on application exit; failing to do so may leave the terminal corrupted. If that does happen typing "reset" on the shell usually fixes this problem.

func EscapedLen

func EscapedLen(s string) int

EscapedLen returns total length of all escape sequences in a given string.

func Exit

func Exit(format string, args ...interface{})

Exit application but deinit first so that the terminal will not be corrupt.

func Flush

func Flush()

Flush copies focused window backing store onto the physical screen.

func Focus

func Focus(w *Window)

Focus on provided window. This will implicitly focus on a window widget that can have focus. Render and flush it onto the terminal.

func FocusPrevious

func FocusPrevious()

FocusPrevious focus on previous focused window. This will implicitly focus on a window widget that can have focus. Render and flush it onto the terminal.

func ForwardKey

func ForwardKey(k Key)

ForwardKey must be called from the application to route key strokes to windows. The life cycle of keystrokes is as follows: widgets -> global application context -> window. Care must be taken in the application to not rely on keystrokes that widgets may use.

func Init

func Init() error

Init switches the terminal to raw mode and commences managed window mode. This function shall be called prior to any ttk calls.

func KeyChannel

func KeyChannel() chan Key

KeyChannel returns the the Key channel that can be used in the application to handle keystrokes.

func Panic

func Panic(format string, args ...interface{})

Panic application but deinit first so that the terminal will not be corrupt.

func Queue

func Queue(f func())

Queue sends work to the queue and returns almost immediately.

func Unescape

func Unescape(s string) string

Unescape returns the unescaped string.

Types

type Attributes

type Attributes struct {
	Fg termbox.Attribute // foreground
	Bg termbox.Attribute // background
}

Attributes represents attributes which are defined as text color, bold, blink etc.

func DecodeColor

func DecodeColor(esc string) (*Attributes, int, error)

DecodeColor decodes an ANSI color escape sequence and ignores trailing characters. It returns an Attributes type that can be used directly in termbox (note that the termbox colors are off by one). The skip contains the location of the next character that was not consumed by the escape sequence.

func DefaultAttributes

func DefaultAttributes() Attributes

DefaultAttributes returns the default attributes. This is a blocking call.

type Cell

type Cell struct {
	termbox.Cell // anon since we are only adding the dirty bit
	// contains filtered or unexported fields
}

Cell contains a single screen cell. This structure exists in order to mark cells that require rendering. This is required in order to only render deltas, this matters over slow links.

type Edit

type Edit struct {
	Widget
	// contains filtered or unexported fields
}

Edit is a text entry widget. It prints the contents of target onto the window. Note: all spaces are trimmed before and after the target string.

func (*Edit) CanFocus

func (e *Edit) CanFocus() bool

CanFocus implements the interface. This is called from queue context so be careful to not use blocking calls.

func (*Edit) Focus

func (e *Edit) Focus()

Focus implements the interface. This is called from queue context so be careful to not use blocking calls.

func (*Edit) GetText

func (e *Edit) GetText() string

GetText returns the edit text. GetText shall be called from queue context.

func (*Edit) KeyHandler

func (e *Edit) KeyHandler(ev termbox.Event) bool

KeyHandler implements the interface. This is called from queue context so be careful to not use blocking calls.

func (*Edit) Render

func (e *Edit) Render()

Render implements the Render interface. This is called from queue context so be careful to not use blocking calls.

func (*Edit) Resize

func (e *Edit) Resize()

func (*Edit) SetAttributes

func (e *Edit) SetAttributes(a Attributes)

SetAttributes sets the Attributes. This will not be displayed immediately. SetAttributes shall be called from queue context.

func (*Edit) SetText

func (e *Edit) SetText(s *string, end bool)

SetText sets the edit text. if end is set to true the cursor and text will be set to the end of the string. This will not be displayed immediately. SetText shall be called from queue context.

func (*Edit) Visibility

func (e *Edit) Visibility(op Visibility) Visibility

type Justify

type Justify int

Justify is used to determine where text is printed on the Status widget (special Label)

const (
	JustifyLeft Justify = iota
	JustifyRight
	JustifyCenter
)

Justify rules for Status widget.

type Key

type Key struct {
	Mod    termbox.Modifier // key modifier
	Key    termbox.Key      // special key
	Ch     rune             // normal key
	Window Windower         // window that contains widget
	Widget Widgeter         // widget that emitted key
}

Key contains a key stroke and possible modifiers.

type Label

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

Label is a text only widget. It prints the contents of text onto the window.

func (*Label) CanFocus

func (l *Label) CanFocus() bool

CanFocus implements the interface. This is called from queue context so be careful to not use blocking calls.

func (*Label) Focus

func (l *Label) Focus()

Focus implements the interface. This is called from queue context so be careful to not use blocking calls.

func (*Label) KeyHandler

func (l *Label) KeyHandler(ev termbox.Event) bool

KeyHandler implements the interface. This is called from queue context so be careful to not use blocking calls.

func (*Label) Render

func (l *Label) Render()

Render implements the Render interface. This is called from queue context so be careful to not use blocking calls.

func (*Label) Resize

func (l *Label) Resize()

func (*Label) SetAttributes

func (l *Label) SetAttributes(a Attributes)

SetAttributes sets the Attributes. This will not be displayed immediately. SetAttributes shall be called from queue context.

func (*Label) SetText

func (l *Label) SetText(format string, args ...interface{})

SetText sets the label caption. This will not be displayed immediately. SetText shall be called from queue context.

func (*Label) Visibility

func (l *Label) Visibility(op Visibility) Visibility

type List

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

List is a text only widget. It prints the contents of text onto the window.

func (*List) Append

func (l *List) Append(format string, args ...interface{})

Append adds a line of text to the list. Append must be called from queue.

func (*List) CanFocus

func (l *List) CanFocus() bool

CanFocus implements the interface. This is called from queue context so be careful to not use blocking calls.

func (*List) Display

func (l *List) Display(where Location)

Display renders the widget. This is called from queue context so be careful to not use blocking calls.

func (*List) Focus

func (l *List) Focus()

Focus implements the interface. This is called from queue context so be careful to not use blocking calls.

func (*List) IsPaging

func (l *List) IsPaging() bool

IsPaging indicates if the widget is displaying bottom line. If the bottom line is being displayed it means that the appended text is rendered.

func (*List) KeyHandler

func (l *List) KeyHandler(ev termbox.Event) bool

KeyHandler implements the interface. This is called from queue context so be careful to not use blocking calls.

func (*List) Len

func (l *List) Len() int

Len returns the number of lines in this List widget.

func (*List) Render

func (l *List) Render()

Render implements the Render interface. This is called from queue context so be careful to not use blocking calls.

func (*List) Resize

func (l *List) Resize()

func (*List) SetAttributes

func (l *List) SetAttributes(a Attributes)

SetAttributes sets the Attributes. This will not be displayed immediately. SetAttributes shall be called from queue context.

func (*List) Visibility

func (l *List) Visibility(op Visibility) Visibility

func (*List) Width

func (l *List) Width() int

Len returns the width of the List widget.

type Location

type Location int

Location are hints for the Display function.

const (
	Top     Location = iota // Display top line of list
	Bottom                  // Display bottom line of list
	Up                      // Render page up on list
	Down                    // Render page down on list
	Current                 // Render current location on list
)

type Visibility

type Visibility int
const (
	VisibilityGet Visibility = iota
	VisibilityHide
	VisibilityShow
)

type Widget

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

Widget is the base structure of all widgets.

func MakeWidget

func MakeWidget(w *Window, x, y int) Widget

MakeWidget creates a generic Widget structure.

type Widgeter

type Widgeter interface {
	CanFocus() bool                   // return true if widget can focus
	Focus()                           // Focus on widget
	Render()                          // Render the widget
	Resize()                          // Resize the widget
	KeyHandler(termbox.Event) bool    // handle key strokes
	Visibility(Visibility) Visibility // show/hide widget
}

Widgeter is the generic Widget interface. All widgets shall conform to it. Since the Widgeter functions are called from queue context the Widget must take care to not call blocking queue context calls.

func NewEdit

func NewEdit(w *Window, x, y int) (Widgeter, error)

NewEdit is the Edit initializer. This call implements the NewWidget convention by taking a *Window and and an anchor point to render the widget.

func NewLabel

func NewLabel(w *Window, x, y int) (Widgeter, error)

NewLabel is the Label initializer. This call implements the NewWidget convention by taking a *Window and and an anchor point to render the widget.

func NewList

func NewList(w *Window, x, y int) (Widgeter, error)

NewList is the List initializer. This call implements the NewWidget convention by taking a *Window and and an anchor point to render the widget.

type Window

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

Window contains a window context.

func NewWindow

func NewWindow(manager Windower) *Window

NewWindow creates a new window type.

func (*Window) AddEdit

func (w *Window) AddEdit(x, y, width int, target *string) *Edit

AddEdit is a convenience function to add a new edit to a window. Capacity and width determine the maxima of the returned value. It wraps the AddWidget call. AddEdit must be called from queue.

func (*Window) AddLabel

func (w *Window) AddLabel(x, y int, format string, args ...interface{}) *Label

AddLabel is a convenience function to add a new label to a window. It wraps the AddWidget call. AddLabel must be called from queue.

func (*Window) AddList

func (w *Window) AddList(x, y, width, height int) *List

AddList is a convenience function to add a new list to a window. It wraps the AddWidget call. AddList must be called from queue.

func (*Window) AddStatus

func (w *Window) AddStatus(y int, j Justify, format string,
	args ...interface{}) *Label

AddStatus is an alternative Label initializer. A Status is a label that has the property that it fills an entire line and is justified. This call implements the NewWidget convention by taking a *Window and and an anchor point to render the widget.

func (*Window) AddWidget

func (w *Window) AddWidget(id string, x, y int) (Widgeter, error)

AddWidget is the generic function to add a widget to a window. This function should only be called by widgets. Application code, by convention, should call the non-generic type asserted call (i.e. AddLabel). AddWidget shall be called from queue context.

func (*Window) FocusNext

func (w *Window) FocusNext()

FocusNext focuses on the next available widget.

func (*Window) FocusPrevious

func (w *Window) FocusPrevious()

FocusPrevious focuses on the next available widget.

type Windower

type Windower interface {
	Init(w *Window)
	Render(w *Window)
	KeyHandler(*Window, Key)
}

Windower interface. Each window has a Windower interface associated with it. It provides all functions to deal with rendering and user interaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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