term

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AttrVerticalRenderOffset instructs the renderer to render
	// the cell with an offset equal to half the height of the cell.
	AttrVerticalRenderOffset tcell.AttrMask = tcell.AttrInvalid << 1
	// AttrNegativeVerticalRenderOffset instructs the renderer to render
	// the cell with an offset equal to minus half of the height of the cell.
	AttrNegativeVerticalRenderOffset tcell.AttrMask = tcell.AttrInvalid << 2
)
View Source
const (
	CursorStyleDefault           = CursorStyle(tcell.CursorStyleDefault)
	CursorStyleBlinkingBlock     = CursorStyle(tcell.CursorStyleBlinkingBlock)
	CursorStyleSteadyBlock       = CursorStyle(tcell.CursorStyleSteadyBlock)
	CursorStyleBlinkingUnderline = CursorStyle(tcell.CursorStyleBlinkingUnderline)
	CursorStyleSteadyUnderline   = CursorStyle(tcell.CursorStyleSteadyUnderline)
	CursorStyleBlinkingBar       = CursorStyle(tcell.CursorStyleBlinkingBar)
	CursorStyleSteadyBar         = CursorStyle(tcell.CursorStyleSteadyBar)
)

List of cursor styles.

Event type. See Event.Type field.

View Source
const (
	KeyF1          Key = Key(termbox.KeyF1)
	KeyF2              = Key(termbox.KeyF2)
	KeyF3              = Key(termbox.KeyF3)
	KeyF4              = Key(termbox.KeyF4)
	KeyF5              = Key(termbox.KeyF5)
	KeyF6              = Key(termbox.KeyF6)
	KeyF7              = Key(termbox.KeyF7)
	KeyF8              = Key(termbox.KeyF8)
	KeyF9              = Key(termbox.KeyF9)
	KeyF10             = Key(termbox.KeyF10)
	KeyF11             = Key(termbox.KeyF11)
	KeyF12             = Key(termbox.KeyF12)
	KeyInsert          = Key(termbox.KeyInsert)
	KeyDelete          = Key(termbox.KeyDelete)
	KeyHome            = Key(termbox.KeyHome)
	KeyEnd             = Key(termbox.KeyEnd)
	KeyPgup            = Key(termbox.KeyPgup)
	KeyPgdn            = Key(termbox.KeyPgdn)
	KeyArrowUp         = Key(termbox.KeyArrowUp)
	KeyArrowDown       = Key(termbox.KeyArrowDown)
	KeyArrowLeft       = Key(termbox.KeyArrowLeft)
	KeyArrowRight      = Key(termbox.KeyArrowRight)
	MouseLeft          = Key(termbox.MouseLeft)
	MouseMiddle        = Key(termbox.MouseMiddle)
	MouseRight         = Key(termbox.MouseRight)
	MouseRelease       = Key(termbox.MouseRelease)
	MouseWheelUp       = Key(termbox.MouseWheelUp)
	MouseWheelDown     = Key(termbox.MouseWheelDown)
	KeyBackspace       = Key(termbox.KeyBackspace2)
	KeyTab             = Key(termbox.KeyTab)
	KeyEnter           = Key(termbox.KeyEnter)
	KeyEsc             = Key(termbox.KeyEsc)
	KeySpace           = Key(termbox.KeySpace)
)

List of keys.

Input mode. See SetInputMode function.

View Source
const (
	ModAlt Modifier = 1 << iota
	ModShift
	ModMeta
	ModCtrl

	ModCtrlShift     = ModShift | ModCtrl
	ModCtrlAlt       = ModCtrl | ModAlt
	ModCtrlMeta      = ModCtrl | ModMeta
	ModCtrlShiftAlt  = ModShift | ModAlt | ModCtrl
	ModCtrlShiftMeta = ModCtrl | ModShift | ModMeta
	ModCtrlAltMeta   = ModCtrl | ModAlt | ModMeta
	ModShiftMeta     = ModShift | ModMeta
	ModAltMeta       = ModAlt | ModMeta
	ModAltShiftMeta  = ModAlt | ModShift | ModMeta
	ModAltShift      = ModAlt | ModShift
)

Alt modifier constant, see Event.Mod field and SetInputMode function.

Variables

This section is empty.

Functions

func CalculateOptimalWidth added in v0.0.2

