terminal

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 25 Imported by: 1 module, 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.

README

terminal

A terminal for MyGo apps of native UI: a view that runs the user's shell, or any program, in a pseudo-terminal, with Ghostty's terminal emulator, libghostty-vt, loaded at run time with no cgo.

import "github.com/egoist/mygo/plugins/terminal"

term, err := terminal.New(terminal.Options{})
// ...
mygo.NewWindow(mygo.WindowOptions{Title: "Terminal", Content: ui.View(func(c *ui.Context) {
	terminal.View(c, term).Fill().AutoFocus()
})})

See the documentation, and examples/terminal for an app.

go generate here builds libghostty-vt for every platform with Zig, from the commit of Ghostty in internal/libbuild, into build/, and writes mygo-plugin.json, which names the files and their SHA-256 for the CLI and this package: publish them as the assets of the release that file points at. It writes ghostty_themes.go too, the themes that Ghostty ships, which go run ./internal/libbuild -themes writes alone, without Zig.

Documentation

Overview

Package terminal is a terminal for MyGo's native UI: a view that runs a shell, or any program, in a pseudo-terminal, with the terminal emulator of Ghostty, libghostty-vt, and draws it with package ui.

term, err := terminal.New(terminal.Options{})
...
mygo.NewWindow(mygo.WindowOptions{Title: "Terminal", Content: ui.View(func(c ui.Frame) {
	terminal.View(c, term).Fill()
})})

It does what terminals do: colors, styles and Unicode (wide characters, emoji, grapheme clusters), the scrollback with reflow on resize, the alternate screen of full-screen programs, the keyboard as legacy sequences or the Kitty keyboard protocol, mouse reporting, bracketed paste, focus reports, synchronized output, hyperlinks (OSC 8), titles, the working directory (OSC 7) and the clipboard (OSC 52). Selecting with the pointer copies as Ghostty does: a drag selects cells, a double click words and a triple click lines; Option (macOS) or Alt drags a rectangle.

libghostty-vt is a native library loaded at run time, with no cgo: `mygo build` and `mygo dev` put the build this package binds into the app, and programs run without them, as with `go run` and `go test`, download it once into the user's cache. See LibraryPath.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GhosttyThemes

func GhosttyThemes() []string

GhosttyThemes returns the names GhosttyTheme takes, sorted: those of the user's theme files and of the themes Ghostty ships.

func LibraryPath

func LibraryPath() (string, error)

LibraryPath returns the libghostty-vt the terminal loads, the first of:

  • the file $MYGO_GHOSTTY_VT names;
  • the one among the app's resources (mygo.PathResources), where `mygo build` and `mygo dev` put it, signed with the app on macOS;
  • the one next to the executable;
  • the one in the user's cache, where the CLI downloads it (<cache>/mygo/natives/<sha256>/), which programs that are not packaged apps, as under `go run` and `go test`, download there when it is missing, checking its SHA-256.

Packaged apps never download it: build them with the CLI.

func Load

func Load() error

Load loads libghostty-vt, once, from LibraryPath. New calls it; call it earlier to report a missing library before showing a terminal.

func View

func View(c *ui.Context, t *Terminal) ui.Element

View shows the terminal in a window of native UI, and takes its keyboard input once it has the focus, which a click gives it. Size it like any element, as with Fill or Grow; its size sets the terminal's, in cells of the font. A terminal shows in one view at a time.

Copy and paste are Command+C and Command+V on macOS, which the Edit menu's roles also send, and Control+Shift+C and Control+Shift+V elsewhere; Shift+Page Up and Shift+Page Down scroll a page of the scrollback, as does the wheel, unless the program takes the mouse. Hold Shift to select while a program takes the mouse, and Command (Control elsewhere) to open a hyperlink (OSC 8) on click.

Types

type CursorStyle

type CursorStyle int

CursorStyle is the shape of the cursor.

const (
	CursorBlock CursorStyle = iota
	CursorBar
	CursorUnderline
)

type Font

type Font struct {
	// Family is the font family, a comma-separated list as
	// ui.Element.Font takes it, to which ui.RegisterFont adds the app's
	// own fonts: "monospace", the system's, when empty.
	Family string
	// Size is the size in DIPs: 13 when zero.
	Size float32
	// Weight is how heavy text is, from 100 (thin) to 900 (black), of the
	// weights the family has: 400 when zero. Bold text is 300 heavier, and
	// at least 700.
	Weight int
	// Features turn OpenType features of the font on and off by tag, as
	// Ghostty's font-feature: "ss01" or "+ss01" turns one on, "calt=0" or
	// "-calt" off, and "cv05=2" picks an alternate. Coding fonts make
	// most of their ligatures with calt, and the others with liga.
	Features []string
	// LineHeight is the height of rows, as a multiple of the font's own
	// line height: 1 when zero, and 1.2 rows 20% taller, as Ghostty's
	// adjust-cell-height = 20%. Text is centered in its row.
	LineHeight float32
	// Thicken draws text with a thicker stroke, as Ghostty's font-thicken:
	// on macOS, with Core Text's font smoothing at its strongest, whatever
	// the text's color and the system's setting. It changes nothing on
	// Linux and Windows.
	Thicken bool
}

