govterm

package module
v0.0.0-...-38a59a6 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

README

govterm

Simple VT-compatible Linux Terminal Emulator

Licence Golang


中文

Introduction

govterm is a pyte-like Linux terminal emulator that simulates a terminal in memory and can be used to parse and process terminal output without the need for an actual physical terminal.

Fork from go-asiterm

Core functions

  • Screen simulation includes a screen emulator that can handle the character stream on the screen, supporting operations such as cursor movement and text scrolling.

  • ANSI control code interpretation handles ANSI control code sequence escapes.

Download

go get github.com/kinabcd/govterm

Use

# Create a virtual screen
screen := govterm.NewScreen(80, 24)

# Create a character stream
stream := govterm.NewStream(screen)

# Input characters
stream.WriteString(input)

# Get screen output
output := screen.Display()

For more usage examples, see example

Documentation

Index

Constants

View Source
const (
	SP    = " "
	NUL   = "\x00"
	BEL   = "\x07"
	BS    = "\x08"
	HT    = "\x09"
	LF    = "\n"
	VT    = "\x0b"
	FF    = "\x0c"
	CR    = "\r"
	SO    = "\x0e"
	SI    = "\x0f"
	CAN   = "\x18"
	SUB   = "\x1a"
	ESC   = "\x1b"
	DEL   = "\x7f"
	CSIC0 = ESC + "["
	CSIC1 = "\x9b"
	CSI   = CSIC0
	STC0  = ESC + "\\"
	STC1  = "\x9c"
	ST    = STC0
	OSCC0 = ESC + "]"
	OSCC1 = "\x9d"
	OSC   = OSCC0
)
View Source
const (
	RegSP    = ` `
	RegNUL   = `\x00`
	RegBEL   = `\x07`
	RegBS    = `\x08`
	RegHT    = `\x09`
	RegLF    = `\n`
	RegVT    = `\x0b`
	RegFF    = `\x0c`
	RegCR    = `\r`
	RegSO    = `\x0e`
	RegSI    = `\x0f`
	RegCAN   = `\x18`
	RegSUB   = `\x1a`
	RegESC   = `\x1b`
	RegDEL   = `\x7f`
	RegCSIC0 = RegESC + `[`
	RegCSIC1 = `\x9b`
	RegCSI   = RegCSIC0
	RegSTC0  = RegESC + `\\`
	RegSTC1  = `\x9c`
	RegST    = RegSTC0
	RegOSCC0 = RegESC + `]`
	RegOSCC1 = `\x9d`
	RegOSC   = RegOSCC0
)
View Source
const (
	// Basic control sequences
	RIS    = "c"
	IND    = "D"
	NEL    = "E"
	HTS    = "H"
	RI     = "M"
	DECSC  = "7"
	DECRC  = "8"
	DECALN = "8"

	// ECMA-48 CSI sequences
	ICH     = "@"
	CUU     = "A"
	CUD     = "B"
	CUF     = "C"
	CUB     = "D"
	CNL     = "E"
	CPL     = "F"
	CHA     = "G"
	CUP     = "H"
	ED      = "J"
	EL      = "K"
	IL      = "L"
	DL      = "M"
	DCH     = "P"
	ECH     = "X"
	HPR     = "a"
	DA      = "c"
	VPA     = "d"
	VPR     = "e"
	HVP     = "f"
	TBC     = "g"
	SM      = "h"
	RM      = "l"
	SGR     = "m"
	DSR     = "n"
	DECSTBM = "r"
	HPA     = "'"
)
View Source
const (
	IRM     = 4
	LNM     = 20
	DECCOLM = 96
	DECSCNM = 160
	DECOM   = 192
	DECAWM  = 224
	DECTCEM = 800
)
View Source
const (
	Bell             = "bell"
	Backspace        = "backspace"
	Tab              = "tab"
	Linefeed         = "linefeed"
	CarriageReturn   = "carriage_return"
	ShiftOut         = "shift_out"
	ShiftIn          = "shift_in"
	Reset            = "reset"
	Index            = "index"
	ReverseIndex     = "reverse_index"
	SetTabStop       = "set_tab_stop"
	SaveCursor       = "save_cursor"
	RestoreCursor    = "restore_cursor"
	AlignmentDisplay = "alignment_display"
)
View Source
const BG_256 = 48
View Source
const FG_256 = 38

