vttest

package module
v0.0.0-...-50d5c24 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 24 Imported by: 1

Documentation

Overview

Package vttest provides a virtual terminal implementation for testing terminal applications. It allows you to create a terminal instance with a pseudo-terminal (PTY) and capture its state at any moment, enabling you to write tests that verify the behavior of terminal applications.

Index

Constants

This section is empty.

Variables

View Source
var DefaultDrawer = func() *Drawer {
	cellW, cellH := 10, 16
	regular, _ := freetype.ParseFont(gomono.TTF)
	bold, _ := freetype.ParseFont(gomonobold.TTF)
	italic, _ := freetype.ParseFont(gomonoitalic.TTF)
	boldItalic, _ := freetype.ParseFont(gomonobolditalic.TTF)
	faceOpts := &truetype.Options{
		Size:    14,
		DPI:     72,
		Hinting: font.HintingFull,
	}
	regularFace := truetype.NewFace(regular, faceOpts)
	boldFace := truetype.NewFace(bold, faceOpts)
	italicFace := truetype.NewFace(italic, faceOpts)
	boldItalicFace := truetype.NewFace(boldItalic, faceOpts)

	return &Drawer{
		CellWidth:      cellW,
		CellHeight:     cellH,
		RegularFace:    regularFace,
		BoldFace:       boldFace,
		ItalicFace:     italicFace,
		BoldItalicFace: boldItalicFace,
	}
}()

DefaultDrawer are the default options used for creating terminal screen images.

Functions

This section is empty.

Types

type Cell

type Cell struct {
	// Content is the [Cell]'s content, which consists of a single grapheme
	// cluster. Most of the time, this will be a single rune as well, but it
	// can also be a combination of runes that form a grapheme cluster.
	Content string `json:"content,omitempty" yaml:"content,omitempty"`

	// The style of the cell. Nil style means no style. Zero value prints a
	// reset sequence.
	Style Style `json:"style,omitzero" yaml:"style,omitzero"`

	// Link is the hyperlink of the cell.
	Link Link `json:"link,omitzero" yaml:"link,omitzero"`

	// Width is the mono-spaced width of the grapheme cluster.
	Width int `json:"width,omitzero" yaml:"width,omitzero"`
}

Cell represents a single cell in the terminal screen.

type Color

type Color struct {
	Color color.Color `json:"color,omitempty" yaml:"color,omitempty"`
}

Color represents a terminal color, which can be one of the following: - An ANSI 16 color (0-15) of type ansi.BasicColor. - An ANSI 256 color (0-255) of type ansi.IndexedColor. - Or any other 24-bit color that implements color.Color.

func (Color) MarshalText

func (c Color) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for Color.

func (*Color) UnmarshalText

func (c *Color) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for Color.

type Cursor

type Cursor struct {
	Position Position       `json:"position," yaml:"position"`
	Visible  bool           `json:"visible" yaml:"visible"`
	Color    Color          `json:"color,omitzero" yaml:"color,omitzero"`
	Style    vt.CursorStyle `json:"style" yaml:"style"`
	Blink    bool           `json:"blink" yaml:"blink"`
}

Cursor represents the cursor state.

type Drawer

type Drawer struct {
	// CellWidth is the width of each cell in pixels. Default is 10.
	CellWidth int
	// CellHeight is the height of each cell in pixels. Default is 16.
	CellHeight int
	// RegularFace is the font face to use for regular text. Default is Go
	// mono.
	RegularFace font.Face
	// BoldFace is the font face to use for bold text. If nil, Go mono bold is
	// used.
	BoldFace font.Face
	// ItalicFace is the font face to use for italic text. If nil, Go mono italic
	// is used.
	ItalicFace font.Face
	// BoldItalicFace is the font face to use for bold italic text. If nil, Go
	// mono bold italic is used.
	BoldItalicFace font.Face
}

Drawer contains options for drawing a terminal emulator screen to an image.

func (*Drawer) Draw

func (d *Drawer) Draw(t uv.Screen) image.Image

Draw draws a uv.Screen to an image using the drawer options.

If s implements a [BackgroundColor]() method, it is used to fill the background. Otherwise, color.Black is used.

type Link struct {
	URL    string `json:"url,omitempty" yaml:"url,omitempty"`
	Params string `json:"params,omitempty" yaml:"params,omitempty"`
}

