term

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NewlineReturn = "\r\n"

	ClearLineAfter   = "\x1b[0K"
	ClearLineBefore  = "\x1b[1K"
	ClearLine        = "\x1b[2K"
	ClearScreenBelow = "\x1b[0J"
	ClearScreen      = "\x1b[2J" // Clears screen, preserving scroll buffer
	ClearDisplay     = "\x1b[3J" // Clears screen fully, wipes the scroll buffer

	CursorTopLeft    = "\x1b[H"
	SaveCursorPos    = "\x1b7"
	RestoreCursorPos = "\x1b8"
	HideCursor       = "\x1b[?25l"
	ShowCursor       = "\x1b[?25h"

	BracketedPasteStart = "\x1b[?2004h"
	BracketedPasteEnd   = "\x1b[?2004l"
)

Terminal control sequences.

Variables

View Source
var (
	ArrowUp    = string([]byte{27, 91, 65}) // ^[[A
	ArrowDown  = string([]byte{27, 91, 66}) // ^[[B
	ArrowRight = string([]byte{27, 91, 67}) // ^[[C
	ArrowLeft  = string([]byte{27, 91, 68}) // ^[[D
)

Some core keys needed by some stuff.

Functions

func BeginBuffer added in v1.2.0

func BeginBuffer()

BeginBuffer starts buffering terminal writes. Calls nest: only the outermost BeginBuffer/EndBuffer pair allocates and flushes the buffer, so render entrypoints can be wrapped independently without coordinating.

func DisableBracketedPaste added in v1.1.4

func DisableBracketedPaste()

DisableBracketedPaste disables bracketed paste mode.

func EnableBracketedPaste added in v1.1.4

func EnableBracketedPaste()

EnableBracketedPaste enables bracketed paste mode.

func EndBuffer added in v1.2.0

func EndBuffer()

EndBuffer flushes and tears down the buffer when leaving the outermost frame. It is safe to call unmatched (it no-ops at depth 0), so it can be deferred even if BeginBuffer is on an early-return path.

func FlushBuffer added in v1.2.0

func FlushBuffer()

FlushBuffer pushes everything buffered so far to the terminal without ending the frame. It must be called before any operation that READS the terminal and depends on prior output being on screen — chiefly an ESC[6n cursor-position query (see GetCursorPos): otherwise the prompt would still sit in the buffer and the reported cursor position would be wrong.

func GetLength

func GetLength() int

GetLength returns the length of the terminal (Y length), or 80 if it cannot be established.

func GetSize

func GetSize(fd int) (width, height int, err error)

GetSize returns the dimensions of the given terminal.

func GetWidth

func GetWidth() (termWidth int)

GetWidth returns the width of the terminal or 80 if it cannot be established.

func IsTerminal

func IsTerminal(fd int) bool

IsTerminal returns true if the given file descriptor is a terminal.

func MoveCursorBackwards

func MoveCursorBackwards(i int)

MoveCursorBackwards moves the cursor backward i columns.

func MoveCursorDown

func MoveCursorDown(i int)

MoveCursorDown moves the cursor down i lines.

func MoveCursorForwards

func MoveCursorForwards(i int)

MoveCursorForwards moves the cursor forward i columns.

func MoveCursorUp

func MoveCursorUp(i int)

MoveCursorUp moves the cursor up i lines.

func Printf added in v1.2.0

func Printf(format string, a ...interface{})

Printf formats and writes to the terminal, honouring the frame buffer.

func Restore

func Restore(fd int, state *State) error

Restore restores the terminal connected to the given file descriptor to a previous state.

func WriteString added in v1.2.0

func WriteString(s string)

WriteString writes s to the terminal: into the frame buffer when one is active, otherwise straight to the output. Empty strings are dropped so callers need not guard.

Types

type State

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

State contains the state of a terminal.

func GetState

func GetState(fd int) (*State, error)

GetState returns the current state of a terminal which may be useful to restore the terminal after a signal.

func MakeRaw

func MakeRaw(fd int) (*State, error)

MakeRaw put the terminal connected to the given file descriptor into raw mode and returns the previous state of the terminal so that it can be restored.

Jump to

Keyboard shortcuts

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