tty

package
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 35 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Terminal output stage that turns Bubble Tea views into the fewest bytes a measured terminal needs

Index

Constants

View Source
const (
	AttrBold uint8 = 1 << iota
	AttrFaint
	AttrItalic
	AttrBlink
	AttrRapidBlink
	AttrReverse
	AttrConceal
	AttrStrike
)

Text attributes a cell carries

View Source
const (
	UnderNone uint8 = iota
	UnderSingle
	UnderDouble
	UnderCurly
	UnderDotted
	UnderDashed
)

Underline shapes

Variables

View Source
var ErrTerminalGone = errors.New("tty: the terminal is gone")

Failure of a program whose terminal went away under it

Functions

func Exec

func Exec(cmd *exec.Cmd, fn func(error) tea.Msg) tea.Cmd

Runs a command on the terminal, the program paused and its screen given back, fn's message delivered after

func JobControl

func JobControl() bool

Whether a shell's job control can stop and continue this process group, false for a group the kernel holds orphaned

func Opening

func Opening(title string) string

A terminal's title opening a sentence: a name stays as its terminal writes it, words before one start with a capital

func Suspend

func Suspend(line string) tea.Cmd

Gives the terminal back, writes the line and stops the process group for the shell, refused without job control

func Title

func Title(mark string, columns int, parts ...string) string

Title of the terminal's window and tab made of a mark and parts, as alpha composes it: the mark, then the parts that say something, the first cut to the columns given with an ellipsis, joined by a middle dot. Every escape sequence and control character is left out, so no part, such as a title a model wrote, can end the title's sequence and write one of its own

Types

type Cell

type Cell struct {
	Text  string
	Width uint8
	Style Style
	Link  uint32
	Rider string
	After string
}

One grid column, a cluster's first column holding its text and width, the rest holding width zero

type Color

type Color uint32

Color packed as a kind byte over a palette index or 24 bit RGB, zero for the default

func Basic

func Basic(n uint8) Color

Basic palette entry 0 to 15

func ColorOf

func ColorOf(c color.Color) Color

Packed form of any color, keeping basic and indexed colors as palette references

func Indexed

func Indexed(n uint8) Color

Entry of the 256 color palette

func RGB

func RGB(r, g, b uint8) Color

Direct 24 bit color

func (Color) Std

func (c Color) Std() color.Color

Standard library form of the color, nil for the default

type ColorAnswerMsg

type ColorAnswerMsg struct {
	Reply      string
	Foreground bool
}

What the terminal answered of the color of its background, OSC 11, or of its foreground, OSC 10, whole as it sent it: the model takes it in place of Bubble Tea's own reading of it, which scales parts of one or three hex digits wrongly and reads a part that is no number as nothing, and which the program holds back where it reads the answers itself

type Frame

type Frame struct {
	W, H  int
	Cells []Cell
}

Grid of cells a view asks for, rows of W columns

func (*Frame) Row

func (f *Frame) Row(y int) []Cell

Row y of the frame

type FrameStat

type FrameStat struct {
	At time.Time
	// Time the model took to build the view the frame shows
	View time.Duration
	// Time the renderer took to turn the view into bytes
	Compose time.Duration
	// Time the terminal took to accept the bytes
	Write time.Duration
	Bytes int
	// The frame erased the whole screen and wrote it anew, pixel images with it
	Erased bool
}

Timing of one frame the renderer wrote to the terminal

func (FrameStat) Total

func (s FrameStat) Total() time.Duration

Whole time the frame took from the model's view to the last byte written

type GoneMsg

type GoneMsg struct{ Err error }

Loss of the terminal without a hangup signal, every write to it failing, that the model answers by ending the program

type Identity

type Identity struct {
	// Name of a terminal known by name, lower case: kitty, ghostty, wezterm, konsole, rio, vscode, iterm, mintty, hyper, foot, xterm, mlterm,
	// alacritty, warp, apple, urxvt, vte, linux or dumb; else TERM lower cased, empty where the environment names none
	Name string
	// tmux stands between the program and the terminal, which passes a sequence on to the terminal under it while its allow-passthrough
	// option is on
	Tmux bool
	// GNU screen stands between them, tmux not
	Screen bool
	// The program runs over SSH, the terminal on another computer
	Remote bool
}

The terminal a program runs in as its environment names it, and what stands between the two: the one identification of the terminal the app reads what it supports by, its images and its notifications alike

func Identify

func Identify(getenv func(string) string) Identity

The terminal the environment names: by a specific TERM first, then by the marks of terminals that keep TERM generic, then by TERM_PROGRAM, then by marks a terminal leaves its children, which a terminal started inside another inherits, so those of a parent lose to fresher ones

func (Identity) Title

func (i Identity) Title() string

The terminal as a person calls it: the name of one known by name, else what its TERM says, else this terminal

func (Identity) Wrap

func (i Identity) Wrap(seq string) string

The sequence wrapped to pass through tmux or GNU screen to the terminal under it, the same text with neither

type Note

type Note struct {
	Title, Body     string
	Event, Session  string
	Folder, Project string
	ID              string
}

What a notification says: its title and one line under it, and for Warp the event it is of, stop as a reply ends and permission_request as the agent waits on the person, with the session and the folder it works in; the identity kitty keeps its parts together by

type NoteForm

type NoteForm int

How a terminal shows a notification of the desktop a program asks for

const (
	// It shows none, so nothing is sent
	NoteNone NoteForm = iota
	// iTerm2's OSC 9, which alpha sends every terminal: one line of text
	NoteOSC9
	// rxvt's OSC 777 notify, which foot, WezTerm, Ghostty and the terminals of VTE take: a title and a body
	NoteOSC777
	// kitty's OSC 99: a title and a body, each in base64 so nothing in them reads as part of the sequence
	NoteOSC99
)

