vt

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: BSD-3-Clause Imports: 18 Imported by: 11

README

vt GoDoc License Go Report Card

This is a package for dealing with terminal emulators.

It is version 2 of xyproto/vt100.

The Orbiton editor and the Algernon web server uses vt.

General info
  • License: BSD-3
  • Version: 1.3.0

Documentation

Index

Examples

Constants

View Source
const NoColor string = "\033[0m"

Variables

View Source
var (
	Pink              = LightMagenta
	Gray              = DarkGray
	BackgroundWhite   = BackgroundLightGray
	BackgroundGray    = BackgroundLightGray
	BackgroundDefault = DefaultBackground
)
View Source
var (
	DarkColorMap = map[string]AttributeColor{
		"black":        Black,
		"Black":        Black,
		"red":          Red,
		"Red":          Red,
		"green":        Green,
		"Green":        Green,
		"yellow":       Yellow,
		"Yellow":       Yellow,
		"blue":         Blue,
		"Blue":         Blue,
		"magenta":      Magenta,
		"Magenta":      Magenta,
		"cyan":         Cyan,
		"Cyan":         Cyan,
		"gray":         DarkGray,
		"Gray":         DarkGray,
		"white":        LightGray,
		"White":        LightGray,
		"lightwhite":   White,
		"LightWhite":   White,
		"darkred":      Red,
		"DarkRed":      Red,
		"darkgreen":    Green,
		"DarkGreen":    Green,
		"darkyellow":   Yellow,
		"DarkYellow":   Yellow,
		"darkblue":     Blue,
		"DarkBlue":     Blue,
		"darkmagenta":  Magenta,
		"DarkMagenta":  Magenta,
		"darkcyan":     Cyan,
		"DarkCyan":     Cyan,
		"darkgray":     DarkGray,
		"DarkGray":     DarkGray,
		"lightred":     LightRed,
		"LightRed":     LightRed,
		"lightgreen":   LightGreen,
		"LightGreen":   LightGreen,
		"lightyellow":  LightYellow,
		"LightYellow":  LightYellow,
		"lightblue":    LightBlue,
		"LightBlue":    LightBlue,
		"lightmagenta": LightMagenta,
		"LightMagenta": LightMagenta,
		"lightcyan":    LightCyan,
		"LightCyan":    LightCyan,
		"lightgray":    LightGray,
		"LightGray":    LightGray,
	}
	LightColorMap = map[string]AttributeColor{
		"black":        Black,
		"Black":        Black,
		"red":          LightRed,
		"Red":          LightRed,
		"green":        LightGreen,
		"Green":        LightGreen,
		"yellow":       LightYellow,
		"Yellow":       LightYellow,
		"blue":         LightBlue,
		"Blue":         LightBlue,
		"magenta":      LightMagenta,
		"Magenta":      LightMagenta,
		"cyan":         LightCyan,
		"Cyan":         LightCyan,
		"gray":         LightGray,
		"Gray":         LightGray,
		"white":        White,
		"White":        White,
		"lightwhite":   White,
		"LightWhite":   White,
		"lightred":     LightRed,
		"LightRed":     LightRed,
		"lightgreen":   LightGreen,
		"LightGreen":   LightGreen,
		"lightyellow":  LightYellow,
		"LightYellow":  LightYellow,
		"lightblue":    LightBlue,
		"LightBlue":    LightBlue,
		"lightmagenta": LightMagenta,
		"LightMagenta": LightMagenta,
		"lightcyan":    LightCyan,
		"LightCyan":    LightCyan,
		"lightgray":    LightGray,
		"LightGray":    LightGray,
		"darkred":      Red,
		"DarkRed":      Red,
		"darkgreen":    Green,
		"DarkGreen":    Green,
		"darkyellow":   Yellow,
		"DarkYellow":   Yellow,
		"darkblue":     Blue,
		"DarkBlue":     Blue,
		"darkmagenta":  Magenta,
		"DarkMagenta":  Magenta,
		"darkcyan":     Cyan,
		"DarkCyan":     Cyan,
		"darkgray":     DarkGray,
		"DarkGray":     DarkGray,
	}
)
View Source
var EnvNoColor = env.Bool("NO_COLOR")

EnvNoColor respects the NO_COLOR environment variable

Functions

func Clear

func Clear()

func Close

func Close()

func ColorSplit added in v1.2.1

