terminal

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package terminal provides low-level terminal state management.

It wraps golang.org/x/term to offer a clean lifecycle for raw mode, terminal size queries, and SIGWINCH resize handling. All terminal operations go through a single Terminal instance so that the rest of the TUI never touches raw ANSI escapes or os.Stdin.Fd() directly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Terminal

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

Terminal manages the lifecycle of the controlling terminal.

It owns the file descriptor and the saved cooked-mode state, and provides goroutine-safe access to width/height. A single Terminal should be created at TUI startup and its [Close] method deferred.

func New

func New(f *os.File) *Terminal

New creates a Terminal for the given file (typically os.Stdin). It does NOT enter raw mode — call Terminal.EnterRawMode explicitly.

func (*Terminal) Close

func (t *Terminal) Close()

Close is an alias for [Restore], suitable for use with defer.

func (*Terminal) EnterRawMode

func (t *Terminal) EnterRawMode() error

EnterRawMode switches the terminal to raw mode and saves the original state for later restoration. It is safe to call multiple times; only the first call takes effect.

func (*Terminal) Fd

func (t *Terminal) Fd() int

Fd returns the underlying file descriptor.

func (*Terminal) Height

func (t *Terminal) Height() int

Height is a convenience helper that returns only the row count.

func (*Terminal) IsRaw

func (t *Terminal) IsRaw() bool

IsRaw reports whether the terminal is currently in raw mode.

func (*Terminal) ListenResize

func (t *Terminal) ListenResize() (stop func())

ListenResize starts a goroutine that listens for SIGWINCH and refreshes the cached terminal size. Call the returned stop function to clean up the signal channel.

Typical usage:

stop := t.ListenResize()
defer stop()

func (*Terminal) OnResize

func (t *Terminal) OnResize(fn func(width, height int))

OnResize registers a callback that fires whenever the terminal size changes. Only one callback is supported — subsequent calls overwrite the previous one.

func (*Terminal) Restore

func (t *Terminal) Restore()

Restore returns the terminal to its original (cooked) mode. It is safe to call multiple times or on an already-restored terminal.

func (*Terminal) Size

func (t *Terminal) Size() (width, height int)

Size returns the current terminal width and height in columns/rows. The values are cached and updated on SIGWINCH.

func (*Terminal) Width

func (t *Terminal) Width() int

Width is a convenience helper that returns only the column count.

Jump to

Keyboard shortcuts

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