Variables

View Source
var (
	LAT1Chars  = []rune{}
	VT100Chars = []rune{}/* 256 elements not displayed */

	IBMPCChars = []rune{}/* 256 elements not displayed */

	VAX42Chars = []rune{}/* 256 elements not displayed */

	CHARMAPS = map[string][]rune{
		"B": LAT1Chars,
		"0": VT100Chars,
		"U": IBMPCChars,
		"V": VAX42Chars,
	}
)
View Source
var (
	Basic = map[string]struct{}{
		BEL: {},
		BS:  {},
		HT:  {},
		LF:  {},
		VT:  {},
		FF:  {},
		CR:  {},
		SO:  {},
		SI:  {},
	}
	Escape = map[string]struct{}{
		RIS:   {},
		IND:   {},
		NEL:   {},
		RI:    {},
		HTS:   {},
		DECSC: {},
		DECRC: {},
	}
	Sharp = map[string]struct{}{
		DECALN: {},
	}
	Csi = map[string]struct{}{
		ICH:     {},
		CUU:     {},
		CUD:     {},
		CUF:     {},
		HPR:     {},
		CUB:     {},
		CNL:     {},
		CPL:     {},
		CHA:     {},
		HPA:     {},
		CUP:     {},
		HVP:     {},
		ED:      {},
		EL:      {},
		IL:      {},
		DL:      {},
		DCH:     {},
		ECH:     {},
		DA:      {},
		DSR:     {},
		VPA:     {},
		TBC:     {},
		SM:      {},
		RM:      {},
		SGR:     {},
		DECSTBM: {},
	}
)
View Source
var BG = BG_ANSI

BG - Alias to BG_ANSI for compatibility

View Source
var BG_AIXTERM = map[int]string{
	100: "brightblack",
	101: "brightred",
	102: "brightgreen",
	103: "brightbrown",
	104: "brightblue",
	105: "brightmagenta",
	106: "brightcyan",
	107: "brightwhite",
}

BG_AIXTERM - Mapping of non-standard aixterm background color codes

View Source
var BG_ANSI = map[int]string{
	40: "black",
	41: "red",
	42: "green",
	43: "brown",
	44: "blue",
	45: "magenta",
	46: "cyan",
	47: "white",
	49: "default",
}

BG_ANSI - Mapping of ANSI background color codes to color names

View Source
var (
	DefaultMode = map[int]struct{}{
		DECCOLM: {},
		DECTCEM: {},
	}
)
View Source
var FG = FG_ANSI

FG - Alias to FG_ANSI for compatibility

View Source
var FG_AIXTERM = map[int]string{
	90: "brightblack",
	91: "brightred",
	92: "brightgreen",
	93: "brightbrown",
	94: "brightblue",
	95: "brightmagenta",
	96: "brightcyan",
	97: "brightwhite",
}

FG_AIXTERM - Mapping of non-standard aixterm foreground color codes

View Source
var FG_ANSI = map[int]string{
	30: "black",
	31: "red",
	32: "green",
	33: "brown",
	34: "blue",
	35: "magenta",
	36: "cyan",
	37: "white",
	39: "default",
}
View Source
var FG_BG_256 = []string{}/* 256 elements not displayed */
View Source
var TEXT = map[int]string{
	1:  "+bold",
	3:  "+italics",
	4:  "+underscore",
	5:  "+blink",
	7:  "+reverse",
	9:  "+strikethrough",
	22: "-bold",
	23: "-italics",
	24: "-underscore",
	25: "-blink",
	27: "-reverse",
	29: "-strikethrough",
}

Functions

func BytesToString

func BytesToString(data []byte) string