Link represents a hyperlink in the terminal screen.

type Modes

type Modes struct {
	ANSI map[ansi.ANSIMode]ansi.ModeSetting `json:"ansi" yaml:"ansi"`
	DEC  map[ansi.DECMode]ansi.ModeSetting  `json:"dec" yaml:"dec"`
}

Modes represents terminal modes.

type Position

type Position struct {
	X int `json:"x" yaml:"x"`
	Y int `json:"y" yaml:"y"`
}

Position represents a position in the terminal.

type Snapshot

type Snapshot struct {
	Modes     Modes    `json:"modes" yaml:"modes"`
	Title     string   `json:"title" yaml:"title"`
	Rows      int      `json:"rows" yaml:"rows"`
	Cols      int      `json:"cols" yaml:"cols"`
	AltScreen bool     `json:"alt_screen" yaml:"alt_screen"`
	Cursor    Cursor   `json:"cursor" yaml:"cursor"`
	BgColor   Color    `json:"bg_color,omitzero" yaml:"bg_color,omitzero"`
	FgColor   Color    `json:"fg_color,omitzero" yaml:"fg_color,omitzero"`
	Cells     [][]Cell `json:"cells" yaml:"cells"`
}

Snapshot represents a snapshot of the terminal state at a given moment.

type Style

type Style struct {
	Fg             Color        `json:"fg,omitzero" yaml:"fg,omitzero"`
	Bg             Color        `json:"bg,omitzero" yaml:"bg,omitzero"`
	UnderlineColor Color        `json:"underline_color,omitzero" yaml:"underline_color,omitzero"`
	Underline      uv.Underline `json:"underline,omitempty" yaml:"underline,omitempty"`
	Attrs          byte         `json:"attrs,omitempty" yaml:"attrs,omitempty"`
}

Style represents the Style of a cell.

type Terminal

type Terminal struct {
	Emulator *vt.SafeEmulator
	// contains filtered or unexported fields
}

Terminal represents a virtual terminal with it's PTY and state.

func NewTerminal

func NewTerminal(tb testing.TB, cols, rows int) (*Terminal, error)

NewTerminal creates a new virtual terminal with the given size for testing purposes. At any moment, you can take a snapshot of the terminal state by calling the Terminal.Snapshot method on the returned Terminal instance.

func (*Terminal) Close

func (t *Terminal) Close() error

Close closes the terminal and its PTY.

func (*Terminal) Image

func (t *Terminal) Image() image.Image

Image return s an image of the terminal emulator screen.

func (*Terminal) Input

func (t *Terminal) Input() io.Reader

Input returns the input side of the terminal's PTY.

func (*Terminal) Output

func (t *Terminal) Output() io.Writer

Output returns the output side of the terminal's PTY.

func (*Terminal) Paste

func (t *Terminal) Paste(text string)

Paste sends the given text to the terminal emulator as if pasted by a user.

func (*Terminal) Resize

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

Resize resizes the terminal and its PTY.

func (*Terminal) SendKey

func (t *Terminal) SendKey(k uv.KeyEvent)

SendKey sends the given key event to the terminal emulator as if typed by a user.

func (*Terminal) SendMouse

func (t *Terminal) SendMouse(m uv.MouseEvent)

SendMouse sends the given mouse event to the terminal emulator as if performed by a user.

func (*Terminal) SendText

func (t *Terminal) SendText(text string)

SendText sends the given raw text to the terminal emulator as if typed by a user.

func (*Terminal) Snapshot

func (t *Terminal) Snapshot() Snapshot

Snapshot takes a snapshot of the current terminal state. The returned Snapshot can be used to inspect the terminal state at the moment the snapshot was taken. It can also be serialized to JSON or YAML for further analysis or testing purposes.

func (*Terminal) Start

func (t *Terminal) Start(cmd *exec.Cmd) error

Start starts a process attached to the terminal's PTY.

func (*Terminal) Wait

func (t *Terminal) Wait(cmd *exec.Cmd) error

Wait waits for the process attached to the terminal's PTY to exit.

Directories

Path Synopsis
Package snapshot provides helpers for working with terminal snapshots.
Package snapshot provides helpers for working with terminal snapshots.

Jump to

Keyboard shortcuts

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