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 ¶
- Constants
- func BBlackf(format string, a ...interface{}) string
- func BBluef(format string, a ...interface{}) string
- func BCyanf(format string, a ...interface{}) string
- func BGreenf(format string, a ...interface{}) string
- func BMagentaf(format string, a ...interface{}) string
- func BRedf(format string, a ...interface{}) string
- func BWhitef(format string, a ...interface{}) string
- func BYellowf(format string, a ...interface{}) string
- func Blackf(format string, a ...interface{}) string
- func Bluef(format string, a ...interface{}) string
- func ColorDisable()
- func ColorEnable()
- func Cyanf(format string, a ...interface{}) string
- func GetChar(f *os.File) (b byte, err error)
- func GetPass(prompt string, f *os.File, pbuf []byte) ([]byte, error)
- func Greenf(format string, a ...interface{}) string
- func Isatty(file *os.File) bool
- func Magentaf(format string, a ...interface{}) string
- func Redf(format string, a ...interface{}) string
- func TestTerm() string
- func Whitef(format string, a ...interface{}) string
- func Yellowf(format string, a ...interface{}) string
- type BBlack
- type BBlue
- type BCyan
- type BGreen
- type BMagenta
- type BRandom
- type BRed
- type BWhite
- type BYellow
- type Black
- type Blinking
- type Blue
- type Bold
- type Color
- type Color256Random
- type ColorRandom
- type Cyan
- type Green
- type Italic
- type Magenta
- type PTY
- type Random
- type Red
- type Termios
- func (t *Termios) Cook()
- func (t *Termios) FromSSH(termModes map[uint8]uint32)
- func (t *Termios) Raw()
- func (t *Termios) Sane()
- func (t *Termios) Set(file *os.File) error
- func (t *Termios) Setwinsz(file *os.File) error
- func (t *Termios) ToSSH() map[uint8]uint32
- func (t *Termios) Winsz(file *os.File) error
- type Underline
- type White
- type Winsize
- type Yellow
Examples ¶
Constants ¶
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
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.
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
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
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.
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
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 ColorEnable ¶
func ColorEnable()
ColorEnable activates the terminal colors , this is the default.
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.
Types ¶
type BBlack ¶
type BBlack string
BBlack implements the Stringer interface to print string background in Black color.
type BBlue ¶
type BBlue string
BBlue implements the Stringer interface to print string background in Blue color.
type BCyan ¶
type BCyan string
BCyan implements the Stringer interface to print string background in Cyan color.
type BGreen ¶
type BGreen string
BGreen implements the Stringer interface to print string background in Green color.
type BMagenta ¶
type BMagenta string
BMagenta implements the Stringer interface to print string background in Magenta color.
type BRandom ¶
type BRandom string
BRandom implements the Stringer interface to print string background in Random color.
type BRed ¶
type BRed string
BRed implements the Stringer interface to print string background in Red color.
type BWhite ¶
type BWhite string
BWhite implements the Stringer interface to print string background in White color.
type BYellow ¶
type BYellow string
BYellow implements the Stringer interface to print string background in Yellow color.
type Black ¶
type Black string
Black implements the Stringer interface to print string foreground in Black color.
type Blinking ¶
type Blinking string
Blinking implements the Stringer interface to print string in Blinking mode.
type Blue ¶
type Blue string
Blue implements the Stringer interface to print string foreground in Blue color.
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 ¶
NewColor gives a type Color back with specified fg/bg colors set that can be printed with anything using the Stringer iface.
func NewColor256 ¶
NewColor256 gives a type Color back using Term 256 color that can be printed with anything using the Stringer iface.
func NewColorRGB ¶
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
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.
type Cyan ¶
type Cyan string
Cyan implements the Stringer interface to print string foreground in Cyan color.
type Green ¶
type Green string
Green implements the Stringer interface to print string foreground in Green color.
type Italic ¶
type Italic string
Italic implements the Stringer interface to print string foreground in Italic color.
type Magenta ¶
type Magenta string
Magenta implements the Stringer interface to print string foreground in Magenta color.
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
type Random ¶
type Random string
Random implements the Stringer interface to print string foreground in Random color.
type Red ¶
type Red string
Red implements the Stringer interface to print string foreground in Red color.
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 (*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) 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) ToSSH ¶
ToSSH converts the Termios attributes to SSH attributes usable as ssh.TerminalModes.
func (*Termios) Winsz ¶
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.
type White ¶
type White string
White implements the Stringer interface to print string foreground in White color.