10 Termbox API
Jakub Sobon edited this page 2020-11-14 01:24:14 -05:00

Doc Status

The termbox package implements the Terminal API using the termbox-go. Termbox provides cell based access to the terminal.

Prefer to use Tcell API instead, nsf/termbox-go is no longer maintained.

The public API surface of this package consists of the following:

termbox.New

Users of Termdash need to provide Termdash API with a terminal instance. Termbox is one of the valid options. Use the New function to create a Terminal instance.

tb, err := termbox.New()
if err != nil {
  return fmt.Errorf("termbox.New => %v", err)
}

termbox.Option

This interface is used to provide optional arguments to New.

termbox.ColorMode

Used to set the color mode in which the terminal will be initialised. The available color modes are documented in the Terminal API. The following example sets the color mode to grayscale. Note that when no options are provided, Termbox is initialised in a color mode that supports all 256 terminal colors. Refer to the Cell API for an explanation of terminal colors.

tb, err := termbox.New(termbox.ColorMode(terminalapi.ColorModeGrayscale))
if err != nil {
  return fmt.Errorf("termbox.New => %v", err)
}

termbox.Close

This function should be used to close the terminal and return it to a sane state once the Termbox instance isn't needed anymore. A good practice is to defer the call right after Termbox instance is created:

tb, err := termbox.New()
if err != nil {
  return fmt.Errorf("termbox.New => %v", err)
}
defer tb.Close()