termui

package module
v0.0.0-...-1a54575 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2019 License: AGPL-3.0 Imports: 9 Imported by: 13

README

DEPRECATED: Please use the upstream termui repo now that development has resumed there.

termui

A fork of termui with a lot of code cleanup and (frequently asked for) improvements.

You can see an implementation/example usage of this library here.

Some usage improvements include:

  • better event/key-combo names
  • more convenient event handling function
  • 256 colors
  • better grid system
  • linegraph uses drawille-go
    • no longer have to choose between dot mode and braille mode; uses a superior braille mode
  • table supports mouse and keyboard navigation
  • table is scrollable
  • more powerful table column width sizing
  • visual improvements to linegraph and table

TODO:

  • readd widgets that were removed like the list and bargraph
  • focusable widgets

Documentation

Index

Constants

View Source
const (
	TOP_RIGHT       = '┐'
	VERTICAL_LINE   = '│'
	HORIZONTAL_LINE = '─'
	TOP_LEFT        = '┌'
	BOTTOM_RIGHT    = '┘'
	BOTTOM_LEFT     = '└'
	VERTICAL_LEFT   = '┤'
	VERTICAL_RIGHT  = '├'
	HORIZONTAL_DOWN = '┬'
	HORIZONTAL_UP   = '┴'
	QUOTA_LEFT      = '«'
	QUOTA_RIGHT     = '»'
)
View Source
const ColorDefault = -1

ColorDefault = clear

View Source
const DOTS = '…'

Variables

View Source
var SPARKS = [8]rune{'▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'}
View Source
var Theme = Colorscheme{
	Fg: 7,
	Bg: -1,

	LabelFg:  7,
	LabelBg:  -1,
	BorderFg: 6,
	BorderBg: -1,

	Sparkline:   4,
	LineGraph:   0,
	TableCursor: 4,
	GaugeColor:  7,
}

Functions

func Clear

func Clear()

Clear clears the screen with the default Bg color.

func Close

func Close()

Close finalizes termui library. It should be called after successful initialization when termui's functionality isn't required anymore.

func Error

func Error(issue, diagnostics string)

func Init

func Init() error

Init initializes termui library. This function should be called before any others. After initialization, the library must be finalized by 'Close' function.

func MaxString

func MaxString(s string, l int) string

MaxString trims a string and adds dots if the string is longer than a give length.

func PollEvents

func PollEvents() <-chan Event

PollEvents gets events from termbox, converts them, then sends them to each of its channels.

func Render

func Render(bs ...Bufferer)

Render renders all Bufferers in the given order to termbox, then asks termbox to print the screen.

func Round

func Round(f float64) float64

Types

type Block

type Block struct {
	Grid     image.Rectangle
	X        int // largest X value in the inner square
	Y        int // largest Y value in the inner square
	XOffset  int // the X position of the widget on the terminal
	YOffset  int // the Y position of the widget on the terminal
	Label    string
	BorderFg Color
	BorderBg Color
	LabelFg  Color
	LabelBg  Color
	Fg       Color
	Bg       Color
}

Block is a base struct for all other upper level widgets.

func NewBlock

func NewBlock() *Block

NewBlock returns a *Block which inherits styles from the current theme.

func (*Block) Buffer

func (self *Block) Buffer() *Buffer

Buffer implements Bufferer interface and draws background, border, and borderlabel.

func (*Block) GetXOffset

func (self *Block) GetXOffset() int

GetXOffset implements Bufferer interface.

func (*Block) GetYOffset

func (self *Block) GetYOffset() int

GetYOffset implements Bufferer interface.

func (*Block) Resize

func (self *Block) Resize(termWidth, termHeight, termCols, termRows int)

Resize computes Height, Width, XOffset, and YOffset given terminal dimensions.

func (*Block) SetGrid

func (self *Block) SetGrid(c0, r0, c1, r1 int)

SetGrid create a rectangle representing the block's dimensions in the grid.

type Buffer

type Buffer struct {
	Area    image.Rectangle // selected drawing area
	CellMap map[image.Point]Cell
}

Buffer is a renderable rectangle cell data container.

func NewBuffer

func NewBuffer() *Buffer

NewBuffer returns a new empty Buffer.

func NewFilledBuffer

func NewFilledBuffer(x0, y0, x1, y1 int, c Cell) *Buffer

NewFilledBuffer returns a new Buffer filled with the given Cell.

func (*Buffer) At

func (self *Buffer) At(x, y int) Cell

At returns the cell at (x,y).

func (*Buffer) Fill

func (self *Buffer) Fill(c Cell)

Fill fills the Buffer with a Cell.

func (*Buffer) Merge

func (self *Buffer) Merge(bs ...*Buffer)

Merge merges the given buffers onto the current Buffer.

func (*Buffer) MergeWithOffset

func (self *Buffer) MergeWithOffset(buf *Buffer, xOffset, yOffset int)

MergeWithOffset merges a Buffer onto another with an offset.

func (*Buffer) SetArea

func (self *Buffer) SetArea(r image.Rectangle)

SetArea assigns a new rect area to self.

func (*Buffer) SetAreaXY

func (self *Buffer) SetAreaXY(x, y int)

SetAreaXY sets the Buffer bounds from (0,0) to (x,y).

func (*Buffer) SetCell

func (self *Buffer) SetCell(x, y int, c Cell)

SetCell assigns a Cell to (x,y).

func (*Buffer) SetString

func (self *Buffer) SetString(x, y int, s string, fg, bg Color)

SetString assigns a string to a Buffer starting at (x,y).

type Bufferer

type Bufferer interface {
	Buffer() *Buffer
	GetXOffset() int
	GetYOffset() int
}

Bufferer should be implemented by all renderable components.

type Cell

