ansi

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2022 License: MIT Imports: 6 Imported by: 70

README

ansi

Implements the ANSI VT100 control set. Please refer to http://www.termsys.demon.co.uk/vtansi.htm

GoDoc

Install

go get github.com/jpillora/ansi

Usage

Get ANSI control code bytes:

ansi.Goto(2,4)
ansi.Set(ansi.Green, ansi.BlueBG)

Wrap an io.ReadWriteCloser:


a := ansi.Wrap(tcpConn)

//Read, Write, Close as normal
a.Read()
a.Write()
a.Close()

//Shorthand for a.Write(ansi.Set(..))
a.Set(ansi.Green, ansi.BlueBG)

//Send query
a.QueryCursorPosition()
//Await report
report := <- a.Reports
report.Type//=> ansi.Position
report.Pos.Row
report.Pos.Col

Wrapped connections will intercept and remove ANSI report codes from a.Read()

API

https://pkg.go.dev/github.com/jpillora/ansi?tab=doc

Documentation

Overview

Package ansi implements the ANSI VT100 control set. Please refer to http://www.termsys.demon.co.uk/vtansi.htm

Index

Constants

View Source
const Esc = byte(27)

Variables

View Source
var (
	ResetBytes      = Set(Reset)
	BrightBytes     = Set(Bright)
	DimBytes        = Set(Dim)
	ItalicBytes     = Set(Italic)
	UnderscoreBytes = Set(Underscore)
	BlinkBytes      = Set(Blink)
	ReverseBytes    = Set(Reverse)
	HiddenBytes     = Set(Hidden)
	//foreground colors
	BlackBytes   = Set(Black)
	RedBytes     = Set(Red)
	GreenBytes   = Set(Green)
	YellowBytes  = Set(Yellow)
	BlueBytes    = Set(Blue)
	MagentaBytes = Set(Magenta)
	CyanBytes    = Set(Cyan)
	WhiteBytes   = Set(White)
	//background colors
	BlackBGBytes   = Set(BlackBG)
	RedBGBytes     = Set(RedBG)
	GreenBGBytes   = Set(GreenBG)
	YellowBGBytes  = Set(YellowBG)
	BlueBGBytes    = Set(BlueBG)
	MagentaBGBytes = Set(MagentaBG)
	CyanBGBytes    = Set(CyanBG)
	WhiteBGBytes   = Set(WhiteBG)
)
View Source
var ClearAllTabs = []byte{Esc, '[', '3', 'g'}
View Source
var ClearTab = []byte{Esc, '[', 'g'}
View Source
var CursorHide = []byte{Esc, '[', '?', '2', '5', 'l'}
View Source
var CursorShow = []byte{Esc, '[', '?', '2', '5', 'h'}
View Source
var DisableLineWrap = []byte{Esc, '[', '7', 'l'}
View Source
var EnableLineWrap = []byte{Esc, '[', '7', 'h'}
View Source
var EraseDown = []byte{Esc, '[', 'J'}
View Source
var EraseEndLine = []byte{Esc, '[', 'K'}
View Source
var EraseLine = []byte{Esc, '[', '2', 'K'}
View Source
var EraseScreen = []byte{Esc, '[', '2', 'J'}
View Source
var EraseStartLine = []byte{Esc, '[', '1', 'K'}
View Source
var EraseUp = []byte{Esc, '[', '1', 'J'}
View Source
var FontSetG0 = []byte{Esc, '('}
View Source
var FontSetG1 = []byte{Esc, ')'}
View Source
var PrintLine = []byte{Esc, '[', '1', 'i'}
View Source
var PrintScreen = []byte{Esc, '[', 'i'}

Printing

View Source
var QueryCode = []byte{Esc, '[', 'c'}
View Source
var QueryCursorPosition = []byte{Esc, '[', '6', 'n'}
View Source
var QueryDeviceStatus = []byte{Esc, '[', '5', 'n'}
View Source
var ResetDevice = []byte{Esc, 'c'}
View Source
var RestoreAttrCursor = []byte{Esc, '8'}
View Source
var SaveAttrCursor = []byte{Esc, '7'}
View Source
var SaveCursor = []byte{Esc, '[', 's'}
View Source
var ScrollDown = []byte{Esc, 'D'}
View Source
var ScrollScreen = []byte{Esc, '[', 'r'}
View Source
var ScrollUp = []byte{Esc, 'M'}
View Source
var SetTab = []byte{Esc, 'H'}

