vt100

package module
v0.0.0-...-f9a4f7e Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2023 License: MIT Imports: 11 Imported by: 8

README

#VT100

GoDoc

This project was based on jaguilar/vt100

This is a vt100 screen reader. It seems to do a pretty decent job of parsing the nethack input stream, which is all I want it for anyway.

Here is a screenshot of the HTML-formatted screen data:

The features we currently support:

  • Cursor movement
  • Erasing
  • Many of the text properties -- underline, inverse, blink, etc.
  • Sixteen colors
  • Cursor saving and unsaving
  • UTF-8
  • Scrolling

Not currently supported (and no plans to support):

  • Prompts
  • Other cooked mode features

The API is not stable! This is a v0 package.

Documentation

Overview

package vt100 implements a quick-and-dirty programmable ANSI terminal emulator.

You could, for example, use it to run a program like nethack that expects a terminal as a subprocess. It tracks the position of the cursor, colors, and various other aspects of the terminal's state, and allows you to inspect them.

We do very much mean the dirty part. It's not that we think it might have bugs. It's that we're SURE it does. Currently, we only handle raw mode, with no cooked mode features like scrolling. We also misinterpret some of the control codes, which may or may not matter for your purpose.

Index

Constants

View Source
const (
	Normal Intensity = 0
	Bright           = 1
	Dim              = 2
)

Variables

View Source
var (
	// Technically RGBAs are supposed to be premultiplied. But CSS doesn't expect them
	// that way, so we won't do it in this file.
	DefaultColor = color.RGBA{0, 0, 0, 0}
	// Our black has 255 alpha, so it will compare negatively with DefaultColor.
	Black   = color.RGBA{0, 0, 0, 255}
	Red     = color.RGBA{255, 0, 0, 255}
	Green   = color.RGBA{0, 255, 0, 255}
	Yellow  = color.RGBA{255, 255, 0, 255}
	Blue    = color.RGBA{0, 0, 255, 255}
	Magenta = color.RGBA{255, 0, 255, 255}
	Cyan    = color.RGBA{0, 255, 255, 255}
	White   = color.RGBA{255, 255, 255, 255}
)

Functions

This section is empty.

Types

type Command

type Command interface {
	// contains filtered or unexported methods
}

Command is a type of object that the terminal can process to perform an update.

func Decode

func Decode(s io.RuneScanner) (Command, error)

Decode decodes one ANSI terminal command from s.

s should be connected to a client program that expects an ANSI terminal on the other end. It will push bytes to us that we are meant to intepret as terminal control codes, or text to place onto the terminal.

This Command alone does not actually update the terminal. You need to pass it to VT100.Process().

You should not share s with any other reader, because it could leave the stream in an invalid state.

type Cursor

type Cursor struct {
	// Y and X are the coordinates.
	Y, X int

	// F is the format that will be displayed.
	F Format
}

Cursor represents both the position and text type of the cursor.

type Format

type Format struct {
	// Fg is the foreground color.
	Fg color.RGBA
	// Bg is the background color.
	Bg color.RGBA
	// Intensity is the text intensity (bright, normal, dim).
	Intensity Intensity
	// Various text properties.
	Underscore, Conceal, Negative, Blink, Inverse bool
}

Format represents the display format of text on a terminal.

type Intensity

type Intensity int

type UnsupportedError

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

UnsupportedError indicates that we parsed an operation that this terminal does not implement. Such errors indicate that the client program asked us to perform an action that we don't know how to. It MAY be safe to continue trying to do additional operations. This is a distinct category of errors from things we do know how to do, but are badly encoded, or errors from the underlying io.RuneScanner that we're reading commands from.

type VT100

type VT100 struct {
	// Height and Width are the dimensions of the terminal.
	Height, Width int

	// Content is the text in the terminal.
	Content [][]rune

	// Format is the display properties of each cell.
	Format [][]Format

	// Cursor is the current state of the cursor.
	Cursor Cursor
	// contains filtered or unexported fields
}

VT100 represents a simplified, raw VT100 terminal.

func NewVT100

func NewVT100(y, x int) *VT100

NewVT100 creates a new VT100 object with the specified dimensions. y and x must both be greater than zero.

Each cell is set to contain a ' ' rune, and all formats are left as the default.

func (*VT100) HTML

func (v *VT100) HTML() string

HTML renders v as an HTML fragment. One idea for how to use this is to debug the current state of the screen reader.

func (*VT100) Process

func (v *VT100) Process(c Command) error

Process handles a single ANSI terminal command, updating the terminal appropriately.

One special kind of error that this can return is an UnsupportedError. It's probably best to check for these and skip, because they are likely recoverable. Support errors are exported as expvars, so it is possibly not necessary to log them. If you want to check what's failed, start a debug http server and examine the vt100-unsupported-commands field in /debug/vars.

func (*VT100) Resize

func (v *VT100) Resize(y, x int)

func (*VT100) UsedHeight

func (v *VT100) UsedHeight() int

func (*VT100) Write

func (v *VT100) Write(dt []byte) (int, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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