func ColorSplit(line, sep string, headColor, sepColor, tailColor AttributeColor, reverse bool) (string, string)

ColorSplit splits on the first sep in line. It returns two parts: left and right. The right part includes the sep itself (so subsequent splits see it). nil color funcs are skipped. reverse=true swaps which side gets the fallback when sep is absent.

func Colorize

func Colorize(line string) string

Colorize comments in gray. Colorize (){}[] in an alternating way. This provides simple/rudimentary syntax highlighting.

func EchoOff

func EchoOff()

func Eprint added in v1.3.0

func Eprint(msg ...interface{})

func Eprintf added in v1.3.0

func Eprintf(format string, msg ...interface{})

func Eprintln added in v1.3.0

func Eprintln(msg ...interface{})

func Fprint added in v1.3.0

func Fprint(w io.Writer, msg ...interface{})

func Fprintf added in v1.3.0

func Fprintf(w io.Writer, format string, msg ...interface{})

func Fprintln added in v1.3.0

func Fprintln(w io.Writer, msg ...interface{})

func GetBackgroundColor

func GetBackgroundColor(tty *TTY) (float64, float64, float64, error)

GetBackgroundColor prints a code to the terminal emulator, reads the results and tries to interpret it as the RGB background color. Returns three float64 values, and possibly an error value.

func Home

func Home()

func Init

func Init()

func MustTermSize

func MustTermSize() (uint, uint)

MustTermSize returns the terminal size using golang.org/x/term for cross-platform compatibility

func Print added in v1.2.10

func Print(msg ...interface{})

func Printf added in v1.2.10

func Printf(format string, msg ...interface{})

func Println added in v1.2.10

func Println(msg ...interface{})
Example
o := NewTextOutput(true, true)
o.Println("hello")
Output:

hello

func Reset

func Reset()

func SetLineWrap

func SetLineWrap(enable bool)

func SetNoColor

func SetNoColor()

func SetXY

func SetXY(x, y uint)

Move cursor to the given position (0,0 is top left)

func SetXYDirect

func SetXYDirect(x, y uint)

func ShowCursor

func ShowCursor(enable bool)

func Stop

func Stop() string

Return a string for resetting the attributes

func WaitForKey

func WaitForKey()

WaitForKey waits for ctrl-c, Return, Esc, Space, or 'q' to be pressed

Types

type AttributeColor

type AttributeColor uint32
const (
	// Non-color attributes
	ResetAll   AttributeColor = 0
	Bright     AttributeColor = 1
	Dim        AttributeColor = 2
	Underscore AttributeColor = 4
	Blink      AttributeColor = 5
	Reverse    AttributeColor = 7
	Hidden     AttributeColor = 8
	None       AttributeColor = 0

	Black     AttributeColor = 30
	Red       AttributeColor = 31
	Green     AttributeColor = 32
	Yellow    AttributeColor = 33
	Blue      AttributeColor = 34
	Magenta   AttributeColor = 35
	Cyan      AttributeColor = 36
	LightGray AttributeColor = 37

	DarkGray     AttributeColor = 90
	LightRed     AttributeColor = 91
	LightGreen   AttributeColor = 92
	LightYellow  AttributeColor = 93
	LightBlue    AttributeColor = 94
	LightMagenta AttributeColor = 95
	LightCyan    AttributeColor = 96
	White        AttributeColor = 97

	BackgroundBlack     AttributeColor = 40
	BackgroundRed       AttributeColor = 41
	BackgroundGreen     AttributeColor = 42
	BackgroundYellow    AttributeColor = 43
	BackgroundBlue      AttributeColor = 44
	BackgroundMagenta   AttributeColor = 45
	BackgroundCyan      AttributeColor = 46
	BackgroundLightGray AttributeColor = 47

	Default           AttributeColor = 39
	DefaultBackground AttributeColor = 49
)

func (AttributeColor) Background

func (ac AttributeColor) Background() AttributeColor

Modify color attributes so that they become background color attributes instead

func (AttributeColor) Bright

func (ac AttributeColor) Bright() AttributeColor

Return a new AttributeColor that has "Bright" added to the list of attributes

func (AttributeColor) Combine

func (ac AttributeColor) Combine(other AttributeColor) AttributeColor

func (*AttributeColor) Equal

func (ac *AttributeColor) Equal(other AttributeColor) bool

func (AttributeColor) Error

func (ac AttributeColor) Error(text string)

Same as output, but outputs to stderr instead of stdout

