term

package
v0.0.0-...-555d40f Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package term implements a subset of the C termios library to interface with Terminals.

This package allows the caller to get and set most Terminal capabilites and sizes as well as create PTYs to enable writing things like script, screen, tmux, and expect.

The Termios type is used for setting/getting Terminal capabilities while the PTY type is used for handling virtual terminals.

Currently this part of this lib is Linux specific.

Also implements a simple version of readline in pure Go and some Stringers for terminal colors and attributes.

Index

Examples

Constants

View Source
const (
	CSI       = "\033["
	FgBlack   = "30"
	FgRed     = "31"
	FgGreen   = "32"
	FgYellow  = "33"
	FgBlue    = "34"
	FgMagenta = "35"
	FgCyan    = "36"
	FgWhite   = "37"
	FgDefault = "39"
	F256      = "38"
	BgBlack   = "40"
	BgRed     = "41"
	BgGreen   = "42"
	BgYellow  = "43"
	BgBlue    = "44"
	BgMagenta = "45"
	BgCyan    = "46"
	BgWhite   = "47"
	BgDefault = "49"
	Bg256     = "48"
	Blink     = "5"
	Ital      = "3"
	Underln   = "4"
	Faint     = "2"
	Bld       = "1"
	NoMode    = "0"
)

Terminal Color and modifier codes

View Source
const (
	TCGETS     = 0x5401     // TCGETS get terminal attributes
	TCSETS     = 0x5402     // TCSETS set terminal attributes
	TIOCGWINSZ = 0x5413     // TIOCGWINSZ used to get the terminal window size
	TIOCSWINSZ = 0x5414     // TIOCSWINSZ used to set the terminal window size
	TIOCGPTN   = 0x80045430 // TIOCGPTN IOCTL used to get the PTY number
	TIOCSPTLCK = 0x40045431 // TIOCSPTLCK IOCT used to lock/unlock PTY
	CBAUD      = 0010017    // CBAUD Serial speed settings
	CBAUDEX    = 0010000    // CBAUDX Serial speed settings
)

IOCTL terminal stuff.

View Source
const (
	IGNBRK  = 0000001 // IGNBRK ignore break characters
	BRKINT  = 0000002 // BRKINT Break genereates an interrupt signal
	IGNPAR  = 0000004 // IGNPAR Ignore characters with parity errors
	PARMRK  = 0000010 // PARMRK Mark parity errors byte{ff,0}
	INPCK   = 0000020 // INPCK enable parity checking
	ISTRIP  = 0000040 // ISTRIP Clear 8th bit of input characters
	INLCR   = 0000100 // INLCR Translate LF => CR
	IGNCR   = 0000200 // IGNCR Ignore Carriage Return
	ICRNL   = 0000400 // ICRNL Translate CR => NL
	IUCLC   = 0001000 // IUCLC Translate uppercase to lowercase
	IXON    = 0002000 // IXON Enable flow control
	IXANY   = 0004000 // IXANY let any char restart input
	IXOFF   = 0010000 // IXOFF start sending start/stop chars
	IMAXBEL = 0020000 // IMAXBEL Sound the bell and skip flushing input buffer
	IUTF8   = 0040000 // IUTF8 assume input being utf-8
)

INPUT handling terminal flags see 'man stty' for further info about most of the constants

View Source
const (
	OPOST  = 0000001 // OPOST post process output
	OLCUC  = 0000002 // OLCUC translate lower case to upper case
	ONLCR  = 0000004 // ONLCR Map NL -> CR-NL
	OCRNL  = 0000010 // OCRNL Map CR -> NL
	ONOCR  = 0000020 // ONOCR No CR at col 0
	ONLRET = 0000040 // ONLRET NL also do CR
	OFILL  = 0000100 // OFILL Fillchar for delay
	OFDEL  = 0000200 // OFDEL use delete instead of null
)

OUTPUT treatment terminal flags