func CalculateOptimalWidth(cells [][]Cell) (max int)

CalculateOptimalWidth calculates the width of this cells, such that nothing is truncated if rendered.

func CellsToBytesBuffer added in v0.0.2

func CellsToBytesBuffer(buffer *bytes.Buffer, cells [][]Cell)

CellsToBytesBuffer copies the bytes representation of the given cell matrix to the supplied buffer.

Caller is responsible for resetting buffer prior to this call if necessary.

func CellsToString added in v0.0.2

func CellsToString(cells [][]Cell) string

CellsToString returns the string representation of the given cell matrix.

func CellsToStringBuilder added in v0.0.2

func CellsToStringBuilder(builder *strings.Builder, cells [][]Cell)

CellsToStringBuilder copies the string representation of the given cell matrix to the supplied builder.

Caller is responsible for resetting builder prior to this call if necessary.

func CloneCells added in v0.0.2

func CloneCells(in [][]Cell) [][]Cell

CloneCells returns a deep clone of in.

func Close

func Close()

Close writer; should be called after successful initialization when termbox's functionality isn't required anymore.

func ContextWithPayload

func ContextWithPayload(ctx context.Context, payload []byte) context.Context

ContextWithPayload returns a new Context that holds locker.

func CoordinatesBlockSort

func CoordinatesBlockSort(from Coordinates, to Coordinates) (
	Coordinates, Coordinates,
)

CoordinatesBlockSort sorts a pair of coordinates (from/to) such that:

	┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐
	│ f    ││ t    ││    f ││    t ││ t  f ││ f  t │
	│    t ││    f ││ t    ││ f    ││      ││      │
	└──────┘└──────┘└──────┘└──────┘└──────┘└──────┘
      |       |        |       |       |       |
      v       v        v       v       v       v
	┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐
	│ f    ││ f    ││ f    ││ f    ││ f  t ││ f  t │
	│    t ││    t ││    t ││    t ││      ││      │
	└──────┘└──────┘└──────┘└──────┘└──────┘└──────┘

func CoordinatesInBounds

func CoordinatesInBounds(pos Coordinates, bounds Coordinates) bool

CoordinatesInBounds returns true if the given position is within the given right-exclusive bounds.

func CoordinatesIntersection

func CoordinatesIntersection(
	startA, endA, startB, endB Coordinates,
) (intersectionStart Coordinates, intersectionEnd Coordinates, ok bool)

CoordinatesIntersection calculates the intersection a ∩ b in a 2D space, defined as the set of all those cells which are common to both a and b. Both a and b are expected to be right-exclusive ranges.

┌──────┐     ┌──────┐     ┌──────┐
│ AA   │  ∩  │      │  =  │      │
│      │     │   BB │     │      │
└──────┘     └──────┘     └──────┘
┌──────┐     ┌──────┐     ┌──────┐
│ AAAAA│  ∩  │      │  =  │      │
│AAA   │     │BBBBB │     │CCC   │
└──────┘     └──────┘     └──────┘
┌──────┐     ┌──────┐     ┌──────┐
│  BBBB│  ∩  │      │  =  │      │
│BBB   │     │AAAAA │     │CCC   │
└──────┘     └──────┘     └──────┘
┌──────┐     ┌──────┐     ┌──────┐
│     A│  ∩  │      │  =  │      │
│AAAAAA│     │BBB   │     │CCC   │
└──────┘     └──────┘     └──────┘

func CoordinatesSort

func CoordinatesSort(from Coordinates, to Coordinates) (
	Coordinates, Coordinates,
)

CoordinatesSort sorts a pair of coordinates (from/to) such that:

	┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐
	│ f    ││ t    ││    f ││    t ││ t  f ││ f  t │
	│    t ││    f ││ t    ││ f    ││      ││      │
	└──────┘└──────┘└──────┘└──────┘└──────┘└──────┘
      |       |        |       |       |       |
      v       v        v       v       v       v
	┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐┌──────┐
	│ f    ││ f    ││    f ││    f ││ f  t ││ f  t │
	│    t ││    t ││ t    ││ t    ││      ││      │
	└──────┘└──────┘└──────┘└──────┘└──────┘└──────┘

func CopyCells added in v0.0.2

func CopyCells(dst [][]Cell, src [][]Cell) [][]Cell

