Documentation
¶
Overview ¶
Package qafterm provides a small, opinionated host layer for embedding a real terminal in a GUI application.
It deliberately does not implement terminal emulation itself: the emulator is github.com/charmbracelet/x/vt, a battle-tested xterm/VT emulator, and the pseudo-terminal handling is github.com/creack/pty. qafterm wires them together and adds the pieces a GUI host needs but no library provides:
- A session that owns a shell in a PTY and pumps bytes in both directions.
- A renderer-facing snapshot (rows of styled cells) that never exposes upstream types, so the emulator can be swapped without breaking users.
- Damage reporting: which rows changed since the last snapshot, so a host repaints only what it must.
- Scrollback-aware viewport rendering (scroll up into history).
- Key and mouse encoding that respects the terminal's own modes (for example application cursor keys).
Everything is pure Go and CGO-free.
Basic use:
ses, err := qafterm.Start(qafterm.Config{Cols: 120, Rows: 30})
if err != nil {
return err
}
defer ses.Close()
go func() {
for {
rows := ses.View(0) // viewport rows
changed := ses.Damage()
render(rows, changed)
time.Sleep(16 * time.Millisecond)
}
}()
Index ¶
- Constants
- Variables
- type Cell
- type Color
- type ColorKind
- type Config
- type KeyEvent
- type Mod
- type MouseButton
- type MouseEvent
- type MouseKind
- type Row
- type Session
- func (s *Session) AltScreen() bool
- func (s *Session) Bell() bool
- func (s *Session) Blur()
- func (s *Session) BracketedPaste() bool
- func (s *Session) Close() error
- func (s *Session) Cols() int
- func (s *Session) Cursor() (x, y int, hidden bool)
- func (s *Session) Damage() []int
- func (s *Session) Done() <-chan struct{}
- func (s *Session) Focus()
- func (s *Session) Key(k KeyEvent) error
- func (s *Session) MaxScrollback() int
- func (s *Session) Mouse(ev MouseEvent) error
- func (s *Session) MouseEnabled() bool
- func (s *Session) Paste(text string) error
- func (s *Session) Resize(cols, rows int) error
- func (s *Session) Rows() int
- func (s *Session) ScrollbackLen() int
- func (s *Session) ShellName() string
- func (s *Session) Snapshot() []Row
- func (s *Session) Text(text string) error
- func (s *Session) Title() string
- func (s *Session) View(scroll int) []Row
- func (s *Session) WorkingDir() string
- type UnderlineStyle
Constants ¶
const ( KeyEnter = vt.KeyEnter KeyTab = vt.KeyTab KeyEsc = vt.KeyEscape KeyBackspace = vt.KeyBackspace KeySpace = vt.KeySpace KeyUp = vt.KeyUp KeyDown = vt.KeyDown KeyRight = vt.KeyRight KeyLeft = vt.KeyLeft KeyHome = vt.KeyHome KeyEnd = vt.KeyEnd KeyPgUp = vt.KeyPgUp KeyPgDown = vt.KeyPgDown KeyInsert = vt.KeyInsert KeyDelete = vt.KeyDelete KeyF1 = vt.KeyF1 KeyF2 = vt.KeyF2 KeyF3 = vt.KeyF3 KeyF4 = vt.KeyF4 KeyF5 = vt.KeyF5 KeyF6 = vt.KeyF6 KeyF7 = vt.KeyF7 KeyF8 = vt.KeyF8 KeyF9 = vt.KeyF9 KeyF10 = vt.KeyF10 KeyF11 = vt.KeyF11 KeyF12 = vt.KeyF12 )
Special key codes. Printable keys use their rune directly.
Variables ¶
var DefaultColor = Color{Kind: ColorDefault}
DefaultColor is the terminal's default color.
Functions ¶
This section is empty.
Types ¶
type Cell ¶
type Cell struct {
// Text is the grapheme cluster shown in the cell. It is empty for a
// blank cell.
Text string
// Width is the number of columns the cell occupies (0, 1 or 2).
Width int
// FG and BG are the foreground and background colors.
FG, BG Color
// Bold, Faint, Italic, Blink, Reverse, Conceal and Strike mirror the
// corresponding SGR attributes.
Bold, Faint, Italic, Blink, Reverse, Conceal, Strike bool
// Underline is the underline decoration.
Underline UnderlineStyle
}
Cell is one character cell of the terminal screen.
type Config ¶
type Config struct {
// Shell is the program to run. Empty means $SHELL, then the current
// user's login shell from the password database, then /bin/sh.
Shell string
// Args are the arguments passed to the shell. When empty the shell is
// started as a login shell ("-l") if it supports it, otherwise bare.
Args []string
// Dir is the working directory of the shell. Empty means the current
// working directory.
Dir string
// Env holds extra environment entries in "KEY=VALUE" form. They are
// appended to the inherited environment; an entry with an existing key
// replaces it.
Env []string
// Cols and Rows are the initial terminal size in cells.
Cols int
Rows int
// Scrollback is the number of history lines kept. Zero means the
// emulator default (10000); negative disables scrollback.
Scrollback int
}
Config describes a terminal session to start.
type KeyEvent ¶
type KeyEvent struct {
// Code is the key: a printable rune, or one of the Key* constants.
Code rune
// Text is the literal text the key produced, when it differs from Code
// (for example a dead key or an IME result).
Text string
// Mod holds the active modifiers.
Mod Mod
}
KeyEvent is a key press delivered to a session.
func SpecialMod ¶
SpecialMod builds a key event for a special key with modifiers.
type MouseButton ¶
type MouseButton uint8
MouseButton identifies a mouse button.
const ( MouseNone MouseButton = iota MouseLeft MouseMiddle MouseRight MouseWheelUp MouseWheelDown MouseWheelLeft MouseWheelRight MouseButton8 MouseButton9 MouseButton10 MouseButton11 )
Mouse buttons, matching the xterm numbering.
type MouseEvent ¶
type MouseEvent struct {
Kind MouseKind
Button MouseButton
// X and Y are the zero-based cell coordinates.
X, Y int
// Mod holds the active modifiers.
Mod Mod
}
MouseEvent is a mouse event delivered to a session.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is a running shell attached to a pseudo-terminal.
func (*Session) AltScreen ¶
AltScreen reports whether the program is using the alternate screen (full screen applications such as vim or htop).
func (*Session) Blur ¶
func (s *Session) Blur()
Blur tells the shell the terminal lost focus (mode 1004).
func (*Session) BracketedPaste ¶
BracketedPaste reports whether the program enabled bracketed paste.
func (*Session) Close ¶
Close terminates the session and releases the pseudo-terminal. The emulator itself is shut down by the input pump (see pumpInput); Close kills the shell, closes the PTY, waits for the output pump, and then wakes the input pump so nothing stays parked.
func (*Session) Cursor ¶
Cursor returns the cursor position in cells and whether the cursor is hidden (the program asked for it to be invisible).
func (*Session) Damage ¶
Damage reports which screen rows changed since the previous call to Snapshot or View. The returned indices are relative to the visible screen; a scrolled viewport should be repainted in full.
func (*Session) Done ¶
func (s *Session) Done() <-chan struct{}
Done is closed when the shell exits.
func (*Session) Focus ¶
func (s *Session) Focus()
Focus tells the shell the terminal gained focus (mode 1004).
func (*Session) Key ¶
Key sends a key press to the shell. Special keys use the Key* constants; printable keys use their rune.
func (*Session) MaxScrollback ¶
MaxScrollback reports the scrollback limit.
func (*Session) Mouse ¶
func (s *Session) Mouse(ev MouseEvent) error
Mouse sends a mouse event to the shell. It is ignored unless the program enabled mouse reporting.
func (*Session) MouseEnabled ¶
MouseEnabled reports whether the program asked for mouse reports. Hosts must not forward mouse events while this is false: the program would receive reports it never asked for.
func (*Session) Paste ¶
Paste sends text to the shell using bracketed paste when the program enabled the mode.
func (*Session) ScrollbackLen ¶
ScrollbackLen reports how many history lines are buffered.
func (*Session) ShellName ¶
ShellName returns a short display name for the shell (for example "zsh"), suitable for a tab title.
func (*Session) Snapshot ¶
Snapshot returns the current viewport: the visible screen rows.
The walk holds emuMu for its whole duration: CellAt and Scrollback hand out pointers into the emulator's live buffer memory, so reading their fields must exclude the output pump's Write (which mutates those cells). emuMu is always taken before s.mu — the same order the pump uses.
func (*Session) View ¶
View returns the viewport rows scrolled up by the given number of lines. A scroll of zero shows the live screen; larger values replace the top of the screen with history lines. The result always has the terminal's height.
func (*Session) WorkingDir ¶
WorkingDir reports the last working directory reported by the shell, when the program supports that reporting.
type UnderlineStyle ¶
type UnderlineStyle uint8
UnderlineStyle is the underline decoration of a cell.
const ( UnderlineNone UnderlineStyle = iota UnderlineSingle UnderlineDouble UnderlineCurly UnderlineDotted UnderlineDashed )