View Source
const (
	CSIZE  = 0000060 // CSIZE used as mask when setting character size
	CS5    = 0000000 // CS5 char size 5bits
	CS6    = 0000020 // CS6 char size 6bits
	CS7    = 0000040 // CS7 char size 7bits
	CS8    = 0000060 // CS8 char size 8bits
	CSTOPB = 0000100 // CSTOPB two stop bits
	CREAD  = 0000200 // CREAD enable input
	PARENB = 0000400 // PARENB generate and expect parity bit
	PARODD = 0001000 // PARODD set odd parity
	HUPCL  = 0002000 // HUPCL send HUP when last process closes term
	CLOCAL = 0004000 // CLOCAL no modem control signals
)

TERM control modes.

View Source
const (
	ISIG    = 0000001 // ISIG enable Interrupt,quit and suspend chars
	ICANON  = 0000002 // ICANON enable erase,kill ,werase and rprnt chars
	XCASE   = 0000004 // XCASE preceedes all uppercase chars with '\'
	ECHO    = 0000010 // ECHO echo input characters
	ECHOE   = 0000020 // ECHOE erase => BS - SPACE - BS
	ECHOK   = 0000040 // ECHOK add newline after kill char
	ECHONL  = 0000100 // ECHONL echo NL even without other characters
	NOFLSH  = 0000200 // NOFLSH no flush after interrupt and kill characters
	TOSTOP  = 0000400 // TOSTOP stop BG jobs trying to write to term
	ECHOCTL = 0001000 // ECHOCTL will echo control characters as ^c
	ECHOPRT = 0002000 // ECHOPRT will print erased characters between \ /
	ECHOKE  = 0004000 // ECHOKE kill all line considering ECHOPRT and ECHOE flags
	IEXTEN  = 0100000 // IEXTEN enable non POSIX special characters
)

TERM modes

View Source
const (
	VINTR    = 0  // VINTR 		char will send an interrupt signal
	VQUIT    = 1  // VQUIT 		char will send a quit signal
	VERASE   = 2  // VEREASE 	char will erase last typed char
	VKILL    = 3  // VKILL 		char will erase current line
	VEOF     = 4  // VEOF 		char will send EOF
	VTIME    = 5  // VTIME 		set read timeout in tenths of seconds
	VMIN     = 6  // VMIN 		set min characters for a complete read
	VSWTC    = 7  // VSWTC 		char will switch to a different shell layer
	VSTART   = 8  // VSTART 	char will restart output after stopping it
	VSTOP    = 9  // VSTOP 		char will stop output
	VSUSP    = 10 // VSUSP 		char will send a stop signal
	VEOL     = 11 // VEOL 		char will end the line
	VREPRINT = 12 // VREPRINT will redraw the current line
	VDISCARD = 13 // VDISCARD
	VWERASE  = 14 // VWERASE 	char will erase last word typed
	VLNEXT   = 15 // VLNEXT 	char will enter the next char quoted
	VEOL2    = 16 // VEOL2 		char alternate to end line

)

Control characters

Variables

This section is empty.

Functions

func BBlackf

func BBlackf(format string, a ...interface{}) string

BBlackf returns a BBlack formatted string.

func BBluef

func BBluef(format string, a ...interface{}) string

BBluef returns a BBlue formatted string.

func BCyanf

func BCyanf(format string, a ...interface{}) string

BCyanf returns a BCyan formatted string.

func BGreenf

func BGreenf(format string, a ...interface{}) string

BGreenf returns a BGreen formatted string.

func BMagentaf

func BMagentaf(format string, a ...interface{}) string

BMagentaf returns a BMagenta formatted string.

func BRedf

func BRedf(format string, a ...interface{}) string

BRedf returns a BRed formatted string.

func BWhitef

func BWhitef(format string, a ...interface{}) string

BWhitef returns a BWhite formatted string.

func BYellowf

func BYellowf(format string, a ...interface{}) string

BYellowf returns a BYellow formatted string.