Font is the font of a terminal's text, with what Ghostty's font options set.

type Options

type Options struct {
	// Command is the program the terminal runs and its arguments. Empty
	// runs the user's shell as a login shell, as terminal apps do: $SHELL,
	// else /bin/zsh on macOS and /bin/sh elsewhere; PowerShell on Windows.
	Command []string
	// Dir is the working directory of the program: the user's home
	// directory when empty.
	Dir string
	// Env adds to the environment of the program, as "KEY=value". The
	// terminal sets TERM=xterm-256color and COLORTERM=truecolor, and LANG
	// to a UTF-8 locale when no locale is set, as when the app starts from
	// the Finder.
	Env []string
	// Conn connects the terminal to something other than a program, such
	// as a remote shell: what the terminal reads from it shows, and what
	// is typed is written to it. Command, Dir and Env are then ignored. A
	// Conn with a method Resize(cols, rows int) error is told the size.
	Conn io.ReadWriteCloser

	// Font is the font of the text: the system's monospaced font at 13
	// DIPs when zero.
	Font Font
	// Theme sets the colors, and DarkTheme those in dark windows when set,
	// as Ghostty's theme = light:…,dark:… does (GhosttyTheme returns
	// Ghostty's themes). Without either, the terminal follows the window's
	// light or dark appearance, with LightTheme and DarkTheme.
	Theme, DarkTheme *Theme
	// Cursor is the shape of the cursor, which programs may change, and
	// NoBlink keeps it from blinking unless a program asks it to.
	Cursor  CursorStyle
	NoBlink bool
	// Scrollback is about how many bytes of output the terminal keeps above
	// the screen: 10 MB when zero, none when negative.
	Scrollback int
	// OptionAsAlt makes Option on macOS the Alt key of programs (Meta),
	// rather than the key that types accented letters and symbols.
	OptionAsAlt bool
	// Transparent leaves the terminal's background undrawn, so that what
	// is under the view shows through, as the window's material or an
	// element's translucent background, as Ghostty's background-opacity
	// does: cells with a background color of their own still draw it.
	// Give the theme the background the view shows on, which the text
	// under a block cursor takes.
	Transparent bool

	// OnTitle, OnExit, OnBell and OnNotify run on a goroutine of the
	// terminal when a program sets the title, when the program (or Conn)
	// ends, when it rings the bell, and when it asks for a desktop
	// notification (OSC 9, OSC 777). Calls to the terminal are safe in
	// them.
	OnTitle  func(title string)
	OnExit   func(code int)
	OnBell   func()
	OnNotify func(title, body string)
}

Options configure a terminal.

type Terminal

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

Terminal is a terminal emulator and the program it runs. Its methods are safe from any goroutine.

func New

func New(opts Options) (*Terminal, error)

New makes a terminal running opts.Command, or connected to opts.Conn. The program starts once a view shows the terminal, at the size of the view, or half a second later at 80×24 when none does.

func (*Terminal) Close

func (t *Terminal) Close() error

Close closes the terminal: the program is hung up (SIGHUP), or Conn closed, and the terminal frees its memory. A view of it then shows nothing.

func (*Terminal) Dir

func (t *Terminal) Dir() string

Dir returns the working directory the shell reported (OSC 7, which the shells of macOS report), or "".

func (*Terminal) Done

func (t *Terminal) Done() <-chan struct{}

Done is closed once the program exited (or Conn ended).

func (*Terminal) ExitCode

func (t *Terminal) ExitCode() int

ExitCode returns the exit code of the program once it exited: 128 plus the signal's number for a program a signal killed, -1 for one that could not start.

func (*Terminal) Feed

func (t *Terminal) Feed(data []byte)

Feed shows data as if the program wrote it: text and escape sequences.

func (*Terminal) Paste

func (t *Terminal) Paste(text string)

Paste pastes text, bracketed when the program asks (mode 2004), so that it does not run as commands as it would typed.

func (*Terminal) Resize added in v0.2.11

func (t *Terminal) Resize(cols, rows int)

Resize sets the size of the screen in cells, for a terminal no view shows, as one a server keeps for a session while no window does: a view sets the size of the terminal it shows.

func (*Terminal) Send

func (t *Terminal) Send(data []byte)

Send sends data to the program as if typed.

func (*Terminal) SetFont

