Help us shape the future of Go! Take the 2026 Go Developer Survey

terminal

package
v1.47.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package terminal contains shared terminal behavior and platform adapters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AutomaticPresentation

func AutomaticPresentation(format string, terminal bool, getenv func(string) string) bool

AutomaticPresentation permits terminal presentation only in automatic mode on a usable terminal outside CI. It does not query terminal state or input.

func CloseFrame

func CloseFrame(output FrameOutput, frame string, interrupted bool) error

CloseFrame bounds the final cleanup write when a terminal stops accepting output. Callers remain responsible for restoring native input/output modes.

func ColorProfile

func ColorProfile(w io.Writer, environ []string, terminal bool) colorprofile.Profile

ColorProfile picks the color profile for --format pretty output written to w. Lipgloss v2 styles always render full color escapes, so the output must be downsampled for its destination. This matches the lipgloss v1 behavior the CLI had before: plain text when w is not a terminal or NO_COLOR is set, and color on a terminal. Color overrides never decorate pipes or files. Callers supply terminal detection so wrapped output keeps its destination identity without exposing feature-specific writer types to this package.

func DarkBackground

func DarkBackground(colorfgbg string) bool

DarkBackground reads the explicit COLORFGBG background, defaulting to dark without querying the terminal.

func DetachedOutputContext

func DetachedOutputContext(ctx context.Context) context.Context

DetachedOutputContext opts the output helper into a separate session on supported platforms, so image diagnostics survive foreground-group signals.

func FishQuote

func FishQuote(value string) string

FishQuote quotes one argument for fish.

func FitPrompt

func FitPrompt(text string, width int) string

FitPrompt clips and pads prompt text to the available terminal cells.

func FormatCommand

func FormatCommand(args []string, shell string) string

FormatCommand formats an openai invocation. An empty result means the shell is unsupported.

func GoCallerName added in v1.40.3

func GoCallerName(pid int) (string, error)

GoCallerName returns the immediate caller of a Go launcher process. The caller must first verify that pid identifies its Go launcher. It does not search older ancestors or use shell configuration.

func InlineFrame

func InlineFrame(content string, width int) string

InlineFrame renders content as one soft-wrapped logical terminal line.

func IsWriterTerminal added in v1.39.1

func IsWriterTerminal(w io.Writer) bool

IsWriterTerminal recognizes the file and borrowed-file writers used by CLI output.

func OpenBrowser

func OpenBrowser(ctx context.Context, url string) error

OpenBrowser opens a trusted URL with the platform browser handler. Callers select the destination and bound the context; the launcher uses direct arguments, filters the child environment, and discards handler diagnostics.

func OutputCommand

func OutputCommand() *cli.Command

OutputCommand serves the private output protocol. Assembly must register it under tokenizer to preserve the existing helper invocation used by NewOutput.

func ParentProcessName

func ParentProcessName() (string, error)

ParentProcessName returns the immediate parent process name.

func ParentShell

func ParentShell(ctx context.Context) string

ParentShell identifies the immediate caller. The login shell in SHELL can differ from an interactive subshell, so it is not a fallback here.

func PowerShellQuote

func PowerShellQuote(value string) string

PowerShellQuote quotes one argument for PowerShell 7.

func QuoteProperties

func QuoteProperties(value string) (plain, control bool)

QuoteProperties reports whether a value is shell-safe without quotes and contains controls.

func SectionDivider added in v1.45.1

func SectionDivider(width int, colors bool, fill, edge, secondary string) string

SectionDivider renders the Batch browser's thin, center-bright rule.

func Sequence

func Sequence(text string) (sequence string, cells, read int)

Sequence counts a complete printable cluster, including ASCII-led keycap emoji. text must be nonempty. ANSI's sequence decoder alone treats the leading digit as a separate byte. Taking the larger width covers both scalar and grapheme-aware terminal presentation.

func ShellName

func ShellName(name string) string

ShellName recognizes supported shells from a process name or executable path.

func ShellQuote

func ShellQuote(value string) string

ShellQuote quotes one argument for Bash or zsh.

func ShellQuoter

func ShellQuoter(shell string) func(string) string

ShellQuoter returns the argument quoter for a supported shell, or nil.

Types

type BorrowedWriter

type BorrowedWriter struct {
	io.Writer
	File *os.File
}

BorrowedWriter redirects writes while preserving a borrowed terminal's identity. Closing the adapter never closes the caller's file.

func (BorrowedWriter) Close

func (w BorrowedWriter) Close() error

func (BorrowedWriter) Fd

func (w BorrowedWriter) Fd() uintptr

func (BorrowedWriter) Read

func (w BorrowedWriter) Read(p []byte) (int, error)

type FrameOutput

type FrameOutput interface {
	WriteContext(context.Context, []byte) (int, error)
}

FrameOutput writes terminal frames with cancellation support.

type FramePainter

type FramePainter interface {
	Control(string)
	Frame(string)
}

FramePainter queues ordered controls and replaceable complete frames.

type Output

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

Output isolates terminal writes in one disposable process. The original terminal remains borrowed for geometry and mode restoration.

func NewOutput

func NewOutput(ctx context.Context, executable string, output *os.File) (*Output, error)

NewOutput starts the executable's hidden tokenizer __output helper before the caller changes terminal modes. Close releases the helper without closing output.

func (*Output) Close

func (w *Output) Close() error

func (*Output) WriteContext

func (w *Output) WriteContext(ctx context.Context, data []byte) (written int, err error)

WriteContext reports only acknowledged bytes. A failed frame can already have reached the terminal partly, so callers must treat output as incomplete.

type Painter

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

Painter keeps terminal backpressure outside the editor's event loop. Initialization controls retain order; only a superseded complete frame can be discarded.

func NewPainter

func NewPainter(output FrameOutput, onFailure context.CancelFunc) *Painter

NewPainter starts a painter that invokes onFailure when output fails.

func (*Painter) Control

func (p *Painter) Control(data string)

func (*Painter) Frame

func (p *Painter) Frame(data string)

func (*Painter) Stop

func (p *Painter) Stop() error

Jump to

Keyboard shortcuts

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