type Notifier

type Notifier struct {
	Form NoteForm
	Name string
	Warp int
	// Wrapped for tmux, which passes a sequence on to the terminal under it when its allow-passthrough option is on
	Tmux bool
	// Wrapped for GNU screen
	Screen bool
}

The terminal a program runs in as its notifications go: the form it takes, what it is called, the version of Warp's agent protocol where Warp renders its payload, and whether tmux or screen stands between the program and it

func NotifierOf

func NotifierOf(getenv func(string) string) Notifier

The terminal the environment names, as Identify names it, and how it shows notifications; any terminal it does not know takes OSC 9, as alpha sends it

func (Notifier) FormName

func (n Notifier) FormName() string

Name of the form the terminal takes, as Status and the doctor say it

func (Notifier) Ignores

func (n Notifier) Ignores() bool

Whether the terminal shows no notification a program asks for

func (Notifier) Sequence

func (n Notifier) Sequence(note Note) string

Sequences that have the terminal show the notification, in its form, wrapped for tmux or screen; nothing for a terminal that shows none. Warp gets alpha's OSC 9 and, where it renders it, its agent payload on OSC 777

type Program

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

Bubble Tea program whose terminal bytes, modes, size, job control and restore tty owns

func NewProgram

func NewProgram(ctx context.Context, model tea.Model) *Program

Program for the model, run on the process's terminal until it quits or ctx ends

func (*Program) OnFrame

func (p *Program) OnFrame(fn func(FrameStat))

Has fn told the timing of every frame written to the terminal, set before Run

func (*Program) OnPanic

func (p *Program) OnPanic(fn func(any))

Has fn told the value of a panic in the model before the program reports it and ends, set before Run

func (*Program) Run

func (p *Program) Run() (model tea.Model, err error)

Runs the program until it quits, the terminal restored before anything it wrote to stderr is shown

func (*Program) Send

func (p *Program) Send(msg tea.Msg)

Sends a message to the model, waiting until the program runs and dropping it once the program ended

func (*Program) SuspendLine

func (p *Program) SuspendLine(fn func() string)

Has fn say the line written to the terminal given back as a stop signal of the operating system suspends the program, as Suspend's line is for a suspend the model asks for; called on the event loop, set before Run, none when nil

func (*Program) Terminated

func (p *Program) Terminated() syscall.Signal

Signal of the operating system that ended the program, zero when it ended on its own

func (*Program) Write

func (p *Program) Write(b []byte) (int, error)

Writes bytes to the terminal ahead of the next frame, in the order they are written, as the text of a raw command is: what pictures hand the terminal goes this way, so what hands an image over never follows what lets it go. Nothing is written before the program runs or once it ended

type Renderer

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

Turns views into terminal bytes for one terminal, measuring the widths it draws before trusting them

func NewRenderer

func NewRenderer(out io.Writer, w, h int) *Renderer

Renderer writing to the terminal, sized w by h

func (*Renderer) Clear

func (r *Renderer) Clear()

Clears the terminal and repaints the whole frame at the next write

func (*Renderer) Err

func (r *Renderer) Err() error

First write error, the terminal gone when it is not nil

func (*Renderer) Flush

func (r *Renderer) Flush(now time.Time) time.Duration

Writes whatever the terminal lacks, returning how long until a probe deadline wants another write, zero for none. The bytes are worked out under the renderer's lock and written without it: a terminal slow to take a large frame holds only the frame loop, never the event loop, which hands over the next view, takes a new size or a terminal answer meanwhile without waiting

func (*Renderer) Hold

func (r *Renderer) Hold()

Stops writing frames until Unhold, the terminal belonging to someone else meanwhile. A frame on its way to the terminal is written whole first, so nothing of it follows once the hold is in force

func (*Renderer) Loop

func (r *Renderer) Loop(interval time.Duration, done <-chan struct{})

Writes frames as they come, at most once per interval, until done closes

func (*Renderer) Release

func (r *Renderer) Release()

Undoes everything the session set and tells the guard nothing is left to undo, the renderer ready to enter again. A frame on its way to the terminal is written whole first

func (*Renderer) Render

func (r *Renderer) Render(v tea.View)

Takes the view to show next, the latest one winning

func (*Renderer) Reply

func (r *Renderer) Reply(msg tea.Msg) bool

Takes a terminal answer, reporting whether it was one tty asked for and so is no business of the program

func (*Renderer) Resize

func (r *Renderer) Resize(w, h int)

Takes a new terminal size, repainting everything at the next frame

func (*Renderer) SetProfile

func (r *Renderer) SetProfile(p colorprofile.Profile)

Sets the color profile cells are mapped into

func (*Renderer) Unhold

func (r *Renderer) Unhold()

Lets frames flow again, the session entered anew at the next write

func (*Renderer) Write

func (r *Renderer) Write(p []byte) (int, error)

Takes bytes another writer meant for the terminal, written ahead of the next frame, noting the queries among them

type Style

type Style struct {
	Fg, Bg, Ul Color
	Under      uint8
	Attrs      uint8
}

Colors, underline shape and attributes of a cell, comparable with ==

type SuspendRefusedMsg

type SuspendRefusedMsg struct{}

Answer to a suspend the program refused with the terminal untouched, no shell job control holding its process group

type TerminateMsg

type TerminateMsg struct{ Signal syscall.Signal }

Stop request of the operating system, an interrupt, a terminate or a hangup, that the model answers by ending the program

Jump to

Keyboard shortcuts

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