func DecodeUTF8WithReplacement

func DecodeUTF8WithReplacement(data []byte) (string, error)

func Pop

func Pop[T int | Savepoint](slice *[]T) (T, bool)

func Translate

func Translate(s string, m []rune) string

Types

type ByteStream

type ByteStream struct {
	*Stream
	// contains filtered or unexported fields
}

func NewByteStream

func NewByteStream(stream *Stream) *ByteStream

func (*ByteStream) Write

func (b *ByteStream) Write(data []byte) (n int, err error)

type Char

type Char struct {
	Data          string
	Fg            string
	Bg            string
	Bold          bool
	Italics       bool
	Underscore    bool
	Strikethrough bool
	Reverse       bool
	Blink         bool
}

func NewChar

func NewChar(data string) Char

func (*Char) Map

func (c *Char) Map() (data map[string]any)

func (*Char) Update

func (c *Char) Update(data map[string]any)

type Cursor

type Cursor struct {
	X      int
	Y      int
	Attrs  Char
	Hidden bool
}

func NewCursor

func NewCursor(x int, y int, attrs Char) Cursor

NewCursor creates a new Cursor with the specified position and attributes.

type Margins

type Margins struct {
	Top    int
	Bottom int
}

type Savepoint

type Savepoint struct {
	Cursor    Cursor
	G0Charset []rune
	G1Charset []rune
	Charset   int
	Origin    bool
	Wrap      bool
}

type Screen

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

func NewScreen

func NewScreen(columns int, lines int) *Screen

func (*Screen) AlignmentDisplay

func (s *Screen) AlignmentDisplay()

func (*Screen) Backspace

func (s *Screen) Backspace()

func (*Screen) Bell

func (s *Screen) Bell(args ...any)

func (*Screen) CarriageReturn

func (s *Screen) CarriageReturn()

func (*Screen) ClearTabStop

func (s *Screen) ClearTabStop(how int)

func (*Screen) CursorBack

func (s *Screen) CursorBack(count int)

func (*Screen) CursorDown

func (s *Screen) CursorDown(count int)

func (*Screen) CursorDown1

func (s *Screen) CursorDown1(count int)

func (*Screen) CursorForward

func (s *Screen) CursorForward(count int)

func (*Screen) CursorPosition

func (s *Screen) CursorPosition(line, column int)

func (*Screen) CursorToColumn

func (s *Screen) CursorToColumn(column int)

func (*Screen) CursorToLine

func (s *Screen) CursorToLine(line int)

func (*Screen) CursorUp

func (s *Screen) CursorUp(count int)

func (*Screen) CursorUp1

func (s *Screen) CursorUp1(count int)

func (*Screen) DefaultChar

func (s *Screen) DefaultChar() Char

func (*Screen) DeleteCharacters

func (s *Screen) DeleteCharacters(count int)

func (*Screen) DeleteLines

func (s *Screen) DeleteLines(count int)

func (*Screen) Display

func (s *Screen) Display() []string

func (*Screen) Draw

func (s *Screen) Draw(data string)

func (*Screen) EnsureHBounds

func (s *Screen) EnsureHBounds()

func (*Screen) EnsureVBounds

func (s *Screen) EnsureVBounds(useMargins bool)

EnsureVBounds ensures the cursor is within vertical screen bounds.

func (*Screen) EraseCharacters

func (s *Screen) EraseCharacters(count int)

func (*Screen) EraseInDisplay

func (s *Screen) EraseInDisplay(how int)

func (*Screen) EraseInLine

func (s *Screen) EraseInLine(how int)

func (*Screen) Index

func (s *Screen) Index()

func (*Screen) InsertCharacters

func (s *Screen) InsertCharacters(count int)

func (*Screen) InsertLines

func (s *Screen) InsertLines(count int)

func (*Screen) LineFeed

func (s *Screen) LineFeed()

func (*Screen) ReportDeviceAttributes

func (s *Screen) ReportDeviceAttributes(mode int, kw map[string]bool)

