Categorygithub.com/gcla/gowid
modulepackage
1.4.0
Repository: https://github.com/gcla/gowid.git
Documentation: pkg.go.dev

# README

Terminal User Interface Widgets in Go

Gowid provides widgets and a framework for making terminal user interfaces. It's written in Go and inspired by urwid.

Widgets out-of-the-box include:

  • input components like button, checkbox and an editable text field with support for passwords
  • layout components for arranging widgets in columns, rows and a grid
  • structured components - a tree, an infinite list and a table
  • pre-canned widgets - a progress bar, a modal dialog, a bar graph and a menu
  • a VT220-compatible terminal widget, heavily cribbed from urwid :smiley:

All widgets support interaction with the mouse when the terminal allows.

Gowid is built on top of the fantastic tcell package.

There are many alternatives to gowid - see Similar Projects

The most developed gowid application is currently termshark, a terminal UI for tshark.

Installation

go get github.com/gcla/gowid/...

Examples

Make sure $GOPATH/bin is in your PATH (or ~/go/bin if GOPATH isn't set), then tab complete "gowid-" e.g.

gowid-fib

Here is a port of urwid's palette example:

Here is urwid's graph example:

And urwid's fibonacci example:

A demonstration of gowid's terminal widget, a port of urwid's terminal widget:

Finally, here is an animation of termshark in action:

Hello World

This example is an attempt to mimic urwid's "Hello World" example.

package main

import (
	"github.com/gcla/gowid"
	"github.com/gcla/gowid/widgets/divider"
	"github.com/gcla/gowid/widgets/pile"
	"github.com/gcla/gowid/widgets/styled"
	"github.com/gcla/gowid/widgets/text"
	"github.com/gcla/gowid/widgets/vpadding"
)

//======================================================================

func main() {

	palette := gowid.Palette{
		"banner":  gowid.MakePaletteEntry(gowid.ColorWhite, gowid.MakeRGBColor("#60d")),
		"streak":  gowid.MakePaletteEntry(gowid.ColorNone, gowid.MakeRGBColor("#60a")),
		"inside":  gowid.MakePaletteEntry(gowid.ColorNone, gowid.MakeRGBColor("#808")),
		"outside": gowid.MakePaletteEntry(gowid.ColorNone, gowid.MakeRGBColor("#a06")),
		"bg":      gowid.MakePaletteEntry(gowid.ColorNone, gowid.MakeRGBColor("#d06")),
	}

	div := divider.NewBlank()
	outside := styled.New(div, gowid.MakePaletteRef("outside"))
	inside := styled.New(div, gowid.MakePaletteRef("inside"))

	helloworld := styled.New(
		text.NewFromContentExt(
			text.NewContent([]text.ContentSegment{
				text.StyledContent("Hello World", gowid.MakePaletteRef("banner")),
			}),
			text.Options{
				Align: gowid.HAlignMiddle{},
			},
		),
		gowid.MakePaletteRef("streak"),
	)

	f := gowid.RenderFlow{}

	view := styled.New(
		vpadding.New(
			pile.New([]gowid.IContainerWidget{
				&gowid.ContainerWidget{IWidget: outside, D: f},
				&gowid.ContainerWidget{IWidget: inside, D: f},
				&gowid.ContainerWidget{IWidget: helloworld, D: f},
				&gowid.ContainerWidget{IWidget: inside, D: f},
				&gowid.ContainerWidget{IWidget: outside, D: f},
			}),
			gowid.VAlignMiddle{},
			f),
		gowid.MakePaletteRef("bg"),
	)

	app, _ := gowid.NewApp(gowid.AppArgs{
		View:    view,
		Palette: &palette,
	})
    
	app.SimpleMainLoop()
}

Running the example above displays this:

Documentation

Similar Projects

Gowid is late to the TUI party. There are many options from which to choose - please read https://appliedgo.net/tui/ for a nice summary for the Go language. Here is a selection:

  • urwid - one of the oldest, for those working in python
  • tview - active, polished, concise, lots of widgets, Go
  • termui - focus on graphing and dataviz, Go
  • gocui - focus on layout, good input options, mouse support, Go
  • clui - active, many widgets, mouse support, Go
  • tui-go - QT-inspired, experimental, nice examples, Go

Dependencies

Gowid depends on these great open-source packages:

  • urwid - not a Go-dependency, but the model for most of gowid's design
  • tcell - a cell based view for text terminals, like xterm, inspired by termbox
  • asciigraph - lightweight ASCII line-graphs for Go
  • logrus - structured pluggable logging for Go
  • testify - tools for testifying that your code will behave as you intend

Contact

License

License: MIT

# Packages

No description provided by the author
Package gwtest provides utilities for testing gowid widgets.
Package gwutil provides general-purpose utilities that are not used by the core of gowid but that have proved useful for several pre-canned widgets.
Package vim provides utilities for parsing and generating vim-like keystrokes.
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
CalculateRenderSizeFallback can be used by widgets that cannot easily compute a value for RenderSize without actually rendering the widget and measuring the bounding box.
No description provided by the author
CellFromRune returns a Cell with the supplied rune and with default coloring and styling.
CellsFromString is a utility function to turn a string into an array of Cells.
ChangeFocus is a general algorithm for applying a change of focus to a type.
ComputeHorizontalSubSize is used to determine the size with which a child widget should be rendered given the parent's render size, and an IWidgetDimension.
ComputeHorizontalSubSizeUnsafe calls ComputeHorizontalSubSize but returns only a single value - the IRenderSize.
TODO - doc.
No description provided by the author
ComputeVerticalSubSize is used to determine the size with which a child widget should be rendered given the parent's render size, and an IWidgetDimension.
ComputeVerticalSubSizeUnsafe calls ComputeVerticalSubSize but returns only a single value - the IRenderSize.
CopyModeUserInput processes copy mode events in a typical fashion - a widget that wraps one with potentially copyable information could defer to this implementation of UserInput.
CopyWidgets is a trivial utility to return a copy of the array of widgets supplied.
Draw will render a Canvas to a tcell Screen.
EmptyLine provides a ready-allocated source of empty cells.
FindInHierarchy starts at w, and applies the supplied predicate function; if it returns true, w is returned.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
FocusPath returns a list of positions, each representing the focus position at that level in the widget hierarchy.
HandleQuitKeys is provided as a simple way to terminate your application using typical "quit" keys - q/Q, ctrl-c, escape.
IColorToTCell is a utility function that will convert an IColor to a TCellColor in preparation for passing to tcell to render; if the conversion fails, a default TCellColor is returned (provided to the function via a parameter).
No description provided by the author
No description provided by the author
No description provided by the author
MakeCell returns a Cell initialized with the supplied run (char to display), foreground color, background color and style attributes.
MakeCellStyle constructs a tcell.Style from gowid colors and styles.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
MakeColorSafe returns a Color struct specified by the string argument, in a do-what-I-mean fashion - it tries the Color struct maker functions in a pre-determined order until one successfully initialized a Color, or until all fail - in which case an error is returned.
No description provided by the author
No description provided by the author
MakeGrayColor returns an initialized GrayColor provided with a string input like "g50" or "g#ab".
MakeGrayColorSafe returns an initialized GrayColor provided with a string input like "g50" or "g#ab".
No description provided by the author
No description provided by the author
No description provided by the author
MakePaletteEntry stores the two IColor parameters provided, and has no style preference.
MakePaletteRef returns a PaletteRef struct storing the (string) name of the PaletteEntry which will be looked up in the IRenderContext.
No description provided by the author
No description provided by the author
No description provided by the author
MakeRGBColor constructs an RGBColor from a string e.g.
MakeRGBColorExt builds an RGBColor from the red, green and blue components provided as integers.
MakeRGBColorExtSafe builds an RGBColor from the red, green and blue components provided as integers.
MakeRGBColorSafe does the same as MakeRGBColor except will return an error if provided with invalid input.
No description provided by the author
MakeStyledPaletteEntry simply stores the three parameters provided - a foreground and background IColor, and a StyleAttrs struct.
MakeStyleMod implements ICellStyler and stores two ICellStylers, one to layer on top of the other.
MakeTCellColor returns an initialized TCellColor given a string input like "yellow".
MakeTCellColor returns an initialized TCellColor given a tcell.Color input.
MakeTCellNoColor returns an initialized TCellColor that represents "no color" - meaning if another color is rendered "under" this one, then the color underneath will be displayed.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewCanvas returns an initialized Canvas struct.
NewCanvasOfSize returns a canvas struct of size cols x rows, where each Cell is default-initialized (i.e.
NewCanvasOfSize returns a canvas struct of size cols x rows, where each Cell is initialized by copying the fill argument.
NewCanvasWithLines allocates a canvas struct and sets its contents to the 2-d array provided as an argument.
NewUrwidColorSafe returns a pointer to an UrwidColor struct and builds the UrwidColor from a string argument e.g.
NewUrwidColorSafe returns a pointer to an UrwidColor struct and builds the UrwidColor from a string argument e.g.
PanicIfCanvasNotRightSize is for debugging - it panics if the size of the supplied canvas does not conform to the size specified by the size argument.
PrefPosition repeatedly unpacks composite widgets until it has to stop.
QuitFn can be used to construct a widget callback that terminates your application.
RangeOverCanvas applies the supplied function to each cell, modifying it in place.
No description provided by the author
RenderRoot is called from the App application object when beginning the widget rendering process.
RenderSize currently passes control through to the widget's RenderSize method.
No description provided by the author
SelectableIfAnySubWidgetsAre is useful for various container widgets.
SetFocusPath takes an array of focus positions, and applies them down the widget hierarchy starting at the supplied widget, w.
No description provided by the author
SubWidgetSize currently passes control through to the widget's SubWidgetSize method.
TranslatedMouseEvent is supplied with a tcell event and an x and y offset - it returns a tcell mouse event that represents a horizontal and vertical translation.
UserInputIfSelectable will return false if the widget is not selectable; otherwise it will try the widget's UserInput function.
No description provided by the author
WriteToCanvas extracts the logic of implementing io.Writer into a free function that can be used by any canvas implementing ICanvas.

# Constants

No description provided by the author
No description provided by the author
Mode16Colors represents a terminal with 16-color support.
Mode24BitColors represents a terminal with 24-bit color support like KDE's terminal.
Mode256Colors represents a terminal with 256-color support.
Mode88Colors represents a terminal with 88-color support such as rxvt.
Mode8Colors represents a terminal with 8-color support.
Mode8Colors represents a terminal with support for monochrome only.
These are used as bitmasks - a style is two AttrMasks.
Just unstyled text.

# Variables

AllStyleMasks is an array of all the styles that can be applied to a Cell.
No description provided by the author
Some pre-initialized color objects for use in applications e.g.
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
ColorDefault is an affirmative preference for the default terminal color.
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
ColorNone means no preference if anything is layered underneath.
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
one side of the color cube.
first index of color cube.
No description provided by the author
IgnoreBase16 should be set to true if gowid should not consider colors 0-21 for closest-match when interpolating RGB colors in 256-color space.
IgnoreUnhandledInput is a helper function for main loops that don't need to deal with hanlding input that the widgets haven't claimed.
No description provided by the author
No description provided by the author
StyleBlink specifies the text should blink, but expresses no preference for other text styles.
StyleBlinkOnly specifies the text should blink, and no other styling should apply.
StyleBold specifies the text should be bold, but expresses no preference for other text styles.
StyleBoldOnly specifies the text should be bold, and no other styling should apply.
StyleDim specifies the text should be dim, but expresses no preference for other text styles.
StyleDimOnly specifies the text should be dim, and no other styling should apply.
StyleNone expresses no preference for any text styles.
StyleReverse specifies the text should be displayed as reverse-video, but expresses no preference for other text styles.
StyleReverseOnly specifies the text should be displayed reverse-video, and no other styling should apply.
StyleUnderline specifies the text should be underlined, but expresses no preference for other text styles.
StyleUnderlineOnly specifies the text should be underlined, and no other styling should apply.

# Structs

AddressProvidesID is a convenience struct that can be embedded in widgets.
App is an implementation of IApp.
AppArgs is a helper struct, providing arguments for the initialization of App.
No description provided by the author
BackgroundColor is an ICellStyler that expresses a specific background color and no preference for foreground color or style.
Callback is a simple implementation of ICallback.
No description provided by the author
No description provided by the author
Canvas is a simple implementation of ICanvas, and is returned by the Render() function of all the current widgets.
CanvasPos is a convenience struct to represent the coordinates of a position on a canvas.
No description provided by the author
Cell represents a single element of terminal output.
ClickCallbacks is a convenience struct for embedding in a widget, providing methods to add and remove callbacks that are executed when the widget is "clicked".
No description provided by the author
ClickTargets is used by the App to keep track of which widgets have been clicked.
Color satisfies IColor, embeds an IColor, and allows a gowid Color to be constructed from a string alone.
No description provided by the author
ColorInverter implements ICellStyler, and simply swaps foreground and background colors.
No description provided by the author
ContainerWidget is a simple implementation that conforms to IContainerWidget.
No description provided by the author
No description provided by the author
No description provided by the author
DefaultColor implements IColor and means use whatever the default terminal color is.
No description provided by the author
No description provided by the author
No description provided by the author
EmptyPalette implements ICellStyler and returns no preference for any colors or styling.
FocusCallbacks is a convenience struct for embedding in a widget, providing methods to add and remove callbacks that are executed when the widget's focus widget changes.
No description provided by the author
No description provided by the author
ForegroundColor is an ICellStyler that expresses a specific foreground color and no preference for background color or style.
GrayColor is an IColor that represents a greyscale specified by the same syntax as urwid - http://urwid.org/manual/displayattributes.html and search for "gray scale entries".
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
IsSelectable is a convenience struct that can be embedded in widgets.
Key is a trivial representation of a keypress, a subset of tcell.Key.
KeyPressCallbacks is a convenience struct for embedding in a widget, providing methods to add and remove callbacks that are executed when the widget is "clicked".
No description provided by the author
No description provided by the author
LineCopy is an argument provided to some Canvas APIs, like Line().
LineResult is returned by some Canvas Line-accessing APIs.
No description provided by the author
No description provided by the author
NoColor implements IColor, and represents "no color preference", distinct from the default terminal color, white, black, etc.
NotSelectable is a convenience struct that can be embedded in widgets.
PaletteEntry is typically used by a gowid application to represent a set of color and style preferences for use by different application widgets e.g.
PaletteRef is intended to represent a PaletteEntry, looked up by name.
RejectUserInput is a convenience struct that can be embedded in widgets that don't accept any user input.
RenderBox is an object passed to a widget's Render function that specifies that it should be rendered with a set number of columns and rows.
RenderFixed is an object passed to a widget's Render function that specifies that the widget itself will determine its own size.
RenderFlow is used by widgets that embed an inner widget, like hpadding.Widget.
RenderFlowWith is an object passed to a widget's Render function that specifies that it should be rendered with a set number of columns, but using as many rows as the widget itself determines it needs.
RenderMax is used in widgets laid out side-by-side - it's intended to have the effect that these widgets are rendered last and provided a height/width that corresponds to the max of the height/width of those widgets already rendered.
RenderWithRatio is used by widgets within a container.
RenderWithUnits is used by widgets within a container.
No description provided by the author
RGBColor allows for use of colors specified as three components, each with values from 0x0 to 0xf.
Three states - false+false, false+true, true+true.
StyleAttrs allows the user to represent a set of styles, either affirmatively set (on) or unset (off) with the rest of the styles being unspecified, meaning they can be determined by styles layered "underneath".
StyledAs is an ICellStyler that expresses a specific text style and no preference for foreground and background color.
StyleMod implements ICellStyler.
SubWidgetCallbacks is a convenience struct for embedding in a widget, providing methods to add and remove callbacks that are executed when the widget's child is modified.
No description provided by the author
SubWidgetsCallbacks is a convenience struct for embedding in a widget, providing methods to add and remove callbacks that are executed when the widget's children are modified.
No description provided by the author
TCellColor is an IColor using tcell's color primitives.
Unit is a one-valued type used to send a message over a channel.
UrwidColor is a gowid Color implementing IColor and which allows urwid color names to be used (http://urwid.org/manual/displayattributes.html#foreground-and-background-settings) e.g.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
WidgetCallback is a simple struct with a name field for IIdentity and that embeds a WidgetChangedFunction to be issued as a callback when a widget property changes.
WidgetCallbackExt is a simple struct with a name field for IIdentity and that embeds a WidgetChangedFunction to be issued as a callback when a widget property changes.
No description provided by the author
No description provided by the author

# Interfaces

IAfterRenderEvent is implemented by clients that wish to run a function on the gowid rendering goroutine, directly after the widget hierarchy is rendered.
IApp is the interface of the application passed to every widget during Render or UserInput.
No description provided by the author
No description provided by the author
No description provided by the author
ICallback represents any object that can provide a way to be compared to others, and that can be called with an arbitrary number of arguments returning no result.
No description provided by the author
No description provided by the author
ICanvas is the interface of any object which can generate a 2-dimensional array of Cells that are intended to be rendered on a terminal.
ICanvasCellReader can provide a Cell given a row and a column.
ICanvasLineReader can provide a particular line of Cells, at the specified y offset.
ICanvasMarkIterator will call the supplied function argument with the name and position of every mark set on the canvas.
ICellProcessor is a general interface used by several gowid types for processing a range of Cell types.
ICellStyler is an analog to urwid's AttrSpec (http://urwid.org/reference/attrspec.html).
No description provided by the author
IClickable is implemented by any type that implements a Click() method, intended to be run in response to a user interaction with the type such as left mouse click or hitting enter.
IClickableWidget is implemented by any widget that implements a Click() method, intended to be run in response to a user interaction with the widget such as left mouse click or hitting enter.
IClickTracker is implemented by any type that can track the state of whether it was clicked.
No description provided by the author
IClipboardSelected is implemented by widgets that support changing their look when they have been "selected" in some application-level copy-mode, the idea being to provide the user with the information that this widget's contents will be copied.
IColor is implemented by any object that can turn itself into a TCellColor, meaning a color with which a cell can be rendered.
IColorMode provides access to a ColorMode value which represents the current mode of the terminal e.g.
No description provided by the author
IComposite is an interface for anything that has a concept of a single "inner" widget.
ICompositeMultiple is an interface for widget containers that have multiple children and that support specifying how the children are laid out relative to each other.
ICompositeMultipleDimensions is an interface for collections of widget dimensions, used in laying out some container widgets.
No description provided by the author
ICompositeMultipleWidget is a widget that implements ICompositeMultiple.
ICompositeWidget is an interface implemented by widgets that contain one subwidget.
IContainerWidget is the type of an object that contains a widget and a render object that determines how it is rendered within a container of widgets.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
IFindNextSelectable is for any object that can iterate to its next or previous object.
IFocus is a container widget concept that describes which widget will be the target of keyboard input.
No description provided by the author
No description provided by the author
IGetScreen provides access to a tcell.Screen object e.g.
No description provided by the author
IIdentity is used for widgets that support being a click target - so it is possible to link the widget that is the target of MouseReleased with the one that was the target of MouseLeft/Right/Middle when they might not necessarily be the same object (i.e.
IIdentityWidget is implemented by any widget that provides an ID() function that identifies itself and allows itself to be compared against other IIdentity implementers.
IKey represents a keypress.
IKeyPress is implemented by any type that implements a KeyPress() method, intended to be run in response to a user interaction with the type such as hitting the escape key.
IMenuCompatible is implemented by any widget that can set a subwidget.
No description provided by the author
IPalette provides application "palette" information - it can look up a Cell styling interface by name (e.g.
IPreferedPosition is implemented by any widget that supports a prefered column or row (position in a dimension), meaning it understands what subwidget is at the current dimensional coordinate, and can move its focus widget to a new position.
No description provided by the author
No description provided by the author
IRenderContext proviees palette and color mode information.
No description provided by the author
No description provided by the author
No description provided by the author
Used in widgets laid out side-by-side - intended to have the effect that these widgets are rendered last and provided a height that corresponds to the max of the height of those widgets already rendered.
Used in widgets laid out side-by-side - intended to limit the width of a widget which may otherwise be specified to be dimensioned in relation to the width available.
No description provided by the author
IRenderSize is the type of objects that can specify how a widget is to be rendered.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ISelectChild is implemented by any type that controls whether or not it will set focus.Selected on its currently "selected" child.
IComposite is an interface for anything that has a concept of a single settable "inner" widget.
ISettableDimensions is implemented by types that maintain a collection of dimensions - to be used by containers that use these dimensions to layout their children widgets.
ISettableSubWidgetsis implemented by a type that maintains a collection of child widgets (like pile, columns) and that allows them to be changed.
ISubWidgetSize returns the size argument that should be provided to render the inner widget based on the size argument provided to the containing widget.
IUnhandledInput is used as a handler for application user input that is not handled by any widget in the widget hierarchy.
No description provided by the author
IWidget is the interface of any object acting as a gowid widget.
IWidgetChangedCallback defines the types that can be used as callbacks that are issued when widget properties change.
Widgets that are used in containers such as Pile or Columns must implement this interface.

# Type aliases

No description provided by the author
No description provided by the author
CellRangeFunc is an adaptor for a simple function to implement ICellProcessor.
ColorMode represents the color capability of a terminal.
No description provided by the author
No description provided by the author
LineCanvas exists to make an array of Cells conform to some interfaces, specifically IRenderBox (it has a width of len(.) and a height of 1), IAppendCanvas, to allow an array of Cells to be passed to the canvas function AppendLine(), and ICanvasLineReader so that an array of Cells can act as a line returned from a canvas.
Palette implements IPalette and is a trivial implementation of a type that can store cell stylers and provide access to them via iteration.
No description provided by the author
No description provided by the author
No description provided by the author
UnhandledInputFunc satisfies IUnhandledInput, allowing use of a simple function for handling input not claimed by any widget.
WidgetChangedFunction meets the IWidgetChangedCallback interface, for simpler usage.
No description provided by the author
No description provided by the author