termui

package
v0.0.0-...-deee935 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2024 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package termui implements drawing and ui elements over drivers. See TerminalDriver.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTheme = Theme{
	Normal: StyleDefault.
		Background(ColorBlack).
		Foreground(ColorWhite),
	Highlight: StyleDefault.
		Background(ColorNavy).
		Foreground(ColorWhite),
	Error: StyleDefault.
		Background(ColorRed).
		Foreground(ColorWhite),
}

The default theme

View Source
var ErrorQuit = errors.New("quit")

Error that indicates we just want to quit the Mode function

View Source
var Palette = []color.Color{
	color.RGBA{R: 0, G: 0, B: 0, A: 255},
	color.RGBA{R: 170, G: 0, B: 0, A: 255},
	color.RGBA{R: 0, G: 170, B: 0, A: 255},
	color.RGBA{R: 170, G: 85, B: 0, A: 255},
	color.RGBA{R: 0, G: 0, B: 170, A: 255},
	color.RGBA{R: 170, G: 0, B: 170, A: 255},
	color.RGBA{R: 0, G: 170, B: 170, A: 255},
	color.RGBA{R: 170, G: 170, B: 170, A: 255},
	color.RGBA{R: 85, G: 85, B: 85, A: 255},
	color.RGBA{R: 255, G: 85, B: 85, A: 255},
	color.RGBA{R: 85, G: 255, B: 85, A: 255},
	color.RGBA{R: 255, G: 255, B: 85, A: 255},
	color.RGBA{R: 85, G: 85, B: 255, A: 255},
	color.RGBA{R: 255, G: 85, B: 255, A: 255},
	color.RGBA{R: 85, G: 255, B: 255, A: 255},
	color.RGBA{R: 255, G: 255, B: 255, A: 255},
}

Built-in palette

View Source
var StyleDefault = Style(0).Foreground(ColorWhite).Background(ColorBlack)

Default terminal style

Functions

func DrawBox

func DrawBox(s TerminalDriver, b util.Rect, style Style)

DrawBox draws a box.

func DrawClear

func DrawClear(s TerminalDriver)

DrawClear clears the screen.

func DrawFill

func DrawFill(s TerminalDriver, b util.Rect, g Glyph)

DrawFill fills a region of the screen.

func DrawHLine

func DrawHLine(s TerminalDriver, p util.Point, w int, style Style)

DrawHLine draws a horizontal line.

func DrawStringCenter

func DrawStringCenter(s TerminalDriver, b util.Rect, text string, style Style)

DrawStringCenter draws a string centered.

func DrawStringLeft

func DrawStringLeft(s TerminalDriver, b util.Rect, t string, style Style)

DrawStringLeft draws a string left-justified.

func DrawStringRight

func DrawStringRight(s TerminalDriver, b util.Rect, text string, style Style)

DrawStringRight draws a string right-justified.

func DrawVLine

func DrawVLine(s TerminalDriver, p util.Point, h int, style Style)

DrawVLine draws a vertical line.

func RunMode

func RunMode(s TerminalDriver, m Mode)

RunMode runs the given mode.

Types

type Color

type Color uint8

Color is a wrapper value for tcell.Color.

const (
	ColorBlack Color = iota
	ColorMaroon
	ColorGreen
	ColorOlive
	ColorNavy
	ColorPurple
	ColorTeal
	ColorSilver
	ColorGray
	ColorRed
	ColorLime
	ColorYellow
	ColorBlue
	ColorFuchsia
	ColorAqua
	ColorWhite
)

func (Color) MarshalJSON

func (c Color) MarshalJSON() ([]byte, error)

func (*Color) UnmarshalJSON

func (c *Color) UnmarshalJSON(in []byte) error

type EventKey

type EventKey struct {
	Key rune
}

type EventQuit

type EventQuit struct{}

type EventResize

type EventResize struct {
	Size util.Point
}

type Glyph

type Glyph struct {
	Rune  rune  // Rune to display
	Style Style // Display style
}

Glyph represents the content of one cell in the terminal.

type List

type List struct {
	Bounds     util.Rect                       // Bounds of the rect on screen
	CursorPos  int                             // Current cursor position
	Boxed      bool                            // If true a box is drawn around the list
	Title      string                          // Title for the box, if any
	Items      []string                        // Items of the list
	Selected   func(TerminalDriver, int) error // The function that is called if the user selects an item
	HideCursor bool                            // If true we will not highlight the cursor position
}

List implements a mode that presents a scrollable list to the user.

func (*List) CurrentSelection

func (m *List) CurrentSelection() string

CurrentSelection returns the string under the current cursor position.

func (*List) Draw

func (m *List) Draw(s TerminalDriver)

Draw implements the Mode interface.

func (*List) HandleEvent

func (m *List) HandleEvent(s TerminalDriver, e any) error

HandleEvent implements the Mode interface.

type Mode

type Mode interface {
	// HandleEvent is responsible for updating the mode's state in response to
	// events. If ErrorQuit is returned the mode is exited. Any other error is
	// treated as fatal.
	HandleEvent(TerminalDriver, any) error
	// Draw is responsible for drawing the mode to the terminal driver.
	Draw(TerminalDriver)
}

Mode is the interface all objects implementing a game mode must implement.

type Style

type Style uint16

Style defines the appearance of a glyph.

func (Style) Background

func (s Style) Background(c Color) Style

Background returns the style with the background set.

func (Style) Decompose

func (s Style) Decompose() (fg, bg Color)

Decompose returns the colors of the style.

func (Style) Foreground

func (s Style) Foreground(c Color) Style

Foreground returns the style with the foreground set.

type TerminalDriver

type TerminalDriver interface {
	// Init is responsible for all initialization operations.
	Init() error
	// Fini is responsible for all cleanup operations.
	Fini()
	// Quit forces the driver to exit.
	Quit()
	// SetCell sets a single cell of the screen.
	SetCell(util.Point, Glyph)
	// GetCell returns the glyph at the given point on the screen.
	GetCell(util.Point) Glyph
	// PollEvent returns the next event.
	PollEvent() any
	// FlushEvents discards all non-system events currently in the buffer.
	FlushEvents()
	// Size returns the size of the screen.
	Size() (int, int)
	// Sync must redraw the entire terminal display.
	Sync()
	// Show must redraw the dirty areas of the terminal display.
	Show()
}

TerminalDriver is the interface terminal front-end implementations must implement to use termui.

type TextBox

type TextBox struct {
	Bounds util.Rect // Fixed bounds of the text box display, line length is inferred from this
	Boxed  bool      // If true a box will be rendered around the text box
	Title  string    // Title to display if any, only valid if Boxed is true
	// contains filtered or unexported fields
}

TextBox implements a word-wrapped text box that displays a single string.

func (*TextBox) Draw

func (m *TextBox) Draw(s TerminalDriver)

Draw implements the Mode interface.

func (*TextBox) HandleEvent

func (m *TextBox) HandleEvent(s TerminalDriver, e any) error

HandleEvent implements the Mode interface.

func (*TextBox) SetText

func (m *TextBox) SetText(s string) int

SetText sets the text that the text box will display and returns the number of lines the string was broken into.

type Theme

type Theme struct {
	Normal    Style // Normal text
	Highlight Style // Highlighted text
	Error     Style // Error messages
}

Theme defines the colors used for various standard things.

var CurrentTheme Theme = DefaultTheme

The current theme

Jump to

Keyboard shortcuts

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