termios

package module
v0.0.0-...-f103fc7 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2021 License: MIT Imports: 10 Imported by: 2

README

termios

For documentation, see https://pkg.golang.ir/github.com/scrouthtv/termios.

Getting started

Use termios by opening the current terminal. You can then

  • read keys from the user
  • write text, clear the terminal
  • get (window size), set (cursor position, style) properties
term, err := termios.Open()
if err != nil {
	panic(err)
}
defer term.Close()

term.ClearScreen(termios.ClearCompletely)
term.SetStyle(Style{termios.ColorRed, ColorDefault, TextBold})
term.WriteString("Hello world!")

term.SetRaw(true) // this is needed for input
keys, err := term.Read()
if err != nil {
	term.WriteString("Error reading keys:", keys)
	return
}
if k.Type == termios.KeySpecial && k.Value == termios.SpecialEnter {
	term.Move(MoveTo(0, 0).SetDown(1)) // move to the beginning of the next column
}

return

Under construction

Tag v3.0 showcases this libraries' functionality and already provides basic functionality for all major platforms. Still to be done:

  • Testing (different Linux terminals, bsd, darwin)
  • Linux: C-Special, A-Special, S-Special -- they neither have terminfo entries nor are identical for all terminals
  • Linux: the builtin terminfo is currently empty
  • Linux: xterm parser
  • Linux: parser#open() should return an error