Tab Control

View Source
var StartPrintLog = []byte{Esc, '[', '5', 'i'}
View Source
var StopPrintLog = []byte{Esc, '[', '4', 'i'}
View Source
var UnsaveCursor = []byte{Esc, '[', 'u'}

Functions

func Goto

func Goto(r, c uint16) []byte

Goto various positions: Cursor Home <ESC>[{ROW};{COLUMN}H Cursor Up <ESC>[{COUNT}A Cursor Down <ESC>[{COUNT}B Cursor Forward <ESC>[{COUNT}C Cursor Backward <ESC>[{COUNT}D Force Cursor Position <ESC>[{ROW};{COLUMN}f

func Scroll

func Scroll(start, end uint16) []byte

func Set

func Set(attrs ...Attribute) []byte

Set attributes

Types

type Ansi

type Ansi struct {
	Reports chan *Report
	// contains filtered or unexported fields
}

Ansi represents a wrapped io.ReadWriter. It will read the stream, parse and remove ANSI report codes and place them on the Reports queue.

func Wrap

func Wrap(rw io.ReadWriter) *Ansi

Wrap an io.ReadWriter (like a net.Conn) to easily read and write control codes

func (*Ansi) Close

func (a *Ansi) Close() error

Close the underlying ReadWriter

func (*Ansi) CursorHide

func (a *Ansi) CursorHide()

func (*Ansi) CursorShow

func (a *Ansi) CursorShow()

func (*Ansi) DisableLineWrap

func (a *Ansi) DisableLineWrap()

func (*Ansi) EnableLineWrap

func (a *Ansi) EnableLineWrap()

func (*Ansi) EraseScreen

func (a *Ansi) EraseScreen()

func (*Ansi) Goto

func (a *Ansi) Goto(r, c uint16)

func (*Ansi) QueryCursorPosition

func (a *Ansi) QueryCursorPosition()

func (*Ansi) Read

func (a *Ansi) Read(dest []byte) (n int, err error)

Reads the underlying ReadWriter

func (*Ansi) Set

func (a *Ansi) Set(attrs ...Attribute)

Set Attribute Mode <ESC>[{attr1};...;{attrn}m

func (*Ansi) Write

func (a *Ansi) Write(p []byte) (n int, err error)

Writes the underlying ReadWriter

type Attribute

type Attribute string

Sets multiple display attribute settings. The following lists standard attributes:

const (
	//formating
	Reset      Attribute = "0"
	Bright     Attribute = "1"
	Dim        Attribute = "2"
	Italic     Attribute = "3"
	Underscore Attribute = "4"
	Blink      Attribute = "5"
	Reverse    Attribute = "7"
	Hidden     Attribute = "8"
	//foreground colors
	Black   Attribute = "30"
	Red     Attribute = "31"
	Green   Attribute = "32"
	Yellow  Attribute = "33"
	Blue    Attribute = "34"
	Magenta Attribute = "35"
	Cyan    Attribute = "36"
	White   Attribute = "37"
	//background colors
	BlackBG   Attribute = "40"
	RedBG     Attribute = "41"
	GreenBG   Attribute = "42"
	YellowBG  Attribute = "43"
	BlueBG    Attribute = "44"
	MagentaBG Attribute = "45"
	CyanBG    Attribute = "46"
	WhiteBG   Attribute = "47"

	// Default Colors
	Default   Attribute = "39"
	DefaultBG Attribute = "49"
)

func (Attribute) Join added in v1.0.3

func (a Attribute) Join(s string, b Attribute) []byte

Wrap joins: attribute, string, another attribute

func (Attribute) String added in v1.0.3

func (a Attribute) String(s string) string

String joins: attribute, string, reset, and converts to a string

type Report

type Report struct {
	Type ReportType
	Code int
	Pos  struct {
		Row, Col int
	}
}

type ReportType

type ReportType int
const (
	Code ReportType = iota
	OK
	Failure
	Position
)

Jump to

Keyboard shortcuts

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