func (AttributeColor) Get

func (ac AttributeColor) Get(text string) string

An alias for StartStop

func (AttributeColor) Head

func (ac AttributeColor) Head() uint32

func (AttributeColor) Ints

func (ac AttributeColor) Ints() []int

func (AttributeColor) Output

func (ac AttributeColor) Output(text string)

Use this color to output the given text. Will reset the attributes at the end of the string. Outputs a newline.

func (AttributeColor) Start

func (ac AttributeColor) Start(text string) string

Get the full string needed for outputting colored text, with the text, but don't reset the attributes at the end of the string

func (AttributeColor) StartStop

func (ac AttributeColor) StartStop(text string) string

Get the full string needed for outputting colored texti, with the text and stopping the color attribute

func (AttributeColor) Stop

func (ac AttributeColor) Stop(text string) string

Get the text and the terminal codes for resetting the attributes

func (AttributeColor) String

func (ac AttributeColor) String() string

Return the VT100 terminal codes for setting this combination of attributes and color attributes

func (AttributeColor) Tail

func (ac AttributeColor) Tail() uint32

type Canvas

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

func NewCanvas

func NewCanvas() *Canvas

func (*Canvas) At

func (c *Canvas) At(x, y uint) (rune, error)

At returns the rune at the given coordinates, or an error if out of bounds

func (*Canvas) Clear

func (c *Canvas) Clear()

Clear canvas

func (*Canvas) Copy

func (c *Canvas) Copy() Canvas

Copy creates a new Canvas struct that is a copy of this one. The mutex is initialized as a new mutex.

func (*Canvas) Draw

func (c *Canvas) Draw()

Draw the entire canvas

func (*Canvas) DrawAndSetCursor

func (c *Canvas) DrawAndSetCursor(x, y uint)

DrawAndSetCursor draws the entire canvas and then places the cursor at x,y

func (*Canvas) Fill

func (c *Canvas) Fill(fg AttributeColor)

Change the foreground color for each character

func (*Canvas) FillBackground

func (c *Canvas) FillBackground(bg AttributeColor)

Change the background color for each character

func (*Canvas) H

func (c *Canvas) H() uint

func (*Canvas) Height

func (c *Canvas) Height() uint

func (*Canvas) HideCursor

func (c *Canvas) HideCursor()

func (*Canvas) HideCursorAndDraw

func (c *Canvas) HideCursorAndDraw()

HideCursorAndDraw will hide the cursor and then draw the entire canvas

func (*Canvas) HideCursorAndRedraw

func (c *Canvas) HideCursorAndRedraw()

func (*Canvas) Lock

func (c *Canvas) Lock()

func (*Canvas) Plot

func (c *Canvas) Plot(x, y uint, r rune)

func (*Canvas) PlotAll

func (c *Canvas) PlotAll()

PlotAll tries to plot each individual rune. It's very inefficient and meant to be used as a robust fallback.

func (*Canvas) PlotColor

func (c *Canvas) PlotColor(x, y uint, fg AttributeColor, r rune)

func (*Canvas) Redraw

func (c *Canvas) Redraw()

func (*Canvas) Resize

func (c *Canvas) Resize()

func (*Canvas) Resized

func (c *Canvas) Resized() *Canvas

Check if the canvas was resized, and adjust values accordingly. Returns a new canvas, or nil.

func (*Canvas) SetLineWrap

func (c *Canvas) SetLineWrap(enable bool)

func (*Canvas) SetRunewise

func (c *Canvas) SetRunewise(b bool)

func (*Canvas) SetShowCursor

func (c *Canvas) SetShowCursor(enable bool)

func (*Canvas) ShowCursor

func (c *Canvas) ShowCursor()

func (*Canvas) Size

func (c *Canvas) Size() (uint, uint)

Return the size of the current canvas

func (*Canvas) String

func (c *Canvas) String() string

String returns only the characters, as a long string with a newline after each row

func (*Canvas) ToImage

func (c *Canvas) ToImage() (image.Image, error)

func (*Canvas) Unlock

func (c *Canvas) Unlock()

func (*Canvas) W

func (c *Canvas) W() uint

func (*Canvas) Width

func (c *Canvas) Width() uint

func (*Canvas) Write

func (c *Canvas) Write(x, y uint, fg, bg AttributeColor, s string)

func (*Canvas) WriteBackground

func (c *Canvas) WriteBackground(x, y uint, bg AttributeColor)