CopyCells copies src into dst, re-using dst's capacity when possible.

func Init

func Init() error

Init initializes the underlying vte client.

func InterruptAt

func InterruptAt(ctx context.Context, interrupter Interrupter, fps int)

InterruptAt interrupts the main event loop at the given fps, using the given interrupter. This function only returns when context is canceled.

func PayloadFromContext

func PayloadFromContext(ctx context.Context) ([]byte, bool)

PayloadFromContext returns the payload value stored in ctx, if any.

func Poll

func Poll() <-chan tcell.Event

Poll gives access to the underlying tcell.Event channel.

func PublishBell

func PublishBell()

PublishBell It's a shorthand for `ScheduleNextTick(RingBell)`.

func PublishEvent

func PublishEvent(ev Event) bool

PublishEvent sends a synthetic event to the event poller. If the event queue is full then this method does not publish the event and returns false.

func RingBell

func RingBell()

RingBell makes an audible noise. This must be synchronized against other accesses to the term.Writer's screen buffer.

func ScheduleNextTick

func ScheduleNextTick(fn func()) bool

ScheduleNextTick schedules running fn on the next event-loop iteration.

func SetAttr

func SetAttr(newattr Attributes)

SetAttr sets the global foreground and background attributes.

func SetCursorStyle

func SetCursorStyle(style CursorStyle)

SetCursorStyle is used to set the cursor style. If the style is not supported (or cursor styles are not supported at all), then this will have no effect.

func Size

func Size() (width int, height int)

Size returns the size of the terminal window.

func StringToCells added in v0.0.2

func StringToCells(str string) (cells [][]Cell)

StringToCells returns the cell matrix representation of the given string.

Types

type Attributes

type Attributes tcell.Style

Attributes represents a cell background and foreground attributes.

func Attr

func Attr() Attributes

Attr returns the global foreground and background attributes.

func AttributesDifference

func AttributesDifference(a, b Attributes) Attributes

AttributesDifference computes the set difference between a and b, that is it returns a set of attributes that contain all the bit flags set in a but not set in b, and returns ColorDefault if a's color is equal to b's color or returns the color set in a.

func AttributesUnion

func AttributesUnion(a, b Attributes) Attributes

AttributesUnion computes the set union between a and b, that is it returns a set of attributes that contain all the bit flags set in a, b or both, and uses the color defined in b or if not set, uses the color in a.

type Cell

type Cell struct {
	Attributes
	// Ch is the main character held by this cell.
	// If character cannot fit in the storage provided by the
	// builtin 'rune', then Width() returns > 1 and Cell.Combining
	// contains the rest of data.
	Ch rune
	// Combining are the remaining data that does not fit in Ch.
	Combining []rune
	// Width returns the monospace width of this Cell.
	Width uint8
	// Bytes is the number of bytes consumed by this Cell.
	Bytes uint8
}

Cell represents a location with content on a terminal screen. 'Ch' is a unicode character, 'Fg' and 'Bg' are foreground and background attributes respectively. Unicode graphene clusters should be processed accordingly and stored into Ch and Combining fields.

type Coordinates

type Coordinates struct {
	X, Y int
}

Coordinates represent a point in a 2D space.

func CoordinatesDiff

func CoordinatesDiff(a, b Coordinates) Coordinates

CoordinatesDiff subtracts a from b.

func CoordinatesSum

func CoordinatesSum(a, b Coordinates) Coordinates

CoordinatesSum adds a to b.

type CursorStyle

type CursorStyle int

CursorStyle represents a given cursor style, which can include the shape and whether the cursor blinks or is solid. Support for changing this is not universal.

type Event

type Event struct {
	Type     EventType // one of Event* constants
	Mod      Modifier  // one of Mod* constants or 0
	Key      Key       // one of Key* constants, invalid if 'Ch' is not 0
	Ch       rune      // a unicode character
	Width    int       // width of the screen
	Height   int       // height of the screen
	Err      error     // error in case if input failed
	MouseX   int       // x coord of mouse
	MouseY   int       // y coord of mouse
	Raw      []byte
	UserFunc func()
	Context  context.Context
}

Event represents a terminal event. The 'Mod', 'Key' and 'Ch' fields are valid if 'Type' is EventKey. The 'Width' and 'Height' fields are valid if 'Type' is EventResize. The 'Err' field is valid if 'Type' is EventError.

