Categorygithub.com/bsdavidson/termui
modulepackage
2.1.1+incompatible
Repository: https://github.com/bsdavidson/termui.git
Documentation: pkg.go.dev

# README

termui Build Status Doc Status

demo cast under osx 10.10; Terminal.app; Menlo Regular 12pt.)

termui is a cross-platform, easy-to-compile, and fully-customizable terminal dashboard. It is inspired by blessed-contrib, but purely in Go.

Now version v2 has arrived! It brings new event system, new theme system, new Buffer interface and specific colour text rendering. (some docs are missing, but it will be completed soon!)

Installation

master mirrors v2 branch, to install:

go get -u github.com/gizak/termui

It is recommanded to use locked deps by using glide: move to termui src directory then run glide up.

For the compatible reason, you can choose to install the legacy version of termui:

go get gopkg.in/gizak/termui.v1

Usage

Layout

To use termui, the very first thing you may want to know is how to manage layout. termui offers two ways of doing this, known as absolute layout and grid layout.

Absolute layout

Each widget has an underlying block structure which basically is a box model. It has border, label and padding properties. A border of a widget can be chosen to hide or display (with its border label), you can pick a different front/back colour for the border as well. To display such a widget at a specific location in terminal window, you need to assign .X, .Y, .Height, .Width values for each widget before sending it to .Render. Let's demonstrate these by a code snippet:

	import ui "github.com/gizak/termui" // <- ui shortcut, optional

	func main() {
		err := ui.Init()
		if err != nil {
			panic(err)
		}
		defer ui.Close()

		p := ui.NewPar(":PRESS q TO QUIT DEMO")
		p.Height = 3
		p.Width = 50
		p.TextFgColor = ui.ColorWhite
		p.BorderLabel = "Text Box"
		p.BorderFg = ui.ColorCyan

		g := ui.NewGauge()
		g.Percent = 50
		g.Width = 50
		g.Height = 3
		g.Y = 11
		g.BorderLabel = "Gauge"
		g.BarColor = ui.ColorRed
		g.BorderFg = ui.ColorWhite
		g.BorderLabelFg = ui.ColorCyan

		ui.Render(p, g) // feel free to call Render, it's async and non-block

		// event handler...
	}

Note that components can be overlapped (I'd rather call this a feature...), Render(rs ...Renderer) renders its args from left to right (i.e. each component's weight is arising from left to right).

Grid layout:

grid

Grid layout uses 12 columns grid system with expressive syntax. To use Grid, all we need to do is build a widget tree consisting of Rows and Cols (Actually a Col is also a Row but with a widget endpoint attached).

	import ui "github.com/gizak/termui"
	// init and create widgets...

	// build
	ui.Body.AddRows(
		ui.NewRow(
			ui.NewCol(6, 0, widget0),
			ui.NewCol(6, 0, widget1)),
		ui.NewRow(
			ui.NewCol(3, 0, widget2),
			ui.NewCol(3, 0, widget30, widget31, widget32),
			ui.NewCol(6, 0, widget4)))

	// calculate layout
	ui.Body.Align()

	ui.Render(ui.Body)

Events

termui ships with a http-like event mux handling system. All events are channeled up from different sources (typing, click, windows resize, custom event) and then encoded as universal Event object. Event.Path indicates the event type and Event.Data stores the event data struct. Add a handler to a certain event is easy as below:

	// handle key q pressing
	ui.Handle("/sys/kbd/q", func(ui.Event) {
		// press q to quit
		ui.StopLoop()
	})

	ui.Handle("/sys/kbd/C-x", func(ui.Event) {
		// handle Ctrl + x combination
	})

	ui.Handle("/sys/kbd", func(ui.Event) {
		// handle all other key pressing
	})

	// handle a 1s timer
	ui.Handle("/timer/1s", func(e ui.Event) {
		t := e.Data.(ui.EvtTimer)
		// t is a EvtTimer
		if t.Count%2 ==0 {
			// do something
		}
	})

	ui.Loop() // block until StopLoop is called

Widgets

Click image to see the corresponding demo codes.

par list gauge linechart barchart barchart sparklines

GoDoc

godoc

TODO

  • Grid layout
  • Event system
  • Canvas widget
  • Refine APIs
  • Focusable widgets