Tag v3.2 adds the GetSize() functionality. It's return value is implementation- and terminal-dependant. The line width should always be reported correctly, however

  • The old Windows Terminal reports the height of the underlying buffer (lines that aren't visible)
  • The new Windows Terminal reports the visible height
  • The Linux implementation is fairly consistent in that it always returns the visible size.

In v3.3, the SetRaw() functionality was reintroduced.

Tag v4.0 introduced the Style and Color type. Every terminal is able set its style to one that as closely as possible resembles the specified style, but may not be able to display exactly the specified style (e. g. before Windows 10 there were only 16 colors).

  • 8 and 16 color constants are defined in the style.go.
  • 256 color constants are named in color256.go. They look like this. This table consistently takes 1 KB of RAM, so you can disable it's compilation with notable

The unix implementation waits for the signal SIGWINCH and reads the new window size using ioctl.

The windows implementation directly reads the current window size from the console info. Waiting for a WINDOW_SIZE_CHANGE event isn't applicable as this would require the developer reading user input every time just before the window size is required or else it'd get desynced.

I originally adopted this library from creack on GitHub. The original project had these functions: setting / reading terminal size and (un-) setting raw mode.

The key parsing API supports these keys on all supported terminals:

  • Letters: a-z, A-Z, 0-9, Extended Latin (U+0100 - U+FFFF)
  • Symbols: + - * # ~ , . - ; : _ < > | ^ ° ! " § $ % & / ( ) = ? { } [ ] \ ` ´
  • C-[a-z], for C-[A-Z] the lower case variant C-[a-z] should be returned
  • A-letter, A-Letter, A-symbol
  • F1 through F12, C-Fx, S-Fx, C-S-Fx
  • Special keys: Delete, Backspace, Enter, Insert, Home, End, PgUp, PgDown, Arrow Keys
  • C-Special, A-Special, S-Special

Combinations of different modifiers are only partly supported.

For non-special keys, C-A-key is explicitely not supported and will always be replaced by key.

Supported terminals

  • Windows:
    • Windows Terminal >= 1.6
    • ConHost >= Windows 7
    • Cmder >= 191012
    • ConEmu >= 210206
    • Fluent Terminal >= 0.7.5
  • Linux: * xterm >= #197 * termite (tested on v15)
    • Windows Terminal >= 1.6 via WSL

Known issues

  • Windows A-arrow, A-enter, A-escape, A-tab, C-A-Entf are not send
  • The Write([]byte) method does not work well with extended latin characters. Test before usage.
  • Windows/Terminal does not send many keys becauseof default keybindings: A-Enter, F11, C-Tab, C-S-Tab, S-Ins
  • Windows/Cmder does not send C-ArrowUp, C-ArrowDown
  • Windows/ConEmu does not send C-PgUp, C-PgDown by default (bound to scroll up / down)
  • Windows/Fluent Terminal hides a lot of keys: https://github.com/felixse/FluentTerminal/issues/885
  • Linux sends C-m instead of enter, C-j instead of C-enter, ...
  • Linux sends ' instead of ´ if typed as ´, press it twice instead
  • xterm does not send A-| if typed as "alt gr" + < (german keyboard layout)

termios

This is a complete rework of creak's original termios.

I rewrote the code to provide this functionality:

  • termios.Open() opens the console (stdin for reading and stdout for writing) and returns a Terminal. The terminal is always opened in raw mode and has these capabilites:
  • t.Close() closes everything that requires closing and resets the terminal to the original state. Any encountered error is discarded.
  • Read() reads a sequence of keystrokes in the order that they were typed by the user
  • Write() writes a string to the terminal
  • IsOpen() reports whether the developer can currently use the terminal for I/O

If any of these functions fail during execution, we try to undo all changes to the point before calling.

Read() and Write() will (try to) use the underlying devices, even if the terminal isn't opened (properly) or has already been closed.

This fork makes use of Go's new, platform-specific, syscall wrappers and all "unsafe" code was removed. The entire library is just a very thin wrapper around the new x/sys packages which (hopefully) uses the correct constants for the correct calls.

For testing, basic_test.go is provided. When called, it prints every raw data the library reads and reads 10 recognized keystrokes from the user.

  • The new sys package isn't available (yet?) for Plan9, so there will be no Plan9 support for now
  • Solaris licensing is weird at the moment, so there will be no Solaris support

Reading Keys

Every pressed key has a

  • Type: either KeyLetter or KeySpecial
  • Modifier: for KeyLetter optionally ModCtrl or ModAlt, for KeySpecial one of Special*
  • Value: for KeyLetter the full rune

Implementation

Terminal is implemented by platform-specific terminal implementations (nixTerm, winTerm). The implementation is responsible for opening and closing as well as reading and writing byte sequences.

Each read byte sequence is passed to an even more specific parser to be converted to a []Key:

  • On Windows, bytes are compared to a built-in table (see parse_win.go).
  • On Linux, consoles that are compatible to xterms advanced input mode (altSendsEscape) is parsed in parse_xterm.go. For other consoles, they are compared to either a terminfo file on the disk (terminfo.go) or a built-in terminfo table (see terminfo_builtin.go).

Documentation

Index

Constants

View Source
const (
	// ActionInit has to be sent to initialize the application.
	// For xterm-like terminals, this is the `smkx` code.
	ActionInit = iota
	// ActionExit has to be sent to close the application.
	// For xterm-like terminals, this is the `rmkx` code.
	ActionExit
)
View Source
const (
	// KeyLetter is a single letter. Value is a keycode out of the Basic Latin keymap.
	KeyLetter = iota
	// KeySpecial indicates that this key should not be printed, but be interpreted instead.
	KeySpecial
	// KeyInvalid is an invalid key, e. g. if an error ocured during parsing.
	KeyInvalid
)
View Source
const (
	// ModCtrl is or'd to the modifier list if the ctrl key was pressed.
	// For technical reasons, C-(A-Z) is reported as C-(a-z).
	ModCtrl = (1 << iota)
	// ModAlt is or'd to the modifier list if the alt key was pressed.
	ModAlt
	// ModShift is or'd to the modifier list if the shift key was pressed.
	// It is only applicable for special keys.
	ModShift
)
View Source
const (
	// SpecialBackspace is the key that deletes the character to the left of the cursor.
	SpecialBackspace = iota
	// SpecialDelete is the key that deletes the character to the right of the cursor.
	SpecialDelete
	// SpecialEnter is the enter / new-line key.
	SpecialEnter
	SpecialArrowLeft
	SpecialArrowRight
	SpecialArrowUp
	SpecialArrowDown
	SpecialHome
	SpecialEnd
	SpecialPgUp
	SpecialPgDown
	SpecialIns
	SpecialF1
	SpecialF2
	SpecialF3
	SpecialF4
	SpecialF5
	SpecialF6
	SpecialF7
	SpecialF8
	SpecialF9
	SpecialF10
	SpecialF11
	SpecialF12
	SpecialTab
	SpecialEscape
)

Variables

View Source
var (
	// ColorDefault resets the terminal to the default color.
	ColorDefault = Color{SpectrumDefault, 0, 0, 0}

	// ColorBlack is the black color.
	ColorBlack = Color{Spectrum8, 0, 0, 0}

	// ColorRed is the red color.
	ColorRed = Color{Spectrum8, 1, 0, 0}

	// ColorGreen is the green color.
	ColorGreen = Color{Spectrum8, 2, 0, 0}

	// ColorYellow is the yellow color, mixed from red and green.
	ColorYellow = Color{Spectrum8, 3, 0, 0}

	// ColorBlue is the blue color.
	ColorBlue = Color{Spectrum8, 4, 0, 0}

	// ColorMagenta is the magenta color, mixed from red and blue.
	ColorMagenta = Color{Spectrum8, 5, 0, 0}

	// ColorCyan is the cyan color, mixed from green and blue.
	ColorCyan = Color{Spectrum8, 6, 0, 0}

	// ColorWhite is the white color, mixed from red, green and blue.
	ColorWhite = Color{Spectrum8, 7, 0, 0}

	// ColorDarkGray is the black color.
	ColorDarkGray = Color{Spectrum16, 8, 0, 0}

	// ColorLightRed is the red color.
	ColorLightRed = Color{Spectrum16, 9, 0, 0}

	// ColorLightGreen is the green color.
	ColorLightGreen = Color{Spectrum16, 10, 0, 0}

	// ColorLightYellow is the yellow color, mixed from red and green.
	ColorLightYellow = Color{Spectrum16, 11, 0, 0}

	// ColorLightBlue is the blue color.
	ColorLightBlue = Color{Spectrum16, 12, 0, 0}

	// ColorLightMagenta is the magenta color, mixed from red and blue.
	ColorLightMagenta = Color{Spectrum16, 13, 0, 0}

	// ColorLightCyan is the cyan color, mixed from green and blue.
	ColorLightCyan = Color{Spectrum16, 14, 0, 0}

	// ColorLightGray is even brighter than white, mixed from red, green and blue.
	ColorLightGray = Color{Spectrum16, 15, 0, 0}
)
View Source
var (
	Color256Black                = Color{Spectrum8, 0, 0, 0}
	Color256Maroon               = Color{Spectrum8, 1, 0, 0}
	Color256Green                = Color{Spectrum8, 2, 0, 0}
	Color256Olive                = Color{Spectrum8, 3, 0, 0}
	Color256Navy                 = Color{Spectrum8, 4, 0, 0}
	Color256Purple               = Color{Spectrum8, 5, 0, 0}
	Color256Teal                 = Color{Spectrum8, 6, 0, 0}
	Color256Silver               = Color{Spectrum8, 7, 0, 0}
	Color256Grey                 = Color{Spectrum8, 8, 0, 0}
	Color256Red                  = Color{Spectrum8, 9, 0, 0}
	Color256Lime                 = Color{Spectrum8, 10, 0, 0}
	Color256Yellow               = Color{Spectrum8, 11, 0, 0}
	Color256Blue                 = Color{Spectrum8, 12, 0, 0}
	Color256Fuchsia              = Color{Spectrum8, 13, 0, 0}
	Color256Aqua                 = Color{Spectrum8, 14, 0, 0}
	Color256White                = Color{Spectrum8, 15, 0, 0}
	Color256Grey0                = Color{Spectrum8, 16, 0, 0}
	Color256NavyBlue             = Color{Spectrum8, 17, 0, 0}
	Color256DarkBlue             = Color{Spectrum8, 18, 0, 0}
	Color256Blue3Dark            = Color{Spectrum8, 19, 0, 0}
	Color256Blue3Light           = Color{Spectrum8, 20, 0, 0}
	Color256Blue1                = Color{Spectrum8, 21, 0, 0}
	Color256DarkGreen            = Color{Spectrum8, 22, 0, 0}
	Color256DeepSkyBlue4Dark     = Color{Spectrum8, 23, 0, 0}
	Color256DeepSkyBlue4Medium   = Color{Spectrum8, 24, 0, 0}
	Color256DeepSkyBlue4Light    = Color{Spectrum8, 25, 0, 0}
	Color256DodgerBlue3          = Color{Spectrum8, 26, 0, 0}
	Color256DodgerBlue2          = Color{Spectrum8, 27, 0, 0}
	Color256Green4               = Color{Spectrum8, 28, 0, 0}
	Color256SpringGreen4         = Color{Spectrum8, 29, 0, 0}
	Color256Turquoise4           = Color{Spectrum8, 30, 0, 0}
	Color256DeepSkyBlue3Dark     = Color{Spectrum8, 31, 0, 0}
	Color256DeepSkyBlue3Light    = Color{Spectrum8, 32, 0, 0}
	Color256DodgerBlue1          = Color{Spectrum8, 33, 0, 0}
	Color256Green3Dark           = Color{Spectrum8, 34, 0, 0}
	Color256SpringGreen3Dark     = Color{Spectrum8, 35, 0, 0}
	Color256DarkCyan             = Color{Spectrum8, 36, 0, 0}
	Color256LightSeaGreen        = Color{Spectrum8, 37, 0, 0}
	Color256DeepSkyBlue2         = Color{Spectrum8, 38, 0, 0}
	Color256DeepSkyBlue1         = Color{Spectrum8, 39, 0, 0}
	Color256Green3Light          = Color{Spectrum8, 40, 0, 0}
	Color256SpringGreen3Light    = Color{Spectrum8, 41, 0, 0}
	Color256SpringGreen2Dark     = Color{Spectrum8, 42, 0, 0}
	Color256Cyan3                = Color{Spectrum8, 43, 0, 0}
	Color256DarkTurquoise        = Color{Spectrum8, 44, 0, 0}
	Color256Turquoise2           = Color{Spectrum8, 45, 0, 0}
	Color256Green1               = Color{Spectrum8, 46, 0, 0}
	Color256SpringGreen2Light    = Color{Spectrum8, 47, 0, 0}
	Color256SpringGreen1         = Color{Spectrum8, 48, 0, 0}
	Color256MediumSpringGreen    = Color{Spectrum8, 49, 0, 0}
	Color256Cyan2                = Color{Spectrum8, 50, 0, 0}
	Color256Cyan1                = Color{Spectrum8, 51, 0, 0}
	Color256DarkRedDark          = Color{Spectrum8, 52, 0, 0}
	Color256DeepPink4Dark        = Color{Spectrum8, 53, 0, 0}
	Color256Purple4Dark          = Color{Spectrum8, 54, 0, 0}
	Color256Purple4Light         = Color{Spectrum8, 55, 0, 0}
	Color256Purple3              = Color{Spectrum8, 56, 0, 0}
	Color256BlueViolet           = Color{Spectrum8, 57, 0, 0}
	Color256Orange4Dark          = Color{Spectrum8, 58, 0, 0}
	Color256Grey37               = Color{Spectrum8, 59, 0, 0}
	Color256MediumPurple4        = Color{Spectrum8, 60, 0, 0}
	Color256SlateBlue3Dark       = Color{Spectrum8, 61, 0, 0}
	Color256SlateBlue3Light      = Color{Spectrum8, 62, 0, 0}
	Color256RoyalBlue1           = Color{Spectrum8, 63, 0, 0}
	Color256Chartreuse4          = Color{Spectrum8, 64, 0, 0}
	Color256DarkSeaGreen4Dark    = Color{Spectrum8, 65, 0, 0}
	Color256PaleTurquoise4       = Color{Spectrum8, 66, 0, 0}
	Color256SteelBlue            = Color{Spectrum8, 67, 0, 0}
	Color256SteelBlue3           = Color{Spectrum8, 68, 0, 0}
	Color256CornflowerBlue       = Color{Spectrum8, 69, 0, 0}
	Color256Chartreuse3Dark      = Color{Spectrum8, 70, 0, 0}
	Color256DarkSeaGreen4Light   = Color{Spectrum8, 71, 0, 0}
	Color256CadetBlueDark        = Color{Spectrum8, 72, 0, 0}
	Color256CadetBlueLight       = Color{Spectrum8, 73, 0, 0}
	Color256SkyBlue3             = Color{Spectrum8, 74, 0, 0}
	Color256SteelBlue1Dark       = Color{Spectrum8, 75, 0, 0}
	Color256Chartreuse3Light     = Color{Spectrum8, 76, 0, 0}
	Color256PaleGreen3Dark       = Color{Spectrum8, 77, 0, 0}
	Color256SeaGreen3            = Color{Spectrum8, 78, 0, 0}
	Color256Aquamarine3          = Color{Spectrum8, 79, 0, 0}
	Color256MediumTurquoise      = Color{Spectrum8, 80, 0, 0}
	Color256SteelBlue1Light      = Color{Spectrum8, 81, 0, 0}
	Color256Chartreuse2Dark      = Color{Spectrum8, 82, 0, 0}
	Color256SeaGreen2            = Color{Spectrum8, 83, 0, 0}
	Color256SeaGreen1Dark        = Color{Spectrum8, 84, 0, 0}
	Color256SeaGreen1Light       = Color{Spectrum8, 85, 0, 0}
	Color256Aquamarine1Dark      = Color{Spectrum8, 86, 0, 0}
	Color256DarkSlateGray2       = Color{Spectrum8, 87, 0, 0}
	Color256DarkRedLight         = Color{Spectrum8, 88, 0, 0}
	Color256DeepPink4Medium      = Color{Spectrum8, 89, 0, 0}
	Color256DarkMagentaDark      = Color{Spectrum8, 90, 0, 0}
	Color256DarkMagentaLight     = Color{Spectrum8, 91, 0, 0}
	Color256DarkVioletDark       = Color{Spectrum8, 92, 0, 0}
	Color256Purple2              = Color{Spectrum8, 93, 0, 0}
	Color256Orange4Light         = Color{Spectrum8, 94, 0, 0}
	Color256LightPink4           = Color{Spectrum8, 95, 0, 0}
	Color256Plum4                = Color{Spectrum8, 96, 0, 0}
	Color256MediumPurple3Dark    = Color{Spectrum8, 97, 0, 0}
	Color256MediumPurple3Light   = Color{Spectrum8, 98, 0, 0}
	Color256SlateBlue1           = Color{Spectrum8, 99, 0, 0}
	Color256Yellow4Dark          = Color{Spectrum8, 100, 0, 0}
	Color256Wheat4               = Color{Spectrum8, 101, 0, 0}
	Color256Grey53               = Color{Spectrum8, 102, 0, 0}
	Color256LightSlateGrey       = Color{Spectrum8, 103, 0, 0}
	Color256MediumPurpleDark     = Color{Spectrum8, 104, 0, 0}
	Color256LightSlateBlue       = Color{Spectrum8, 105, 0, 0}
	Color256Yellow4Light         = Color{Spectrum8, 106, 0, 0}
	Color256DarkOliveGreen3Dark  = Color{Spectrum8, 107, 0, 0}
	Color256DarkSeaGreen         = Color{Spectrum8, 108, 0, 0}
	Color256LightSkyBlue3Dark    = Color{Spectrum8, 109, 0, 0}
	Color256LightSkyBlue3Light   = Color{Spectrum8, 110, 0, 0}
	Color256SkyBlue2             = Color{Spectrum8, 111, 0, 0}
	Color256Chartreuse2Light     = Color{Spectrum8, 112, 0, 0}
	Color256DarkOliveGreen3Light = Color{Spectrum8, 113, 0, 0}
	Color256PaleGreen3Light      = Color{Spectrum8, 114, 0, 0}
	Color256DarkSeaGreen3Dark    = Color{Spectrum8, 115, 0, 0}
	Color256DarkSlateGray3       = Color{Spectrum8, 116, 0, 0}
	Color256SkyBlue1             = Color{Spectrum8, 117, 0, 0}
	Color256Chartreuse1          = Color{Spectrum8, 118, 0, 0}
	Color256LightGreenDark       = Color{Spectrum8, 119, 0, 0}
	Color256LightGreenLight      = Color{Spectrum8, 120, 0, 0}
	Color256PaleGreen1Dark       = Color{Spectrum8, 121, 0, 0}
	Color256Aquamarine1Light     = Color{Spectrum8, 122, 0, 0}
	Color256DarkSlateGray1       = Color{Spectrum8, 123, 0, 0}
	Color256Red3Dark             = Color{Spectrum8, 124, 0, 0}
	Color256DeepPink4Light       = Color{Spectrum8, 125, 0, 0}
	Color256MediumVioletRed      = Color{Spectrum8, 126, 0, 0}
	Color256Magenta3             = Color{Spectrum8, 127, 0, 0}
	Color256DarkVioletLight      = Color{Spectrum8, 128, 0, 0}
	Color256PurpleLight          = Color{Spectrum8, 129, 0, 0}
	Color256DarkOrange3Dark      = Color{Spectrum8, 130, 0, 0}
	Color256IndianRedDark        = Color{Spectrum8, 131, 0, 0}
	Color256HotPink3Dark         = Color{Spectrum8, 132, 0, 0}
	Color256MediumOrchid3        = Color{Spectrum8, 133, 0, 0}
	Color256MediumOrchid         = Color{Spectrum8, 134, 0, 0}
	Color256MediumPurple2Dark    = Color{Spectrum8, 135, 0, 0}
	Color256DarkGoldenrod        = Color{Spectrum8, 136, 0, 0}
	Color256LightSalmon3Dark     = Color{Spectrum8, 137, 0, 0}
	Color256RosyBrown            = Color{Spectrum8, 138, 0, 0}
	Color256Grey63               = Color{Spectrum8, 139, 0, 0}
	Color256MediumPurple2Light   = Color{Spectrum8, 140, 0, 0}
	Color256MediumPurple1        = Color{Spectrum8, 141, 0, 0}
	Color256Gold3Dark            = Color{Spectrum8, 142, 0, 0}
	Color256DarkKhaki            = Color{Spectrum8, 143, 0, 0}
	Color256NavajoWhite3         = Color{Spectrum8, 144, 0, 0}
	Color256Grey69               = Color{Spectrum8, 145, 0, 0}
	Color256LightSteelBlue3      = Color{Spectrum8, 146, 0, 0}
	Color256LightSteelBlue       = Color{Spectrum8, 147, 0, 0}
	Color256Yellow3Dark          = Color{Spectrum8, 148, 0, 0}
	Color256DarkOliveGreen3      = Color{Spectrum8, 149, 0, 0}
	Color256DarkSeaGreen3Light   = Color{Spectrum8, 150, 0, 0}
	Color256DarkSeaGreen2Dark    = Color{Spectrum8, 151, 0, 0}
	Color256LightCyan3           = Color{Spectrum8, 152, 0, 0}
	Color256LightSkyBlue1        = Color{Spectrum8, 153, 0, 0}
	Color256GreenYellow          = Color{Spectrum8, 154, 0, 0}
	Color256DarkOliveGreen2      = Color{Spectrum8, 155, 0, 0}
	Color256PaleGreen1Light      = Color{Spectrum8, 156, 0, 0}
	Color256DarkSeaGreen2Light   = Color{Spectrum8, 157, 0, 0}
	Color256DarkSeaGreen1Dark    = Color{Spectrum8, 158, 0, 0}
	Color256PaleTurquoise1       = Color{Spectrum8, 159, 0, 0}
	Color256Red3Light            = Color{Spectrum8, 160, 0, 0}
	Color256DeepPink3Dark        = Color{Spectrum8, 161, 0, 0}
	Color256DeepPink3Light       = Color{Spectrum8, 162, 0, 0}
	Color256Magenta3Dark         = Color{Spectrum8, 163, 0, 0}
	Color256Magenta3Light        = Color{Spectrum8, 164, 0, 0}
	Color256Magenta2Dark         = Color{Spectrum8, 165, 0, 0}
	Color256DarkOrange3Light     = Color{Spectrum8, 166, 0, 0}
	Color256IndianRedLight       = Color{Spectrum8, 167, 0, 0}
	Color256HotPink3Light        = Color{Spectrum8, 168, 0, 0}
	Color256HotPink2             = Color{Spectrum8, 169, 0, 0}
	Color256Orchid               = Color{Spectrum8, 170, 0, 0}
	Color256MediumOrchid1Dark    = Color{Spectrum8, 171, 0, 0}
	Color256Orange3              = Color{Spectrum8, 172, 0, 0}
	Color256LightSalmon3Light    = Color{Spectrum8, 173, 0, 0}
	Color256LightPink3           = Color{Spectrum8, 174, 0, 0}
	Color256Pink3                = Color{Spectrum8, 175, 0, 0}
	Color256Plum3                = Color{Spectrum8, 176, 0, 0}
	Color256Violet               = Color{Spectrum8, 177, 0, 0}
	Color256Gold3Light           = Color{Spectrum8, 178, 0, 0}
	Color256LightGoldenrod3      = Color{Spectrum8, 179, 0, 0}
	Color256Tan                  = Color{Spectrum8, 180, 0, 0}
	Color256MistyRose3           = Color{Spectrum8, 181, 0, 0}
	Color256Thistle3             = Color{Spectrum8, 182, 0, 0}
	Color256Plum2                = Color{Spectrum8, 183, 0, 0}
	Color256Yellow3Light         = Color{Spectrum8, 184, 0, 0}
	Color256Khaki3               = Color{Spectrum8, 185, 0, 0}
	Color256LightGoldenrod2      = Color{Spectrum8, 186, 0, 0}
	Color256LightYellow3         = Color{Spectrum8, 187, 0, 0}
	Color256Grey84               = Color{Spectrum8, 188, 0, 0}
	Color256LightSteelBlue1      = Color{Spectrum8, 189, 0, 0}
	Color256Yellow2              = Color{Spectrum8, 190, 0, 0}
	Color256DarkOliveGreen1Dark  = Color{Spectrum8, 191, 0, 0}
	Color256DarkOliveGreen1Light = Color{Spectrum8, 192, 0, 0}
	Color256DarkSeaGreen1Light   = Color{Spectrum8, 193, 0, 0}
	Color256Honeydew2            = Color{Spectrum8, 194, 0, 0}
	Color256LightCyan1           = Color{Spectrum8, 195, 0, 0}
	Color256Red1                 = Color{Spectrum8, 196, 0, 0}
	Color256DeepPink2            = Color{Spectrum8, 197, 0, 0}
	Color256DeepPink1Dark        = Color{Spectrum8, 198, 0, 0}
	Color256DeepPink1Light       = Color{Spectrum8, 199, 0, 0}
	Color256Magenta2Light        = Color{Spectrum8, 200, 0, 0}
	Color256Magenta1             = Color{Spectrum8, 201, 0, 0}
	Color256OrangeRed1           = Color{Spectrum8, 202, 0, 0}
	Color256IndianRed1Dark       = Color{Spectrum8, 203, 0, 0}
	Color256IndianRed1Light      = Color{Spectrum8, 204, 0, 0}
	Color256HotPinkDark          = Color{Spectrum8, 205, 0, 0}
	Color256HotPinkLight         = Color{Spectrum8, 206, 0, 0}
	Color256MediumOrchid1Light   = Color{Spectrum8, 207, 0, 0}
	Color256DarkOrange           = Color{Spectrum8, 208, 0, 0}
	Color256Salmon1              = Color{Spectrum8, 209, 0, 0}
	Color256LightCoral           = Color{Spectrum8, 210, 0, 0}
	Color256PaleVioletRed1       = Color{Spectrum8, 211, 0, 0}
	Color256Orchid2              = Color{Spectrum8, 212, 0, 0}
	Color256Orchid1              = Color{Spectrum8, 213, 0, 0}
	Color256Orange1              = Color{Spectrum8, 214, 0, 0}
	Color256SandyBrown           = Color{Spectrum8, 215, 0, 0}
	Color256LightSalmon1         = Color{Spectrum8, 216, 0, 0}
	Color256LightPink1           = Color{Spectrum8, 217, 0, 0}
	Color256Pink1                = Color{Spectrum8, 218, 0, 0}
	Color256Plum1                = Color{Spectrum8, 219, 0, 0}
	Color256Gold1                = Color{Spectrum8, 220, 0, 0}
	Color256LightGoldenrod2Dark  = Color{Spectrum8, 221, 0, 0}
	Color256LightGoldenrod2Light = Color{Spectrum8, 222, 0, 0}
	Color256NavajoWhite1         = Color{Spectrum8, 223, 0, 0}
	Color256MistyRose1           = Color{Spectrum8, 224, 0, 0}
	Color256Thistle1             = Color{Spectrum8, 225, 0, 0}
	Color256Yellow1              = Color{Spectrum8, 226, 0, 0}
	Color256LightGoldenrod1      = Color{Spectrum8, 227, 0, 0}
	Color256Khaki1               = Color{Spectrum8, 228, 0, 0}
	Color256Wheat1               = Color{Spectrum8, 229, 0, 0}
	Color256Cornsilk1            = Color{Spectrum8, 230, 0, 0}
	Color256Grey100              = Color{Spectrum8, 231, 0, 0}
	Color256Grey3                = Color{Spectrum8, 232, 0, 0}
	Color256Grey7                = Color{Spectrum8, 233, 0, 0}
	Color256Grey11               = Color{Spectrum8, 234, 0, 0}
	Color256Grey15               = Color{Spectrum8, 235, 0, 0}
	Color256Grey19               = Color{Spectrum8, 236, 0, 0}
	Color256Grey23               = Color{Spectrum8, 237, 0, 0}
	Color256Grey27               = Color{Spectrum8, 238, 0, 0}
	Color256Grey30               = Color{Spectrum8, 239, 0, 0}
	Color256Grey35               = Color{Spectrum8, 240, 0, 0}
	Color256Grey39               = Color{Spectrum8, 241, 0, 0}
	Color256Grey42               = Color{Spectrum8, 242, 0, 0}
	Color256Grey46               = Color{Spectrum8, 243, 0, 0}
	Color256Grey50               = Color{Spectrum8, 244, 0, 0}
	Color256Grey54               = Color{Spectrum8, 245, 0, 0}
	Color256Grey58               = Color{Spectrum8, 246, 0, 0}
	Color256Grey62               = Color{Spectrum8, 247, 0, 0}
	Color256Grey66               = Color{Spectrum8, 248, 0, 0}
	Color256Grey70               = Color{Spectrum8, 249, 0, 0}
	Color256Grey74               = Color{Spectrum8, 250, 0, 0}
	Color256Grey78               = Color{Spectrum8, 251, 0, 0}
	Color256Grey82               = Color{Spectrum8, 252, 0, 0}
	Color256Grey85               = Color{Spectrum8, 253, 0, 0}
	Color256Grey89               = Color{Spectrum8, 254, 0, 0}
	Color256Grey93               = Color{Spectrum8, 255, 0, 0}
)
View Source
var InvalidKey = Key{KeyInvalid, 0, utf8.RuneError}

InvalidKey is returned if an error ocured during reading.

Functions

This section is empty.

Types

type ClearType

type ClearType uint8
const (
	ClearToEnd      ClearType = iota
	ClearToStart    ClearType = iota
	ClearCompletely ClearType = iota
)

type Color

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

Color is a color of a specified spectrum.

func (*Color) Downsample

func (c *Color) Downsample(target Spectrum) *Color

Downsample returns the closest color in the specified target spectrum. If target has more colors than the original color's spectrum, the old color is returned.

func (*Color) Spectrum

func (c *Color) Spectrum() Spectrum

Spectrum returns the spectrum of a color.

type IOError

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

func (*IOError) Error

func (e *IOError) Error() string

func (*IOError) Unwrap

func (e *IOError) Unwrap() error

type InvalidClearTypeError

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

func (*InvalidClearTypeError) Error

func (e *InvalidClearTypeError) Error() string

type InvalidMovementError

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

func (*InvalidMovementError) Error

func (e *InvalidMovementError) Error() string

type InvalidResponseError

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

func (*InvalidResponseError) Error

func (e *InvalidResponseError) Error() string

type Key

type Key struct {
	Type  byte
	Mod   byte
	Value rune
}

Key is an abstract key combination on the keyboard.

func (*Key) Equal

func (k *Key) Equal(other *Key) bool

Equal compares this key with another one and returns wether they correspond to the same combination of keys. Keep in mind that a key might not always have all modifiers set.

func (*Key) String

func (k *Key) String() string

type Movement

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

func MoveBy

func MoveBy(x, y int) *Movement

func MoveDown

func MoveDown(x int) *Movement

func MoveLeft

func MoveLeft(x int) *Movement

func MoveRight

func MoveRight(x int) *Movement

func MoveTo

func MoveTo(x, y int) *Movement

func MoveUp

func MoveUp(x int) *Movement

func (*Movement) SetColumn

func (m *Movement) SetColumn(col int) *Movement

func (*Movement) SetDown

func (m *Movement) SetDown(n int) *Movement

func (*Movement) SetLeft

func (m *Movement) SetLeft(n int) *Movement

func (*Movement) SetRight

func (m *Movement) SetRight(n int) *Movement

func (*Movement) SetUp

func (m *Movement) SetUp(n int) *Movement

func (*Movement) String

func (m *Movement) String() string

type Position

type Position struct {
	X int
	Y int
}

type RGB

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

RGB is an rgb color with 8 bpc.

func (*RGB) Difference

func (c *RGB) Difference(other *RGB) uint8

Difference returns the difference between two colors on a scale from 0 to 255. E.g. if the colors are completely inverted, Difference returns 255, if they're very close colors (just a pitch more red), Difference returns a number close to 0.

type Spectrum

type Spectrum uint8

Spectrum specifies the colorspace a color is given in.

const (
	// SpectrumDefault only has one color: the terminal's default color.
	SpectrumDefault Spectrum = 0

	// Spectrum8 indicates that the spectrum has 8 colors.
	// It usually includes red, blue, green and binary combinations of these.
	// It needs 3 bits of storage.
	Spectrum8 Spectrum = 1

	// Spectrum16 indicates that the spectrum has 16 colors.
	// It usually adds a bright modifier to the 8 color spectrum.
	// It needs 4 bits of storage.
	Spectrum16 Spectrum = 2

	// Spectrum256 indicates that the spectrum has 256 colors.
	// It is usually a terminal-specific gradient.
	// It takes 8 bits of storage.
	Spectrum256 Spectrum = 3

	// SpectrumRGB indicates that the spectrum has all RGB colors.
	// Each component can be specified freely between 0 and 255.
	// It takes 24 bits of storage.
	SpectrumRGB Spectrum = 4
)

func (*Spectrum) Equal

func (s *Spectrum) Equal(other *Spectrum) bool

func (*Spectrum) MoreThan

func (s *Spectrum) MoreThan(other *Spectrum) bool

MoreThan tests whether this spectrum has *more* colors than the other.

type Style

type Style struct {
	Foreground Color
	Background Color
	Extras     TextAttribute
}

Style collects all attributes that can be given to a character in the terminal. It includes foreground, background color and more text attributes.

type TermSize

type TermSize struct {
	Width  uint16
	Height uint16
}

TermSize groups the width and height of a terminal in characters.

type Terminal

type Terminal interface {

	// Read reads a single keypress
	// and returns an array of keys in the order that they were typed
	// or in case of an error an empty list and the error.
	// If the terminal hasn't been set to raw mode, the user must first press enter for keys
	// to be sent to the application.
	Read() ([]Key, error)

	// WriteString writes the specified string at the current position into the terminal
	// It returns the number of bytes (there may be multiple bytes in a character) written
	// or an error.
	WriteString(string) (int, error)

	// Write writes the specified data at the current position into the terminal.
	// It returns the number of bytes written or an error.
	Write([]byte) (int, error)

	// SetRaw enables or disables raw mode for this terminal.
	SetRaw(bool) error

	// IsOpen returns whether the developer can currently read from / write to
	// this terminal.
	IsOpen() bool
	Close()

	// GetSize returns the terminal's current size.
	GetSize() TermSize

	// SetStyle sets the terminal style. Not all terminals support all styles (e. g. 24bit colors).
	SetStyle(Style) error

	// GetPosition returns the current cursor position. On some terminals, this takes some time.
	GetPosition() (*Position, error)

	// Move moves the cursor and point of writing to a new position specified by the Movement.
	// Implementations will not cross line borders if the provided horizontal movement exceeds line width.
	Move(*Movement) error

	// ClearScreen clears the screen depending on the ClearType.
	ClearScreen(ClearType) error

	// ClearLine clears this line depending on the ClearType.
	ClearLine(ClearType) error
	// contains filtered or unexported methods
}

Terminal is an abstract terminal where the user can press arbitrary keys and the developer can write arbitrary strings as well as some actions.

func Open

func Open() (Terminal, error)

Open opens a new terminal.

type TextAttribute

type TextAttribute uint8

TextAttribute sets more styling options on text.

const (
	// TextDefault unsets all text attributes.
	TextDefault TextAttribute = 0

	// TextBold makes the text appear bold.
	// On some terminals it will create bright text instead.
	TextBold TextAttribute = 0x1

	// TextDim makes the text appear dim.
	TextDim TextAttribute = 0x2

	// TextUnderlined underlines the text.
	TextUnderlined TextAttribute = 0x4

	// TextBlink blinks the text. Does not work on some terminals.
	TextBlink TextAttribute = 0x8

	// TextReverse reverses foreground and background color.
	TextReverse TextAttribute = 0x10

	// TextHidden hides the text.
	TextHidden TextAttribute = 0x20

	// TextCursive prints the text in cursive. Does only work on few terminals.
	TextCursive TextAttribute = 0x40
)

Directories

Path Synopsis
bwin module
keys module
utf8 module

Jump to

Keyboard shortcuts

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