func FromTcellEvent

func FromTcellEvent(tev tcell.Event) Event

FromTcellEvent converts a tcell.Event into a term.Event.

func PollEvent

func PollEvent() (ev Event)

PollEvent waits for an event and returns it. This is a blocking function call.

func (Event) KeyComb added in v0.0.2

func (e Event) KeyComb() KeyComb

KeyComb returns this event as a KeyComb, or panics if this event is not of type EventKey.

type EventType

type EventType uint8

EventType is the type of event being dispatched.

type InputMode

type InputMode int

InputMode is the keyboard input mode.

func SetInputMode

func SetInputMode(mode InputMode) InputMode

SetInputMode sets termbox input mode. Termbox has two input modes:

1. Esc input mode. When ESC sequence is in the buffer and it doesn't match any known sequence. ESC means KeyEsc. This is the default input mode.

2. Alt input mode. When ESC sequence is in the buffer and it doesn't match any known sequence. ESC enables ModAlt modifier for the next keyboard event.

Both input modes can be OR'ed with Mouse mode. Setting Mouse mode bit up will enable mouse button press/release and drag events.

If 'mode' is InputCurrent, returns the current input mode. See also Input* constants.

type Interrupter

type Interrupter interface {
	Interrupt(context.Context) error
}

Interrupter wraps the basic method Interrupt, which sends an interrupt event to the main loop, forcing a redraw of all components.

The given context is piped back into the next loop iteration so callers can use it to distinguish between an interrupt-driven call to Draw or just the next tick.

func FuncInterrupter

func FuncInterrupter(fn func(context.Context) error) Interrupter

FuncInterrupter returns an Interrupter that calls fn every time Interrupt is called.

func NopInterrupter

func NopInterrupter() Interrupter

NopInterrupter is an interrupter that does nothing when Interrupt is called.

type Key

type Key uint16

Key is a keyboard key.

type KeyComb added in v0.0.2

type KeyComb struct {
	Mod Modifier
	Key Key
	Ch  rune
}

KeyComb represents is a key combination. See event for more details.

func ParseKey added in v0.0.3

func ParseKey(str string) (KeyComb, error)

ParseKey parses str into a KeyComb or returns error if it fails to parse it.

func ParseKeys added in v0.0.3

func ParseKeys(sequence string) (ret []KeyComb, err error)

ParseKeys parses the given sequence of key combinations.

func (KeyComb) ShortString added in v0.0.3

func (k KeyComb) ShortString() string

ShortString returns the shorthand of the long string representation of this KeyComb.

func (KeyComb) String added in v0.0.3

func (k KeyComb) String() string

String returns the long string representation of this KeyComb.

type Modifier

type Modifier uint8

Modifier is a key modifier like <alt> or <ctrl>.

type NoopWriter

type NoopWriter struct{}

NoopWriter is a Writer that does nothing.

func (NoopWriter) Clear

func (w NoopWriter) Clear(Attributes) (err error)

func (NoopWriter) Context

func (w NoopWriter) Context() context.Context

func (NoopWriter) Flush

func (w NoopWriter) Flush() (err error)

func (NoopWriter) SetCell

func (w NoopWriter) SetCell(pos Coordinates, cell Cell)

func (NoopWriter) SetCursor

func (w NoopWriter) SetCursor(pos Coordinates)

func (NoopWriter) UnionAttributes

func (w NoopWriter) UnionAttributes(pos Coordinates, attr Attributes)

type Range

type Range struct {
	Start Coordinates
	End   Coordinates
}

Range is represents a selection in a 2D space.

func MergeRanges

func MergeRanges(ranges []Range) []Range

MergeRanges combines the given range slice, such that it returns the smallest set of ranges that is equivalent to the given set of ranges, by merging all intersecting ranges.

type StringWriter

type StringWriter struct {
	CursorCh     rune
	SetContext   context.Context
	BackgroundCh rune
	ForegroundCh rune
	// contains filtered or unexported fields
}

StringWriter satisfies Writer by rendering the cells into a plain string.

func NewStringWriter

func NewStringWriter(width, height int) (t *StringWriter)

NewStringWriter allocates storage for a new StringWriter and initializes it.

func (*StringWriter) Cells

func (w *StringWriter) Cells() []Cell