func (t *Terminal) SetFont(f Font)

SetFont changes the font, as Options.Font. set them, as for zooming.

func (*Terminal) SetTheme

func (t *Terminal) SetTheme(theme, dark *Theme)

SetTheme changes the colors, as Options.Theme and DarkTheme.

func (*Terminal) Size

func (t *Terminal) Size() (cols, rows int)

Size returns the size of the screen in cells.

func (*Terminal) Snapshot added in v0.2.11

func (t *Terminal) Snapshot() []byte

Snapshot returns what the terminal shows as escape sequences: its scrollback and screen with their styles, the cursor and the modes programs set. Fed to a new terminal of the same size, it shows the same, as a terminal attaching again to a session a server keeps.

func (*Terminal) Text

func (t *Terminal) Text() string

Text returns the text of the screen and its scrollback, with the lines a program's long output wrapped joined.

func (*Terminal) Title

func (t *Terminal) Title() string

Title returns the title the program set (OSC 0, OSC 2).

type Theme

type Theme struct {
	Foreground, Background ui.Color
	// Cursor colors the cursor: the foreground when zero. CursorText
	// colors the text under a block cursor: the background when zero.
	Cursor, CursorText ui.Color
	// Selection highlights the selected text: the foreground, a third
	// opaque, when zero. SelectionText colors the selected text: its own
	// colors when zero.
	Selection, SelectionText ui.Color
	// Palette holds the 16 colors of programs: black, red, green, yellow,
	// blue, magenta, cyan and white, then their bright kinds.
	Palette [16]ui.Color
}

Theme is the colors of a terminal, which programs may change (OSC 4, 10, 11, 12) until the terminal resets. GhosttyTheme returns Ghostty's themes.

func DarkTheme

func DarkTheme() *Theme

DarkTheme returns the colors of terminals in dark windows: the window's background and text, with Visual Studio Code's palette.

func GhosttyTheme

func GhosttyTheme(name string) (*Theme, error)

GhosttyTheme returns a theme of Ghostty's by its name, as Ghostty's theme option takes it ("Catppuccin Mocha", "Dracula", "Rose Pine Dawn"…): a theme file of the user's in Ghostty's themes directory ($XDG_CONFIG_HOME/ghostty/themes, ~/.config/ghostty/themes by default), else one of the themes Ghostty ships, which the plugin carries, of any case; or the theme file a path names. Each call returns a new Theme.

func LightTheme

func LightTheme() *Theme

LightTheme returns the colors of terminals in light windows: the window's background and text, with Visual Studio Code's palette.

func ParseGhosttyTheme

func ParseGhosttyTheme(data []byte) (*Theme, error)

ParseGhosttyTheme reads a theme in Ghostty's format, Ghostty's configuration's lines that set colors:

palette = 0=#45475a
…
palette = 15=#a6adc8
background = #1e1e2e
foreground = #cdd6f4
cursor-color = #f5e0dc
cursor-text = #1e1e2e
selection-background = #f5e0dc
selection-foreground = #1e1e2e

Colors are in hex (#rgb, #rrggbb, rrggbb…). Other lines of Ghostty's configuration are ignored, as are the palette's colors past the 16th, and the colors set to cell-foreground or cell-background, which the terminal then picks itself (see Theme). The colors a theme leaves out are those of DarkTheme, or LightTheme when its background is light.

Directories

Path Synopsis
internal
libbuild command
Libbuild builds libghostty-vt for every platform MyGo supports from Ghostty's sources at a commit, with Zig (the version Ghostty asks for), and writes mygo-plugin.json, which names the files and their SHA-256 for the CLI and the terminal package, and ghostty_themes.go, the themes that Ghostty ships:
Libbuild builds libghostty-vt for every platform MyGo supports from Ghostty's sources at a commit, with Zig (the version Ghostty asks for), and writes mygo-plugin.json, which names the files and their SHA-256 for the CLI and the terminal package, and ghostty_themes.go, the themes that Ghostty ships:
library
Package library finds libghostty-vt for the terminal plugin: next to the app, in the user's cache, or downloaded there, as mygo-plugin.json names it.
Package library finds libghostty-vt for the terminal plugin: next to the app, in the user's cache, or downloaded there, as mygo-plugin.json names it.
pty
Package pty runs processes in pseudo-terminals, in pure Go: /dev/ptmx on macOS and Linux, ConPTY on Windows.
Package pty runs processes in pseudo-terminals, in pure Go: /dev/ptmx on macOS and Linux, ConPTY on Windows.
vt
Package vt binds libghostty-vt, the terminal emulator of Ghostty, which it loads at run time through purego: no cgo.
Package vt binds libghostty-vt, the terminal emulator of Ghostty, which it loads at run time through purego: no cgo.

Jump to

Keyboard shortcuts

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