ansiterm

package
v6.14.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2015 License: MIT, Apache-2.0 Imports: 7 Imported by: 0

README

go-ansiterm

This is a cross platform Ansi Terminal Emulation library. It reads a stream of Ansi characters and produces the appropriate function calls. The results of the function calls are platform dependent.

For example the parser might receive "ESC, [, A" as a stream of three characters. This is the code for Cursor Up (http://www.vt100.net/docs/vt510-rm/CUU). The parser then calls the cursor up function (CUU()) on an event handler. The event handler determines what platform specific work must be done to cause the cursor to move up one position.

The parser (parser.go) is a partial implementation of this state machine (http://vt100.net/emu/vt500_parser.png). There are also two event handler implementations, one for tests (test_event_handler.go) to validate that the expected events are being produced and called, the other is a Windows implementation (winterm/win_event_handler.go).

See parser_test.go for examples exercising the state machine and generating appropriate function calls.

Documentation

Index

Constants

View Source
const (
	// ECMA-48 Set Graphics Rendition
	// Note:
	// -- Constants leading with an underscore (e.g., _ANSI_xxx) are unsupported or reserved
	// -- Fonts could possibly be supported via SetCurrentConsoleFontEx
	// -- Windows does not expose the per-window cursor (i.e., caret) blink times
	ANSI_SGR_RESET = 0
	ANSI_SGR_BOLD  = 1
	ANSI_SGR_DIM   = 2

	ANSI_SGR_UNDERLINE = 4

	ANSI_SGR_REVERSE = 7

	ANSI_SGR_BOLD_DIM_OFF = 22

	ANSI_SGR_UNDERLINE_OFF = 24

	ANSI_SGR_REVERSE_OFF = 27

	ANSI_SGR_FOREGROUND_BLACK   = 30
	ANSI_SGR_FOREGROUND_RED     = 31
	ANSI_SGR_FOREGROUND_GREEN   = 32
	ANSI_SGR_FOREGROUND_YELLOW  = 33
	ANSI_SGR_FOREGROUND_BLUE    = 34
	ANSI_SGR_FOREGROUND_MAGENTA = 35
	ANSI_SGR_FOREGROUND_CYAN    = 36
	ANSI_SGR_FOREGROUND_WHITE   = 37

	ANSI_SGR_FOREGROUND_DEFAULT = 39
	ANSI_SGR_BACKGROUND_BLACK   = 40
	ANSI_SGR_BACKGROUND_RED     = 41
	ANSI_SGR_BACKGROUND_GREEN   = 42
	ANSI_SGR_BACKGROUND_YELLOW  = 43
	ANSI_SGR_BACKGROUND_BLUE    = 44
	ANSI_SGR_BACKGROUND_MAGENTA = 45
	ANSI_SGR_BACKGROUND_CYAN    = 46
	ANSI_SGR_BACKGROUND_WHITE   = 47

	ANSI_SGR_BACKGROUND_DEFAULT = 49

	ANSI_MAX_CMD_LENGTH = 4096

	MAX_INPUT_EVENTS = 128
	DEFAULT_WIDTH    = 80
	DEFAULT_HEIGHT   = 24

	ANSI_BEL              = 0x07
	ANSI_BACKSPACE        = 0x08
	ANSI_TAB              = 0x09
	ANSI_LINE_FEED        = 0x0A
	ANSI_VERTICAL_TAB     = 0x0B
	ANSI_FORM_FEED        = 0x0C
	ANSI_CARRIAGE_RETURN  = 0x0D
	ANSI_ESCAPE_PRIMARY   = 0x1B
	ANSI_ESCAPE_SECONDARY = 0x5B
	ANSI_OSC_STRING_ENTRY = 0x5D
	ANSI_COMMAND_FIRST    = 0x40
	ANSI_COMMAND_LAST     = 0x7E
	DCS_ENTRY             = 0x90
	CSI_ENTRY             = 0x9B
	OSC_STRING            = 0x9D
	ANSI_PARAMETER_SEP    = ";"
	ANSI_CMD_G0           = '('
	ANSI_CMD_G1           = ')'
	ANSI_CMD_G2           = '*'
	ANSI_CMD_G3           = '+'
	ANSI_CMD_DECPNM       = '>'
	ANSI_CMD_DECPAM       = '='
	ANSI_CMD_OSC          = ']'
	ANSI_CMD_STR_TERM     = '\\'

	KEY_CONTROL_PARAM_2 = ";2"
	KEY_CONTROL_PARAM_3 = ";3"
	KEY_CONTROL_PARAM_4 = ";4"
	KEY_CONTROL_PARAM_5 = ";5"
	KEY_CONTROL_PARAM_6 = ";6"
	KEY_CONTROL_PARAM_7 = ";7"
	KEY_CONTROL_PARAM_8 = ";8"
	KEY_ESC_CSI         = "\x1B["
	KEY_ESC_N           = "\x1BN"
	KEY_ESC_O           = "\x1BO"

	FILL_CHARACTER = ' '
)

ANSI constants References: -- http://www.ecma-international.org/publications/standards/Ecma-048.htm -- http://man7.org/linux/man-pages/man4/console_codes.4.html -- http://manpages.ubuntu.com/manpages/intrepid/man4/console_codes.4.html -- http://en.wikipedia.org/wiki/ANSI_escape_code -- http://vt100.net/emu/dec_ansi_parser -- http://vt100.net/emu/vt500_parser.svg -- http://invisible-island.net/xterm/ctlseqs/ctlseqs.html -- http://www.inwap.com/pdp10/ansicode.txt

View Source
const LogEnv = "DEBUG_TERMINAL"

Variables

View Source
var Alphabetics = append(UpperCase, LowerCase...)

Alphabetics 40-7E hex (all of upper and lower case)

View Source
var CsiCollectables = append(getByteRange(0x30, 0x39), getByteRange(0x3B, 0x3F)...)
View Source
var CsiParams = getByteRange(0x30, 0x3F)

Parameters 30-3F hex 0123456789:;<=>? CSI Parameters 30-39, 3B hex 0123456789;

View Source
var EscapeIntermediateToGroundBytes = getByteRange(0x30, 0x7E)
View Source
var EscapeToGroundBytes = getEscapeToGroundBytes()
View Source
var Executors = getExecuteBytes()
View Source
var Intermeds = getByteRange(0x20, 0x2F)

SPACE 20+A0 hex Always and everywhere a blank space Intermediate 20-2F hex !"#$%&'()*+,-./

View Source
var LowerCase = getByteRange(0x60, 0x7E)

Lowercase 60-7E hex `abcdefghijlkmnopqrstuvwxyz{|}~

View Source
var Printables = getByteRange(0x20, 0x7F)
View Source
var ToGroundBytes = getToGroundBytes()
View Source
var UpperCase = getByteRange(0x40, 0x5F)

Uppercase 40-5F hex @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_

Functions

This section is empty.

Types

type AnsiContext

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

type AnsiEventHandler

type AnsiEventHandler interface {
	// Print
	Print(b byte) error

	// Execute C0 commands
	Execute(b byte) error

	// CUrsor Up
	CUU(int) error

	// CUrsor Down
	CUD(int) error

	// CUrsor Forward
	CUF(int) error

	// CUrsor Backward
	CUB(int) error

	// Cursor to Next Line
	CNL(int) error

	// Cursor to Previous Line
	CPL(int) error

	// Cursor Horizontal position Absolute
	CHA(int) error

	// Vertical line Position Absolute
	VPA(int) error

	// CUrsor Position
	CUP(int, int) error

	// Horizontal and Vertical Position (depends on PUM)
	HVP(int, int) error

	// Text Cursor Enable Mode
	DECTCEM(bool) error

	// Origin Mode
	DECOM(bool) error

	// 132 Column Mode
	DECCOLM(bool) error

	// Erase in Display
	ED(int) error

	// Erase in Line
	EL(int) error

	// Insert Line
	IL(int) error

	// Delete Line
	DL(int) error

	// Insert Character
	ICH(int) error

	// Delete Character
	DCH(int) error

	// Set Graphics Rendition
	SGR([]int) error

	// Pan Down
	SU(int) error

	// Pan Up
	SD(int) error

	// Device Attributes
	DA([]string) error

	// Set Top and Bottom Margins
	DECSTBM(int, int) error

	// Index
	IND() error

	// Reverse Index
	RI() error

	// Flush updates from previous commands
	Flush() error
}

type AnsiParser

type AnsiParser struct {
	CsiEntry           State
	CsiParam           State
	DcsEntry           State
	Escape             State
	EscapeIntermediate State
	Error              State
	Ground             State
	OscString          State
	// contains filtered or unexported fields
}

func CreateParser

func CreateParser(initialState string, evtHandler AnsiEventHandler) *AnsiParser

func (*AnsiParser) Parse

func (ap *AnsiParser) Parse(bytes []byte) (int, error)

type BaseState

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

func (BaseState) Enter

func (base BaseState) Enter() error

func (BaseState) Exit

func (base BaseState) Exit() error

func (BaseState) Handle

func (base BaseState) Handle(b byte) (s State, e error)

func (BaseState) Name

func (base BaseState) Name() string

func (BaseState) Transition

func (base BaseState) Transition(s State) error

type CsiEntryState

type CsiEntryState struct {
	BaseState
}

func (CsiEntryState) Enter

func (csiState CsiEntryState) Enter() error

func (CsiEntryState) Handle

func (csiState CsiEntryState) Handle(b byte) (s State, e error)

func (CsiEntryState) Transition

func (csiState CsiEntryState) Transition(s State) error

type CsiParamState

type CsiParamState struct {
	BaseState
}

func (CsiParamState) Handle

func (csiState CsiParamState) Handle(b byte) (s State, e error)

func (CsiParamState) Transition

func (csiState CsiParamState) Transition(s State) error

type DcsEntryState

type DcsEntryState struct {
	BaseState
}

type ErrorState

type ErrorState struct {
	BaseState
}

type EscapeIntermediateState

type EscapeIntermediateState struct {
	BaseState
}

func (EscapeIntermediateState) Handle

func (escState EscapeIntermediateState) Handle(b byte) (s State, e error)

func (EscapeIntermediateState) Transition

func (escState EscapeIntermediateState) Transition(s State) error

type EscapeState

type EscapeState struct {
	BaseState
}

func (EscapeState) Enter

func (escState EscapeState) Enter() error

func (EscapeState) Handle

func (escState EscapeState) Handle(b byte) (s State, e error)

func (EscapeState) Transition

func (escState EscapeState) Transition(s State) error

type GroundState

type GroundState struct {
	BaseState
}

func (GroundState) Handle

func (gs GroundState) Handle(b byte) (s State, e error)

type OscStringState

type OscStringState struct {
	BaseState
}

func (OscStringState) Handle

func (oscState OscStringState) Handle(b byte) (s State, e error)

type State

type State interface {
	Enter() error
	Exit() error
	Handle(byte) (State, error)
	Name() string
	Transition(State) error
}

type StateId

type StateId int

type TestAnsiEventHandler

type TestAnsiEventHandler struct {
	FunctionCalls []string
}

func CreateTestAnsiEventHandler

func CreateTestAnsiEventHandler() *TestAnsiEventHandler

func (*TestAnsiEventHandler) CHA

func (h *TestAnsiEventHandler) CHA(param int) error

func (*TestAnsiEventHandler) CNL

func (h *TestAnsiEventHandler) CNL(param int) error

func (*TestAnsiEventHandler) CPL

func (h *TestAnsiEventHandler) CPL(param int) error

func (*TestAnsiEventHandler) CUB

func (h *TestAnsiEventHandler) CUB(param int) error

func (*TestAnsiEventHandler) CUD

func (h *TestAnsiEventHandler) CUD(param int) error

func (*TestAnsiEventHandler) CUF

func (h *TestAnsiEventHandler) CUF(param int) error

func (*TestAnsiEventHandler) CUP

func (h *TestAnsiEventHandler) CUP(x int, y int) error

func (*TestAnsiEventHandler) CUU

func (h *TestAnsiEventHandler) CUU(param int) error

func (*TestAnsiEventHandler) DA

func (h *TestAnsiEventHandler) DA(params []string) error

func (*TestAnsiEventHandler) DCH

func (h *TestAnsiEventHandler) DCH(param int) error

func (*TestAnsiEventHandler) DECCOLM

func (h *TestAnsiEventHandler) DECCOLM(use132 bool) error

func (*TestAnsiEventHandler) DECOM

func (h *TestAnsiEventHandler) DECOM(visible bool) error

func (*TestAnsiEventHandler) DECSTBM

func (h *TestAnsiEventHandler) DECSTBM(top int, bottom int) error

func (*TestAnsiEventHandler) DECTCEM

func (h *TestAnsiEventHandler) DECTCEM(visible bool) error

func (*TestAnsiEventHandler) DL

func (h *TestAnsiEventHandler) DL(param int) error

func (*TestAnsiEventHandler) ED

func (h *TestAnsiEventHandler) ED(param int) error

func (*TestAnsiEventHandler) EL

func (h *TestAnsiEventHandler) EL(param int) error

func (*TestAnsiEventHandler) Execute

func (h *TestAnsiEventHandler) Execute(b byte) error

func (*TestAnsiEventHandler) Flush

func (h *TestAnsiEventHandler) Flush() error

func (*TestAnsiEventHandler) HVP

func (h *TestAnsiEventHandler) HVP(x int, y int) error

func (*TestAnsiEventHandler) ICH

func (h *TestAnsiEventHandler) ICH(param int) error

func (*TestAnsiEventHandler) IL

func (h *TestAnsiEventHandler) IL(param int) error

func (*TestAnsiEventHandler) IND

func (h *TestAnsiEventHandler) IND() error

func (*TestAnsiEventHandler) Print

func (h *TestAnsiEventHandler) Print(b byte) error

func (*TestAnsiEventHandler) RI

func (h *TestAnsiEventHandler) RI() error

func (*TestAnsiEventHandler) SD

func (h *TestAnsiEventHandler) SD(param int) error

func (*TestAnsiEventHandler) SGR

func (h *TestAnsiEventHandler) SGR(params []int) error

func (*TestAnsiEventHandler) SU

func (h *TestAnsiEventHandler) SU(param int) error

func (*TestAnsiEventHandler) VPA

func (h *TestAnsiEventHandler) VPA(param int) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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