Cells returns the internal cell slice.

func (*StringWriter) Clear

func (w *StringWriter) Clear(attr Attributes) (err error)

Clear satisfies Writer. Note that attr are ignored as they can't be represented in a string.

func (*StringWriter) Context

func (w *StringWriter) Context() context.Context

Context returns context.Background

func (*StringWriter) Flush

func (w *StringWriter) Flush() (err error)

Flush flushes the contents of this writer into the underlying cell buffer.

func (*StringWriter) Init

func (w *StringWriter) Init(width, height int)

Init initializes this StringWriter with the given height and width.

func (*StringWriter) Reset

func (w *StringWriter) Reset()

Reset resets this writer.

func (*StringWriter) Resize

func (w *StringWriter) Resize(width, height int)

Resize satisfies Writer.

func (*StringWriter) SetCell

func (w *StringWriter) SetCell(pos Coordinates, cell Cell)

SetCell satisfies Writer.

func (*StringWriter) SetCursor

func (w *StringWriter) SetCursor(pos Coordinates)

SetCursor satisfies Writer by substituting the rune at pos for a pre-defined cursor-like rune.

func (*StringWriter) String

func (w *StringWriter) String() string

String returns the string representation of the contents of this Writer.

func (*StringWriter) UnionAttributes

func (w *StringWriter) UnionAttributes(pos Coordinates, attr Attributes)

UnionAttributes satisfies Writer.

type TermboxWriter

type TermboxWriter struct {
	// contains filtered or unexported fields
}

TermboxWriter implements a termbox-like API using tcell/v3.

var (

	// DefaultWriter returns the default global Writer.
	DefaultWriter *TermboxWriter = NewTermboxWriter()
)

func NewTermboxWriter

func NewTermboxWriter() *TermboxWriter

NewTermboxWriter allocates storage for a new TermboxWriter and initializes it.

func (*TermboxWriter) Clear

func (w *TermboxWriter) Clear(attr Attributes) (err error)

Clear fills the screen with the given attributs and empty cells.

func (*TermboxWriter) Context

func (w *TermboxWriter) Context() context.Context

Context satisfies term.Writer.

func (*TermboxWriter) Flush

func (w *TermboxWriter) Flush() error

Flush makes all the content changes made using SetCell and UnionAttributes visible on the display.

func (*TermboxWriter) SetCell

func (w *TermboxWriter) SetCell(pos Coordinates, c Cell)

SetCell satisfies term.Writer.

func (*TermboxWriter) SetContext

func (w *TermboxWriter) SetContext(ctx context.Context)

SetContext sets the context for the next call to Context.

func (*TermboxWriter) SetCursor

func (w *TermboxWriter) SetCursor(pos Coordinates)

SetCursor displays the terminal cursor at the given location.

func (*TermboxWriter) UnionAttributes

func (w *TermboxWriter) UnionAttributes(pos Coordinates, attr Attributes)

UnionAttributes satisfies term.Writer.

type Writer

type Writer interface {
	// Context returns the current context of the Writer.
	// This context can be used by tui.Components in combination
	// with term.Interrupter.Interrupt(context.Context) to disambiguate
	// regular calls to Draw from interrupt-driven calls to Draw.
	Context() context.Context
	// SetCell sets the contents of the given cell location.  If
	// the coordinates are out of range, then the operation is ignored.
	SetCell(Coordinates, Cell)
	// UnionAttributes computes the set union between a and b,
	// that is overrides a set of attributes at the given coordinates
	// that contain all the bit flags set in a, b or both, and uses the color
	// defined in b or if not set, uses the color in a.
	UnionAttributes(Coordinates, Attributes)
}

Writer abstracts termbox write functionality to decouple components from termbox, so they're easier to test.

func BoundsCheckWriter added in v0.0.2

func BoundsCheckWriter(width, height int, w Writer) Writer

BoundsCheckWriter returns a Writer which wraps w to make sure that calls to SetCell and SetCursor will never be out of the bounds defined by height or width.

Directories

Path Synopsis
Package graphemecluster is a wrapper around uniseg to support non-standard characters that are otherwise not supported, like the ones found in nerd fonts.
Package graphemecluster is a wrapper around uniseg to support non-standard characters that are otherwise not supported, like the ones found in nerd fonts.

Jump to

Keyboard shortcuts

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