WriteBackground will write a background color to the canvas The x and y must be within range (x < c.w and y < c.h)

func (*Canvas) WriteBackgroundAddRuneIfEmpty

func (c *Canvas) WriteBackgroundAddRuneIfEmpty(x, y uint, bg AttributeColor, r rune)

WriteBackgroundAddRuneIfEmpty will write a background color to the canvas The x and y must be within range (x < c.w and y < c.h)

func (*Canvas) WriteBackgroundNoLock

func (c *Canvas) WriteBackgroundNoLock(x, y uint, bg AttributeColor)

WriteBackgroundNoLock will write a background color to the canvas The x and y must be within range (x < c.w and y < c.h) The canvas mutex is not locked

func (*Canvas) WriteRune

func (c *Canvas) WriteRune(x, y uint, fg, bg AttributeColor, r rune)

WriteRune will write a colored rune to the canvas

func (*Canvas) WriteRuneB

func (c *Canvas) WriteRuneB(x, y uint, fg, bgb AttributeColor, r rune)

WriteRuneB will write a colored rune to the canvas The x and y must be within range (x < c.w and y < c.h)

func (*Canvas) WriteRuneBNoLock

func (c *Canvas) WriteRuneBNoLock(x, y uint, fg, bgb AttributeColor, r rune)

WriteRuneBNoLock will write a colored rune to the canvas The x and y must be within range (x < c.w and y < c.h) The canvas mutex is not locked

func (*Canvas) WriteRunesB

func (c *Canvas) WriteRunesB(x, y uint, fg, bgb AttributeColor, r rune, count uint)

WriteRunesB will write repeated colored runes to the canvas. This is the same as WriteRuneB, but bg.Background() has already been called on the background attribute. The x and y must be within range (x < c.w and y < c.h). x + count must be within range too.

func (*Canvas) WriteString

func (c *Canvas) WriteString(x, y uint, fg, bg AttributeColor, s string)

WriteString will write a string to the canvas.

func (*Canvas) WriteTagged added in v1.2.11

func (c *Canvas) WriteTagged(x, y uint, bgColor AttributeColor, tagged string)

WriteTagged writes a tagged string ("<green>hello</green>") to the Canvas

type Char

type Char ColorRune

for API stability

type CharAttribute

type CharAttribute struct {
	A AttributeColor
	R rune
}

CharAttribute is a rune and a color attribute

type ColorRune

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

type TTY

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

func NewTTY

func NewTTY() (*TTY, error)

NewTTY opens /dev/tty in raw and cbreak mode as a term.Term

func (*TTY) ASCII

func (tty *TTY) ASCII() int

ASCII returns the ASCII code of the key pressed

func (*TTY) Close

func (tty *TTY) Close()

Close will restore and close the raw terminal

func (*TTY) Flush

func (tty *TTY) Flush()

Flush flushes the terminal output

func (*TTY) Key

func (tty *TTY) Key() int

Key reads the keycode or ASCII code and avoids repeated keys

func (*TTY) KeyCode

func (tty *TTY) KeyCode() int

KeyCode returns the key code of the key pressed

func (*TTY) NoBlock

func (tty *TTY) NoBlock()

NoBlock sets the terminal to cbreak mode (non-blocking)

func (*TTY) PrintRawBytes

func (tty *TTY) PrintRawBytes()

PrintRawBytes for debugging raw byte sequences

func (*TTY) RawMode

func (tty *TTY) RawMode()

RawMode switches the terminal to raw mode

func (*TTY) ReadString

func (tty *TTY) ReadString() (string, error)

ReadString reads a string from the TTY with timeout

func (*TTY) Restore

func (tty *TTY) Restore()

Restore the terminal to its original state

func (*TTY) Rune

func (tty *TTY) Rune() rune

Rune reads a rune, handling special sequences for arrows, Home, End, etc.

func (*TTY) SetTimeout

func (tty *TTY) SetTimeout(d time.Duration)

SetTimeout sets a timeout for reading a key

func (*TTY) String

func (tty *TTY) String() string

String reads a string, handling key sequences and printable characters

func (*TTY) Term

func (tty *TTY) Term() *term.Term

Term will return the underlying term.Term

func (*TTY) WriteString

func (tty *TTY) WriteString(s string) error

WriteString writes a string to the terminal

type TextOutput

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

TextOutput keeps state about verbosity and if colors are enabled

func New added in v1.2.5

