Documentation
¶
Overview ¶
Package term provides terminal utilities for interactive sessions.
Index ¶
Constants ¶
const ( // EscapePrefix is Ctrl-/ (0x1f) EscapePrefix byte = 0x1f )
Variables ¶
This section is empty.
Functions ¶
func EscapeHelpText ¶
func EscapeHelpText() string
EscapeHelpText returns help text explaining the escape sequences.
func GetSize ¶
GetSize returns the terminal dimensions (width, height). Returns (0, 0) if the file is not a terminal or size cannot be determined.
func IsEscapeError ¶
IsEscapeError returns true if the error is an EscapeError.
func IsTerminal ¶
IsTerminal returns true if the file is a terminal.
func RestoreTerminal ¶
func RestoreTerminal(state *RawModeState) error
RestoreTerminal restores the terminal to its previous state.
Types ¶
type EscapeAction ¶
type EscapeAction int
EscapeAction represents an action triggered by an escape sequence.
const ( // EscapeNone means no escape action was triggered. EscapeNone EscapeAction = iota // EscapeStop means the user wants to stop the run. EscapeStop // EscapeSnapshot means the user wants to take a manual snapshot. EscapeSnapshot )
func GetEscapeAction ¶
func GetEscapeAction(err error) EscapeAction
GetEscapeAction extracts the action from an EscapeError, or returns EscapeNone.
type EscapeError ¶
type EscapeError struct {
Action EscapeAction
}
EscapeError is returned when an escape sequence is detected.
func (EscapeError) Error ¶
func (e EscapeError) Error() string
type EscapeProxy ¶
type EscapeProxy struct {
// contains filtered or unexported fields
}
EscapeProxy wraps a reader and watches for escape sequences.
Escape sequences are: Ctrl-/ followed by:
- k: stop the run (returns EscapeError to unwind Read)
- s: take a snapshot (invokes onAction callback, continues reading)
If Ctrl-/ is followed by an unrecognized key, both bytes are passed through. If Ctrl-/ is followed by another Ctrl-/, a single Ctrl-/ is passed through (allowing the user to send a literal Ctrl-/).
func NewEscapeProxy ¶
func NewEscapeProxy(r io.Reader) *EscapeProxy
NewEscapeProxy creates an EscapeProxy that wraps the given reader.
func (*EscapeProxy) OnAction ¶ added in v0.3.0
func (e *EscapeProxy) OnAction(fn func(EscapeAction))
OnAction sets a callback for non-disruptive escape actions. Unlike EscapeStop which returns an EscapeError to unwind Read(), actions handled via OnAction (such as EscapeSnapshot) invoke the callback and continue reading.
The callback is invoked synchronously during Read() calls. It should be non-blocking; long-running work should be dispatched to a goroutine.
func (*EscapeProxy) OnPrefixChange ¶
func (e *EscapeProxy) OnPrefixChange(fn func(active bool))
OnPrefixChange sets a callback that fires when the escape prefix state changes. The callback receives true when Ctrl-/ is pressed (waiting for command key), and false when the sequence completes or is canceled. This can be used to update UI state, such as showing escape key hints in a status bar.
The callback is invoked synchronously during Read() calls. Callers must ensure the callback doesn't block or cause deadlocks. If Read() can be called concurrently, the callback must be thread-safe.
type RawModeState ¶
type RawModeState struct {
// contains filtered or unexported fields
}
RawModeState holds the previous terminal state for restoration.
func EnableRawMode ¶
func EnableRawMode(f *os.File) (*RawModeState, error)
EnableRawMode puts the terminal into raw mode, disabling echo and line buffering. Returns a state that must be passed to RestoreTerminal when done. This is required for escape sequence detection to work properly.