goterm: github.com/google/goterm/term Index | Examples | Files

package term

import "github.com/google/goterm/term"

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

Package Files

color.go ssh.go termios.go

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

func BBlackf Uses

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

BBlackf returns a BBlack formatted string.

func BBluef Uses

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

BBluef returns a BBlue formatted string.

func BCyanf Uses

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

BCyanf returns a BCyan formatted string.

func BGreenf Uses

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

BGreenf returns a BGreen formatted string.

func BMagentaf Uses

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

BMagentaf returns a BMagenta formatted string.

func BRedf Uses

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

BRedf returns a BRed formatted string.

func BWhitef Uses

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

BWhitef returns a BWhite formatted string.

func BYellowf Uses

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

BYellowf returns a BYellow formatted string.

func Blackf Uses

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

Blackf returns a Black formatted string.

func Bluef Uses

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

Bluef returns a Blue formatted string.

func ColorDisable Uses

func ColorDisable()

ColorDisable disables the terminal colors.

func ColorEnable Uses

func ColorEnable()

ColorEnable activates the terminal colors , this is the default.

func Cyanf Uses

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

Cyanf returns a Cyan formatted string.

func GetChar Uses

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

GetChar reads a single byte.

func GetPass Uses

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

GetPass reads password from a TTY with no echo.

func Greenf Uses

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

Greenf returns a Green formatted string.

func Isatty Uses

func Isatty(file *os.File) bool

Isatty returns true if file is a tty.

func Magentaf Uses

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

Magentaf returns a Magenta formatted string.

func Redf Uses

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

Redf returns a Red formatted string.

func TestTerm Uses

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 Uses

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

Whitef returns a White formatted string.

func Yellowf Uses

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

Yellowf returns a Yellow formatted string.

type BBlack Uses

type BBlack string

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

func (BBlack) String Uses

func (c BBlack) String() string

String implements the Stringer interface for type BBlack.

type BBlue Uses

type BBlue string

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

func (BBlue) String Uses

func (c BBlue) String() string

String implements the Stringer interface for type BBlue.

type BCyan Uses

type BCyan string

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

func (BCyan) String Uses

func (c BCyan) String() string

String implements the Stringer interface for type BCyan.

type BGreen Uses

type BGreen string

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

func (BGreen) String Uses

func (c BGreen) String() string

String implements the Stringer interface for type BGreen.

type BMagenta Uses

type BMagenta string

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

func (BMagenta) String Uses

func (c BMagenta) String() string

String implements the Stringer interface for type BMagenta.

type BRandom Uses

type BRandom string

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

func (BRandom) String Uses

func (c BRandom) String() string

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

type BRed Uses

type BRed string

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

func (BRed) String Uses

func (c BRed) String() string

String implements the Stringer interface for type BRed.

type BWhite Uses

type BWhite string

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

func (BWhite) String Uses

func (c BWhite) String() string

String implements the Stringer interface for type BWhite.

type BYellow Uses

type BYellow string

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

func (BYellow) String Uses

func (c BYellow) String() string

String implements the Stringer interface for type BYellow.

type Black Uses

type Black string

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

func (Black) String Uses

func (c Black) String() string

String implements the Stringer interface for type Black.

type Blinking Uses

type Blinking string

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

func (Blinking) String Uses

func (c Blinking) String() string

String implements the Stringer interface for type Blinking.

type Blue Uses

type Blue string

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

func (Blue) String Uses

func (c Blue) String() string

String implements the Stringer interface for type Blue.

type Bold Uses

type Bold string

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

func (Bold) String Uses

func (c Bold) String() string

String implements the Stringer interface for type Bold.

type Color Uses

type Color string

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

ExampleColor()

Code:

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 Green world
Warning!
Green world
Defcon:  2 FAST PACE

func NewColor Uses

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 Uses

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 Uses

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 Uses

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

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

func (Color) String Uses

func (c Color) String() string

String the stringer interface for all base color types.

type Color256Random Uses

type Color256Random string

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

type ColorRandom Uses

type ColorRandom string

ColorRandom implements the Stringer interface to print string Random color.

func (ColorRandom) String Uses

func (c ColorRandom) String() string

String is a random color stringer.

type Cyan Uses

type Cyan string

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

func (Cyan) String Uses

func (c Cyan) String() string

String implements the Stringer interface for type Cyan.

type Green Uses

type Green string

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

func (Green) String Uses

func (c Green) String() string

String implements the Stringer interface for type Green.

type Italic Uses

type Italic string

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

func (Italic) String Uses

func (c Italic) String() string

String implements the Stringer interface for type Italic.

type Magenta Uses

type Magenta string

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

func (Magenta) String Uses

func (c Magenta) String() string

String implements the Stringer interface for type Magenta.

type PTY Uses

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 Uses

func OpenPTY() (*PTY, error)

OpenPTY Creates a new Master/Slave PTY pair.

func (*PTY) Close Uses

func (p *PTY) Close() error

Close closes the PTYs that OpenPTY created.

func (*PTY) GetChar Uses

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

GetChar fine old getchar() for a PTY.

func (*PTY) PTSName Uses

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

PTSName return the name of the pty.

func (*PTY) PTSNumber Uses

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

PTSNumber return the pty number.

func (*PTY) ReadByte Uses

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

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

type Random Uses

type Random string

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

func (Random) String Uses

func (c Random) String() string

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

type Red Uses

type Red string

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

func (Red) String Uses

func (c Red) String() string

String implements the Stringer interface for type Red.

type Termios Uses

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 Uses

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

Attr Gets (terminal related) attributes from file.

func (*Termios) Cook Uses

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 Uses

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

FromSSH converts SSH attributes to Termios attributes.

func (*Termios) Raw Uses

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 Uses

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 Uses

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

Set Sets terminal t attributes on file.

func (*Termios) Setwinsz Uses

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

Setwinsz Sets the terminal window size.

func (*Termios) ToSSH Uses

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

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

func (*Termios) Winsz Uses

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 Uses

type Underline string

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

func (Underline) String Uses

func (c Underline) String() string

String implements the Stringer interface for type Underline.

type White Uses

type White string

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

func (White) String Uses

func (c White) String() string

String implements the Stringer interface for type White.

type Winsize Uses

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 Uses

type Yellow string

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

func (Yellow) String Uses

func (c Yellow) String() string

String implements the Stringer interface for type Yellow.

Package term imports 8 packages (graph) and is imported by 5 packages. Updated 2019-10-14. Refresh now. Tools for package owners.