func New() *TextOutput

New can initialize a new TextOutput struct, which can have colors turned on or off and where the output can be enabled (verbose) or disabled (silent). If NO_COLOR is set, colors are disabled.

func NewTextOutput

func NewTextOutput(color, enabled bool) *TextOutput

NewTextOutput can initialize a new TextOutput struct, which can have colors turned on or off and where the output can be enabled (verbose) or disabled (silent). If NO_COLOR is set, colors are disabled, regardless.

func (*TextOutput) DarkTags

func (o *TextOutput) DarkTags(colors ...string) string

Replace <blue> with starting a light blue color attribute and <off> with using the default attributes. </blue> can also be used for using the default attributes.

func (*TextOutput) Disable added in v1.2.8

func (o *TextOutput) Disable()

Disable text output

func (*TextOutput) Enable added in v1.2.8

func (o *TextOutput) Enable()

Enable text output

func (*TextOutput) Enabled added in v1.3.0

func (o *TextOutput) Enabled() bool

Enabled checks if the text output is enabled

func (*TextOutput) Eprint added in v1.3.0

func (o *TextOutput) Eprint(msg ...interface{})

Eprint writes a message to stderr if output is enabled

func (*TextOutput) Eprintf added in v1.3.0

func (o *TextOutput) Eprintf(format string, args ...interface{})

Eprintf writes a formatted message to stderr if output is enabled

func (*TextOutput) Eprintln added in v1.3.0

func (o *TextOutput) Eprintln(msg ...interface{})

Eprintln writes a message to stderr if output is enabled

func (*TextOutput) Err

func (o *TextOutput) Err(msg string)

Err writes an error message in red to stderr if output is enabled

func (*TextOutput) ErrExit added in v1.2.3

func (o *TextOutput) ErrExit(msg string)

ErrExit writes an error message to stderr and quit with exit code 1

func (*TextOutput) ExtractToSlice

func (o *TextOutput) ExtractToSlice(s string, pcc *[]CharAttribute) uint

ExtractToSlice iterates over an ANSI encoded string, parsing out color codes and places it in a slice of CharAttribute. Each CharAttribute in the slice represents a character in the input string and its corresponding color attributes. This function handles escaping sequences and converts ANSI color codes to AttributeColor structs. The returned uint is the number of stored elements.

func (*TextOutput) Fprint added in v1.3.0

func (o *TextOutput) Fprint(w io.Writer, msg ...interface{})

Fprint writes a message to the given io.Writer, if output is enabled

func (*TextOutput) Fprintf added in v1.3.0

func (o *TextOutput) Fprintf(w io.Writer, format string, args ...interface{})

Fprintf writes a formatted message to the given io.Writer, if output is enabled

func (*TextOutput) Fprintln added in v1.3.0

func (o *TextOutput) Fprintln(w io.Writer, msg ...interface{})

Fprintln writes a message to the given io.Writer, if output is enabled

func (*TextOutput) InterfaceTags

func (o *TextOutput) InterfaceTags(colors ...interface{}) string

InterfaceTags is the same as LightTags, but with interfaces

func (*TextOutput) LightBlue

func (o *TextOutput) LightBlue(s string) string

func (*TextOutput) LightTags

func (o *TextOutput) LightTags(colors ...string) string

Replace <blue> with starting a light blue color attribute and <off> with using the default attributes. </blue> can also be used for using the default attributes.

func (*TextOutput) OutputTags

func (o *TextOutput) OutputTags(colors ...string)

OutputTags will output text that may have tags like "<blue>", "</blue>" or "<off>" for enabling or disabling color attributes. Respects the color/enabled settings of this TextOutput.

func (*TextOutput) Print

func (o *TextOutput) Print(msg ...interface{})

Print writes a message to stdout if output is enabled

func (*TextOutput) Printf added in v1.2.7

func (o *TextOutput) Printf(format string, args ...interface{})

Printf writes a formatted message to stdout if output is enabled

func (*TextOutput) Println

func (o *TextOutput) Println(msg ...interface{})

Println writes a message to stdout if output is enabled

func (*TextOutput) Tags

func (o *TextOutput) Tags(colors ...string) string

Same as LightTags

Directories

Path Synopsis
cmd
bg command
blink command
canvas command
color command
key command
menu command
move command
ok command
resize command
rune command
shooter command
string command
waitkey command
widget command
writerune command

Jump to

Keyboard shortcuts

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