func (*Screen) ReportDeviceStatus

func (s *Screen) ReportDeviceStatus(mode int, kw map[string]bool)

func (*Screen) Reset

func (s *Screen) Reset()

func (*Screen) ResetMode

func (s *Screen) ResetMode(modes []int, kw map[string]any)

func (*Screen) Resize

func (s *Screen) Resize(lines, columns int)

func (*Screen) RestoreCursor

func (s *Screen) RestoreCursor()

func (*Screen) ReverseIndex

func (s *Screen) ReverseIndex()

func (*Screen) SaveCursor

func (s *Screen) SaveCursor()

func (*Screen) SelectGraphicRendition

func (s *Screen) SelectGraphicRendition(attrs ...int)

func (*Screen) SetMargins

func (s *Screen) SetMargins(top, bottom int)

func (*Screen) SetMode

func (s *Screen) SetMode(modes []int, kw map[string]any)

func (*Screen) SetTabStop

func (s *Screen) SetTabStop()

func (*Screen) ShiftIn

func (s *Screen) ShiftIn()

func (*Screen) ShiftOut

func (s *Screen) ShiftOut()

func (*Screen) String

func (s *Screen) String() string

func (*Screen) Tab

func (s *Screen) Tab()

func (*Screen) WriteProcessInput

func (s *Screen) WriteProcessInput(data string)

WriteProcessInput default is a noop data text to write to the process stdin

type ScreenBuffer

type ScreenBuffer struct {
	Map         map[int]*StaticDefaultDict[int, Char]
	DefaultChar Char
}

func NewScreenBuffer

func NewScreenBuffer(defaultVal Char) *ScreenBuffer

func (*ScreenBuffer) Clear

func (db *ScreenBuffer) Clear()

func (*ScreenBuffer) Delete

func (db *ScreenBuffer) Delete(key int)

func (*ScreenBuffer) Get

func (db *ScreenBuffer) Get(key int) *StaticDefaultDict[int, Char]

func (*ScreenBuffer) GetValue

func (db *ScreenBuffer) GetValue(key int) StaticDefaultDict[int, Char]

func (*ScreenBuffer) HasKey

func (db *ScreenBuffer) HasKey(key int) bool

func (*ScreenBuffer) Set

func (db *ScreenBuffer) Set(key int, value StaticDefaultDict[int, Char])

type StaticDefaultDict

type StaticDefaultDict[KT comparable, VT any] struct {
	Data       map[KT]VT
	DefaultVal VT
}

func NewStaticDefaultDict

func NewStaticDefaultDict[KT comparable, VT any](defaultVal VT) *StaticDefaultDict[KT, VT]

NewStaticDefaultDict creates a new StaticDefaultDict with the provided default value.

func (*StaticDefaultDict[KT, VT]) Del

func (sd *StaticDefaultDict[KT, VT]) Del(key KT)

func (*StaticDefaultDict[KT, VT]) Get

func (sd *StaticDefaultDict[KT, VT]) Get(key KT) VT

func (*StaticDefaultDict[KT, VT]) Set

func (sd *StaticDefaultDict[KT, VT]) Set(key KT, value VT)

type Stream

type Stream struct {
	UseUTF8         bool
	TakingPlainText bool
	Basic           map[string]struct{}
	Escape          map[string]struct{}
	Sharp           map[string]struct{}
	Csi             map[string]struct{}
	TextPattern     *regexp.Regexp
	// contains filtered or unexported fields
}

func NewStream

func NewStream(screen *Screen) *Stream

func (*Stream) HandleBasic

func (s *Stream) HandleBasic(char string)

func (*Stream) HandleCSI

func (s *Stream) HandleCSI(char string, params []int, kw map[string]any)

func (*Stream) HandleEscape

func (s *Stream) HandleEscape(char string)

func (*Stream) HandleSharp

func (s *Stream) HandleSharp(char string)

func (*Stream) WriteString

func (s *Stream) WriteString(data string) (n int, err error)

Jump to

Keyboard shortcuts

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