type Cell struct {
	Ch rune
	Fg Color
	Bg Color
}

Cell is a rune with assigned Fg and Bg.

func NewCell

func NewCell(ch rune, Fg, Bg Color) Cell

NewCell returne a new Cell given all necessary fields.

type Color

type Color int

Color is an integer in the range -1 to 255. -1 is clear, while 0-255 are xterm 256 colors.

const (
	AttrBold Color = 1 << (iota + 9)
	AttrUnderline
	AttrReverse
)

Copied from termbox. Attributes that can be bitwise OR'ed with a color.

type Colorscheme

type Colorscheme struct {
	Fg Color
	Bg Color

	LabelFg  Color
	LabelBg  Color
	BorderFg Color
	BorderBg Color

	Sparkline   Color
	LineGraph   Color
	TableCursor Color
	GaugeColor  Color
}

A Colorscheme represents the current look-and-feel of the dashboard.

type Event

type Event struct {
	Type    EventType
	ID      string
	Payload interface{}
}

type EventType

type EventType int
const (
	KeyboardEvent EventType = iota
	MouseEvent
	ResizeEvent
)

type Gauge

type Gauge struct {
	*Block
	Percent     int
	GaugeColor  Color
	Description string
}

Gauge is a progress bar like widget.

func NewGauge

func NewGauge() *Gauge

NewGauge return a new gauge with current theme.

func (*Gauge) Buffer

func (self *Gauge) Buffer() *Buffer

Buffer implements Bufferer interface.

type Grid

type Grid struct {
	Widgets []GridBufferer
	Width   int
	Height  int
	Cols    int
	Rows    int
}

Grid holds widgets and information about terminal dimensions. Widgets are adjusted and rendered through the grid.

var Body *Grid

func NewGrid

func NewGrid() *Grid

NewGrid creates an empty Grid.

func (*Grid) Buffer

func (self *Grid) Buffer() *Buffer

Buffer implements the Bufferer interface by merging each widget in Grid into one buffer.

func (*Grid) GetXOffset

func (self *Grid) GetXOffset() int

GetXOffset implements Bufferer interface.

func (*Grid) GetYOffset

func (self *Grid) GetYOffset() int

GetYOffset implements Bufferer interface.

func (*Grid) Resize

func (self *Grid) Resize()

Resize resizes each widget in the grid.

func (*Grid) Set

func (self *Grid) Set(x0, y0, x1, y1 int, widget GridBufferer)

Set assigns a widget and its grid dimensions to Grid.

type GridBufferer

type GridBufferer interface {
	Bufferer
	Resize(int, int, int, int)
	SetGrid(int, int, int, int)
}

GridBufferer introduces a Bufferer that can be manipulated by Grid.

type LineGraph

type LineGraph struct {
	*Block
	Data      map[string][]float64
	LineColor map[string]Color
	Zoom      int
	Labels    map[string]string

	DefaultLineColor Color
}

LineGraph implements a line graph of data points.

func NewLineGraph

func NewLineGraph() *LineGraph

NewLineGraph returns a new LineGraph with current theme.

func (*LineGraph) Buffer

func (self *LineGraph) Buffer() *Buffer

Buffer implements Bufferer interface.

type Mouse

type Mouse struct {
	Drag bool
	X    int
	Y    int
}

Mouse payload.

type Resize

type Resize struct {
	Width  int
	Height int
}

Resize payload.

type Sparkline

type Sparkline struct {
	Data       []int
	Title1     string
	Title2     string
	TitleColor Color
	LineColor  Color
}

Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃. The data points should be non-negative integers.

func NewSparkline

func NewSparkline() *Sparkline

NewSparkline returns an unrenderable single sparkline that intended to be added into a Sparklines.

type Sparklines

type Sparklines struct {
	*Block
	Lines []*Sparkline
}

Sparklines is a renderable widget which groups together the given sparklines.

func NewSparklines

func NewSparklines(ss ...*Sparkline) *Sparklines

NewSparklines return a new *Sparklines with given Sparklines, you can always add a new Sparkline later.

func (*Sparklines) Add

func (self *Sparklines) Add(sl Sparkline)

Add appends a given Sparkline to the *Sparklines.

func (*Sparklines) Buffer

func (self *Sparklines) Buffer() *Buffer

Buffer implements Bufferer interface.

type Table

type Table struct {
	*Block

	Header []string
	Rows   [][]string

	ColWidths  []int
	CellXPos   []int  // column position
	ColResizer func() // for widgets that inherit a Table and want to overload the ColResize method
	Gap        int    // gap between columns
	PadLeft    int

	Cursor      bool
	CursorColor Color

	UniqueCol    int    // the column used to identify the selected item
	SelectedItem string // used to keep the cursor on the correct item if the data changes
	SelectedRow  int
	TopRow       int // used to indicate where in the table we are scrolled at
}

Table tracks all the attributes of a Table instance

func NewTable

func NewTable() *Table

NewTable returns a new Table instance

func (*Table) Bottom

func (self *Table) Bottom()

func (*Table) Buffer

func (self *Table) Buffer() *Buffer

Buffer implements the Bufferer interface.

func (*Table) Click

func (self *Table) Click(x, y int)

func (*Table) ColResize

func (self *Table) ColResize()

ColResize is the default column resizer, but can be overriden. ColResize calculates the width of each column.

func (*Table) Down

func (self *Table) Down()

func (*Table) HalfPageDown

func (self *Table) HalfPageDown()

func (*Table) HalfPageUp

func (self *Table) HalfPageUp()

func (*Table) PageDown

func (self *Table) PageDown()

func (*Table) PageUp

func (self *Table) PageUp()

func (*Table) Top

func (self *Table) Top()

func (*Table) Up

func (self *Table) Up()

Jump to

Keyboard shortcuts

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