func Blackf

func Blackf(format string, a ...interface{}) string

Blackf returns a Black formatted string.

func Bluef

func Bluef(format string, a ...interface{}) string

Bluef returns a Blue formatted string.

func ColorDisable

func ColorDisable()

ColorDisable disables the terminal colors.

func ColorEnable

func ColorEnable()

ColorEnable activates the terminal colors , this is the default.

func Cyanf

func Cyanf(format string, a ...interface{}) string

Cyanf returns a Cyan formatted string.

func GetChar

func GetChar(f *os.File) (b byte, err error)

GetChar reads a single byte.

func GetPass

func GetPass(prompt string, f *os.File, pbuf []byte) ([]byte, error)

GetPass reads password from a TTY with no echo.

func Greenf

func Greenf(format string, a ...interface{}) string

Greenf returns a Green formatted string.

func Isatty

func Isatty(file *os.File) bool

Isatty returns true if file is a tty.

func Magentaf

func Magentaf(format string, a ...interface{}) string

Magentaf returns a Magenta formatted string.

func Redf

func Redf(format string, a ...interface{}) string

Redf returns a Red formatted string.

func TestTerm

func TestTerm() string

TestTerm tries out most of the functions in this package and return a colourful string. Could be used to check what your terminal supports.

func Whitef

func Whitef(format string, a ...interface{}) string

Whitef returns a White formatted string.

func Yellowf

func Yellowf(format string, a ...interface{}) string

Yellowf returns a Yellow formatted string.

Types

type BBlack

type BBlack string

BBlack implements the Stringer interface to print string background in Black color.

func (BBlack) String

func (c BBlack) String() string

String implements the Stringer interface for type BBlack.

type BBlue

type BBlue string

BBlue implements the Stringer interface to print string background in Blue color.

func (BBlue) String

func (c BBlue) String() string

String implements the Stringer interface for type BBlue.

type BCyan

type BCyan string

BCyan implements the Stringer interface to print string background in Cyan color.

func (BCyan) String

func (c BCyan) String() string

String implements the Stringer interface for type BCyan.

type BGreen

type BGreen string

BGreen implements the Stringer interface to print string background in Green color.

func (BGreen) String

func (c BGreen) String() string

String implements the Stringer interface for type BGreen.

type BMagenta

type BMagenta string

BMagenta implements the Stringer interface to print string background in Magenta color.

func (BMagenta) String

func (c BMagenta) String() string

String implements the Stringer interface for type BMagenta.

type BRandom

type BRandom string

BRandom implements the Stringer interface to print string background in Random color.

func (BRandom) String

func (c BRandom) String() string

String gives a random bg color everytime it's printed.

type BRed

type BRed string

BRed implements the Stringer interface to print string background in Red color.

func (BRed) String

func (c BRed) String() string

String implements the Stringer interface for type BRed.

type BWhite

type BWhite string

BWhite implements the Stringer interface to print string background in White color.

func (BWhite) String

func (c BWhite) String() string

String implements the Stringer interface for type BWhite.

type BYellow

type BYellow string

BYellow implements the Stringer interface to print string background in Yellow color.

func (BYellow) String

func (c BYellow) String() string

String implements the Stringer interface for type BYellow.

type Black

type Black string

Black implements the Stringer interface to print string foreground in Black color.

func (Black) String

func (c Black) String() string

String implements the Stringer interface for type Black.

type Blinking

type Blinking string

Blinking implements the Stringer interface to print string in Blinking mode.

func (Blinking) String

func (c Blinking) String() string

String implements the Stringer interface for type Blinking.

type Blue

type Blue string

Blue implements the Stringer interface to print string foreground in Blue color.

func (Blue) String

func (c Blue) String() string

String implements the Stringer interface for type Blue.

type Bold

type Bold string

Bold implements the Stringer interface to print string in Bold mode.

func (Bold) String

func (c Bold) String() string

String implements the Stringer interface for type Bold.

type Color