Changelog

License

This library is under the MIT License

# Packages

No description provided by the author
No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Close finalizes termui library, should be called after successful initialization when termui's functionality isn't required anymore.
0<=r,g,b <= 5.
DTrimTxCls trims the overflowed text cells sequence and append dots at the end.
No description provided by the author
No description provided by the author
Init initializes termui library.
No description provided by the author
No description provided by the author
No description provided by the author
NewBarChart returns a new *BarChart with current theme.
NewBlock returns a *Block which inherits styles from current theme.
NewBuffer returns a new Buffer.
NewCanvas returns an empty Canvas.
NewCell returns a new cell.
NewCol accepts: widgets are LayoutBufferer or widgets is A NewRow.
No description provided by the author
NewFilledBuffer returns a new Buffer filled with ch, fb and bg.
NewGauge return a new gauge with current theme.
NewGrid returns *Grid with given rows.
NewLineChart returns a new LineChart with current theme.
NewList returns a new *List with current theme.
NewMarkdownTxBuilder returns a TextBuilder employing markdown syntax.
NewBarChart returns a new *BarChart with current theme.
NewPar returns a new *Par with given text as its content.
NewRow creates a new row out of given columns.
NewSparkline returns a unrenderable single sparkline that intended to be added into Sparklines.
NewSparklines return a new *Spaklines with given Sparkline(s), you can always add a new Sparkline later.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
StringToAttribute converts text to a termui attribute.
TermHeight returns the current terminal's height.
No description provided by the author
TermWidth returns the current terminal's width.
TextCells returns a coloured text cells []Cell.
No description provided by the author
TrimStr2Runes trims string to w[-1 rune], appends …, and returns the runes of that string if string is grather then n.
TrimStrIfAppropriate trim string to "s[:-1] + …" if string > width otherwise return string.
TrimTxCells trims the overflowed text cells sequence.

# Constants

All supported positions.
All supported positions.
All supported positions.
All supported positions.
All supported positions.
All supported positions.
All supported positions.
All supported positions.
Text style.
Text style.
Text style.
No description provided by the author
No description provided by the author
8 basic clolrs.
8 basic clolrs.
8 basic clolrs.
8 basic clolrs.
8 basic clolrs.
8 basic clolrs.
8 basic clolrs.
No description provided by the author
8 basic clolrs.
8 basic clolrs.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Have a constant that defines number of colors.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
DefaultTxBuilder is set to be MarkdownTxBuilder.
No description provided by the author
No description provided by the author

# Structs

BarChart creates multiple bars in a widget: bc := termui.NewBarChart() data := []int{3, 2, 5, 3, 9, 5} bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"} bc.BorderLabel = "Bar Chart" bc.Data = data bc.Width = 26 bc.Height = 10 bc.DataLabels = bclabels bc.TextColor = termui.ColorGreen bc.BarColor = termui.ColorRed bc.NumColor = termui.ColorYellow */.
Block is a base struct for all other upper level widgets, consider it as css: display:block.
Buffer is a renderable rectangle cell data container.
Cell is a rune with assigned Fg and Bg.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Grid implements 12 columns system.
Hline is a horizontal line.
LineChart has two modes: braille(default) and dot.
List displays []string as its items, it has a Overflow option (default is "hidden"), when set to "hidden", the item exceeding List's width is truncated, but when set to "wrap", the overflowed text breaks into next line.
MarkdownTxBuilder implements TextBuilder interface, using markdown syntax.
This is the implemetation of multi-colored or stacked bar graph.
Par displays a paragraph.
Row builds a layout tree.
Sparkline is like: ▅▆▂▂▅▇▂▂▃▆▆▆▅▃.
Sparklines is a renderable widget which groups together the given sparklines.
Vline is a vertical line.
No description provided by the author

# Interfaces

Bufferer should be implemented by all renderable components.
GridBufferer introduces a Bufferer that can be manipulated by Grid.
TextBuilder is a minimal interface to produce text []Cell using specific syntax (markdown).
No description provided by the author

# Type aliases

Align is the position of the gauge's label.
Attribute is printable cell's color and style.
Canvas contains drawing map: i,j -> rune.
No description provided by the author
event mixins.