type Color string

Color is the type returned by the colour setters to print any terminal colour.

Example

ExampleColor()

g := Green("Green world")
fmt.Println("Hello", g)
fmt.Println(Red("Warning!"))
if string(g) == "Green world" {
	fmt.Println(Blinking(string(g)))
}
var col fmt.Stringer
atk := 20
switch {
case atk == 0:
	col = Blue("5 FADE OUT")
case atk < 4:
	col = Green("4 DOUBLE TAKE")
case atk < 10:
	col = Yellow("3 ROUND HOUSE")
case atk < 50:
	col = Red("2 FAST PACE")
case atk >= 50:
	col = Blinking("1 COCKED PISTOL")
}
fmt.Println("Defcon: ", col)
Output:

Hello �[32mGreen world�[39m
�[31mWarning!�[39m
�[5mGreen world�[0m
Defcon:  �[31m2 FAST PACE�[39m

func NewColor

func NewColor(str string, fg string, bg string) (Color, error)

NewColor gives a type Color back with specified fg/bg colors set that can be printed with anything using the Stringer iface.

func NewColor256

func NewColor256(str string, fg string, bg string) (Color, error)

NewColor256 gives a type Color back using Term 256 color that can be printed with anything using the Stringer iface.

func NewColorRGB

func NewColorRGB(str string, red uint8, green uint8, blue uint8) Color

NewColorRGB takes R G B and returns a ColorRGB type that can be printed by anything using the Stringer iface. Only Konsole to my knowledge that supports 24bit color

func NewCombo

func NewCombo(s string, mods ...string) Color

NewCombo Takes a combination of modes and return a string with them all combined.

func (Color) String

func (c Color) String() string

String the stringer interface for all base color types.

type Color256Random

type Color256Random string

Color256Random implements the Stringer interface to print string random 256 color Term style.

type ColorRandom

type ColorRandom string

ColorRandom implements the Stringer interface to print string Random color.

func (ColorRandom) String

func (c ColorRandom) String() string

String is a random color stringer.

type Cyan

type Cyan string

Cyan implements the Stringer interface to print string foreground in Cyan color.

func (Cyan) String

func (c Cyan) String() string

String implements the Stringer interface for type Cyan.

type Green

type Green string

Green implements the Stringer interface to print string foreground in Green color.

func (Green) String

func (c Green) String() string

String implements the Stringer interface for type Green.

type Italic

type Italic string

Italic implements the Stringer interface to print string foreground in Italic color.

func (Italic) String

func (c Italic) String() string

String implements the Stringer interface for type Italic.

type Magenta

type Magenta string

Magenta implements the Stringer interface to print string foreground in Magenta color.

func (Magenta) String

func (c Magenta) String() string

String implements the Stringer interface for type Magenta.

type PTY

type PTY struct {
	Master *os.File // Master The Master part of the PTY
	Slave  *os.File // Slave The Slave part of the PTY
}

PTY the PTY Master/Slave are always bundled together so makes sense to bundle here too.

Slave - implements the virtual terminal functionality and the place you connect client applications Master - Things written to the Master are forwarded to the Slave terminal and the other way around.

This gives reading from Master would give you nice line-by-line with no strange characters in
Cooked() Mode and every char in Raw() mode.

Since Slave is a virtual terminal it depends on the terminal settings ( in this lib the Termios ) what and when data is forwarded through the terminal.

See 'man pty' for further info

func OpenPTY

func OpenPTY() (*PTY, error)

OpenPTY Creates a new Master/Slave PTY pair.

func (*PTY) Close

func (p *PTY) Close() error

Close closes the PTYs that OpenPTY created.

func (*PTY) GetChar

func (p *PTY) GetChar() (byte, error)

GetChar fine old getchar() for a PTY.

func (*PTY) PTSName

func (p *PTY) PTSName() (string, error)

PTSName return the name of the pty.

func (*PTY) PTSNumber

func (p *PTY) PTSNumber() (uint, error)

PTSNumber return the pty number.

func (*PTY) ReadByte

func (p *PTY) ReadByte() (byte, error)

ReadByte implements the io.ByteReader interface to read single char from the PTY.

type Random

type Random string

Random implements the Stringer interface to print string foreground in Random color.

func (Random) String

func (c Random) String() string

String gives a random fg color everytime it's printed.

type Red

type Red string

Red implements the Stringer interface to print string foreground in Red color.

func (Red) String

func (c Red) String() string

String implements the Stringer interface for type Red.

type Termios

type Termios struct {
	Iflag  uint32      // Iflag Handles the different Input modes
	Oflag  uint32      // Oflag For the different Output modes
	Cflag  uint32      // Cflag Control modes
	Lflag  uint32      // Lflag Local modes
	Line   byte        // Line
	Cc     [tNCCS]byte // Cc Control characters. How to handle special Characters eg. Backspace being ^H or ^? and so on
	Ispeed uint32      // Ispeed Hardly ever used speed of terminal
	Ospeed uint32      // Ospeed "
	Wz     Winsize     // Wz Terminal size information.
}

Termios merge of the C Terminal and Kernel termios structs.

func Attr

func Attr(file *os.File) (Termios, error)

Attr Gets (terminal related) attributes from file.

func (*Termios) Cook

func (t *Termios) Cook()

Cook Set the Terminal to Cooked mode. In this mode the Terminal process the information before sending it on to the application.

func (*Termios) FromSSH

func (t *Termios) FromSSH(termModes map[uint8]uint32)

FromSSH converts SSH attributes to Termios attributes.

func (*Termios) Raw

func (t *Termios) Raw()

Raw Sets terminal t to raw mode. This gives that the terminal will do the absolut minimal of processing, pretty much send everything through. This is normally what Shells and such want since they have their own readline and movement code.

func (*Termios) Sane

func (t *Termios) Sane()

Sane reset Term to sane values. Should be pretty much what the shell command "reset" does to the terminal.

func (*Termios) Set

func (t *Termios) Set(file *os.File) error

Set Sets terminal t attributes on file.

func (*Termios) Setwinsz

func (t *Termios) Setwinsz(file *os.File) error

Setwinsz Sets the terminal window size.

func (*Termios) ToSSH

func (t *Termios) ToSSH() map[uint8]uint32

ToSSH converts the Termios attributes to SSH attributes usable as ssh.TerminalModes.

func (*Termios) Winsz

func (t *Termios) Winsz(file *os.File) error

Winsz Fetches the current terminal windowsize. example handling changing window sizes with PTYs:

import "os" import "os/signal"

var sig = make(chan os.Signal,2) // Channel to listen for UNIX SIGNALS on signal.Notify(sig, syscall.SIGWINCH) // That'd be the window changing

for {
	<-sig
	term.Winsz(os.Stdin)			// We got signaled our terminal changed size so we read in the new value
 term.Setwinsz(pty.Slave) // Copy it to our virtual Terminal
}

type Underline

type Underline string

Underline implements the Stringer interface to print string in Underline mode.

func (Underline) String

func (c Underline) String() string

String implements the Stringer interface for type Underline.

type White

type White string

White implements the Stringer interface to print string foreground in White color.

func (White) String

func (c White) String() string

String implements the Stringer interface for type White.

type Winsize

type Winsize struct {
	WsRow    uint16 // WsRow 		Terminal number of rows
	WsCol    uint16 // WsCol 		Terminal number of columns
	WsXpixel uint16 // WsXpixel Terminal width in pixels
	WsYpixel uint16 // WsYpixel Terminal height in pixels
}

Winsize handle the terminal window size.

type Yellow

type Yellow string

Yellow implements the Stringer interface to print string foreground in Yellow color.

func (Yellow) String

func (c Yellow) String() string

String implements the Stringer interface for type Yellow.

Jump to

Keyboard shortcuts

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