Documentation
¶
Overview ¶
Package twin provides Terminal Window interaction
Package twin provides Terminal Window interaction
Index ¶
- Variables
- func Printable(char rune) bool
- type AttrMask
- type Color
- type ColorCount
- type Event
- type EventExit
- type EventKeyCode
- type EventMouse
- type EventResize
- type EventRune
- type EventTerminalBackgroundDetected
- type FakeScreen
- func (screen *FakeScreen) Clear()
- func (screen *FakeScreen) Close()
- func (screen *FakeScreen) Events() chan Event
- func (screen *FakeScreen) GetRow(row int) []StyledRune
- func (screen *FakeScreen) RequestTerminalBackgroundColor()
- func (screen *FakeScreen) SetCell(column int, row int, styledRune StyledRune) int
- func (screen *FakeScreen) Show()
- func (screen *FakeScreen) ShowCursorAt(_ int, _ int)
- func (screen *FakeScreen) ShowNLines(int)
- func (screen *FakeScreen) Size() (width int, height int)
- type KeyCode
- type MouseButtonMask
- type MouseMode
- type Screen
- type Style
- func (current Style) RenderUpdateFrom(previous Style, terminalColorCount ColorCount) string
- func (style Style) String() string
- func (style Style) WithAttr(attr AttrMask) Style
- func (style Style) WithBackground(color Color) Style
- func (style Style) WithForeground(color Color) Style
- func (style Style) WithHyperlink(hyperlinkURL *string) Style
- func (style Style) WithUnderlineColor(color Color) Style
- func (style Style) WithoutAttr(attr AttrMask) Style
- type StyledRune
- type UnixScreen
- func (screen *UnixScreen) Clear()
- func (screen *UnixScreen) Close()
- func (screen *UnixScreen) Events() chan Event
- func (screen *UnixScreen) RequestTerminalBackgroundColor()
- func (screen *UnixScreen) SetCell(column int, row int, styledRune StyledRune) int
- func (screen *UnixScreen) Show()
- func (screen *UnixScreen) ShowCursorAt(column int, row int)
- func (screen *UnixScreen) ShowNLines(height int)
- func (screen *UnixScreen) Size() (width int, height int)
Constants ¶
This section is empty.
Variables ¶
var ColorDefault = newColor(ColorCountDefault, 0)
Reset to default foreground / background color
Functions ¶
Types ¶
type Color ¶
type Color uint32
Create using NewColor16(), NewColor256 or NewColor24Bit(), or use ColorDefault.
func NewColor16 ¶
Four bit colors as defined here: https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
func NewColor256 ¶
func NewColorHex ¶
func (Color) ColorCount ¶ added in v1.26.0
func (color Color) ColorCount() ColorCount
func (Color) Distance ¶ added in v1.20.0
Wrapper for Chroma's color distance function.
That one says it uses this formula: https://www.compuphase.com/cmetric.htm
The result from this function has been scaled to 0.0-1.0, where 1.0 is the distance between black and white.
type ColorCount ¶ added in v1.26.0
type ColorCount uint8
const ( // Default foreground / background color ColorCountDefault ColorCount = iota // https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit // // Note that this type is only used for output, on input we store 3 bit // colors as 4 bit colors since they map to the same values. ColorCount8 // https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit ColorCount16 // https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit ColorCount256 // RGB: https://en.wikipedia.org/wiki/ANSI_escape_code#24-bit ColorCount24bit )
type EventExit ¶ added in v1.16.0
type EventExit struct { }
If we're unable to continue showing the screen, we'll send this event and drop out.
type EventKeyCode ¶
type EventKeyCode struct {
// contains filtered or unexported fields
}
func (*EventKeyCode) KeyCode ¶
func (eventKeyCode *EventKeyCode) KeyCode() KeyCode
type EventMouse ¶
type EventMouse struct {
// contains filtered or unexported fields
}
func (*EventMouse) Buttons ¶
func (eventMouse *EventMouse) Buttons() MouseButtonMask
type EventResize ¶
type EventResize struct { }
After you get this, query Screen.Size() to get the new size
type EventTerminalBackgroundDetected ¶ added in v1.23.0
type EventTerminalBackgroundDetected struct { // Terminal background color Color Color }
type FakeScreen ¶
type FakeScreen struct {
// contains filtered or unexported fields
}
Used for testing.
Try GetRow() after some SetCell() calls to see what you got.
func NewFakeScreen ¶
func NewFakeScreen(width int, height int) *FakeScreen
func (*FakeScreen) Clear ¶
func (screen *FakeScreen) Clear()
func (*FakeScreen) Close ¶
func (screen *FakeScreen) Close()
func (*FakeScreen) Events ¶
func (screen *FakeScreen) Events() chan Event
func (*FakeScreen) GetRow ¶
func (screen *FakeScreen) GetRow(row int) []StyledRune
func (*FakeScreen) RequestTerminalBackgroundColor ¶ added in v1.23.3
func (screen *FakeScreen) RequestTerminalBackgroundColor()
func (*FakeScreen) SetCell ¶
func (screen *FakeScreen) SetCell(column int, row int, styledRune StyledRune) int
func (*FakeScreen) Show ¶
func (screen *FakeScreen) Show()
func (*FakeScreen) ShowCursorAt ¶
func (screen *FakeScreen) ShowCursorAt(_ int, _ int)
func (*FakeScreen) ShowNLines ¶ added in v1.8.4
func (screen *FakeScreen) ShowNLines(int)
func (*FakeScreen) Size ¶
func (screen *FakeScreen) Size() (width int, height int)
type MouseButtonMask ¶
type MouseButtonMask uint16
const ( MouseWheelUp MouseButtonMask = 1 << iota MouseWheelDown MouseWheelLeft MouseWheelRight )
type MouseMode ¶ added in v1.19.0
type MouseMode int
const ( MouseModeAuto MouseMode = iota // Don't capture mouse events. This makes selecting with the mouse work. On // some terminals mouse scrolling will work using arrow keys emulation, and // on some not. MouseModeSelect // Capture mouse events. This makes mouse scrolling work. Special gymnastics // will be required for marking with the mouse to copy text. MouseModeScroll )
type Screen ¶
type Screen interface { // Close() restores terminal to normal state, must be called after you are // done with your screen Close() Clear() // Returns the width of the rune just added, in number of columns. // // Note that if you set a wide rune (like '午') in one column, then whatever // you put in the next column will be hidden by the wide rune. A wide rune // in the last screen column will be replaced by a space, to prevent it from // overflowing onto the next line. SetCell(column int, row int, styledRune StyledRune) int // Render our contents into the terminal window Show() // Can be called after Close()ing the screen to fake retaining its output. // Plain Show() is what you'd call during normal operation. ShowNLines(lineCountToShow int) // Returns screen width and height. // // NOTE: Never cache this response! On window resizes you'll get an // EventResize on the Screen.Events channel, and this method will start // returning the new size instead. Size() (width int, height int) // ShowCursorAt() moves the cursor to the given screen position and makes // sure it is visible. // // If the position is outside of the screen, the cursor will be hidden. ShowCursorAt(column int, row int) // RequestTerminalBackgroundColor() asks the terminal to report its // background color. // // If your terminal supports background color queries and it responds, the // result will be reported as an EventTerminalBackgroundDetected on the // Events() channel. RequestTerminalBackgroundColor() // This channel is what your main loop should be checking. Events() chan Event }
func NewScreen ¶
NewScreen() requires Close() to be called after you are done with your new screen, most likely somewhere in your shutdown code.
func NewScreenWithMouseMode ¶ added in v1.19.0
func NewScreenWithMouseModeAndColorCount ¶ added in v1.26.0
func NewScreenWithMouseModeAndColorCount(mouseMode MouseMode, terminalColorCount ColorCount) (Screen, error)
type Style ¶
type Style struct {
// contains filtered or unexported fields
}
var StyleDefault Style
func (Style) RenderUpdateFrom ¶
func (current Style) RenderUpdateFrom(previous Style, terminalColorCount ColorCount) string
Emit an ANSI escape sequence switching from a previous style to the current one.
func (Style) WithBackground ¶ added in v1.20.0
func (Style) WithForeground ¶ added in v1.20.0
func (Style) WithHyperlink ¶ added in v1.14.0
Call with nil to remove the link
func (Style) WithUnderlineColor ¶ added in v1.26.0
func (Style) WithoutAttr ¶
type StyledRune ¶ added in v1.27.2
StyledRune is a rune with a style to be written to a one or more cells on the screen. Note that a StyledRune may use more than one cell on the screen ('午' for example).
func NewStyledRune ¶ added in v1.27.2
func NewStyledRune(rune rune, style Style) StyledRune
func TrimSpaceLeft ¶ added in v1.7.1
func TrimSpaceLeft(runes []StyledRune) []StyledRune
Returns a slice of cells with leading whitespace cells removed
func TrimSpaceRight ¶ added in v1.7.1
func TrimSpaceRight(runes []StyledRune) []StyledRune
Returns a slice of cells with trailing whitespace cells removed
func (StyledRune) String ¶ added in v1.27.2
func (styledRune StyledRune) String() string
func (StyledRune) Width ¶ added in v1.27.2
func (styledRune StyledRune) Width() int
How many screen cells will this rune cover? Most runes cover one, but some like '午' will cover two.
type UnixScreen ¶
type UnixScreen struct {
// contains filtered or unexported fields
}
func (*UnixScreen) Clear ¶
func (screen *UnixScreen) Clear()
func (*UnixScreen) Close ¶
func (screen *UnixScreen) Close()
Close() restores terminal to normal state, must be called after you are done with the screen returned by NewScreen()
func (*UnixScreen) Events ¶
func (screen *UnixScreen) Events() chan Event
func (*UnixScreen) RequestTerminalBackgroundColor ¶ added in v1.23.3
func (screen *UnixScreen) RequestTerminalBackgroundColor()
func (*UnixScreen) SetCell ¶
func (screen *UnixScreen) SetCell(column int, row int, styledRune StyledRune) int
func (*UnixScreen) Show ¶
func (screen *UnixScreen) Show()
func (*UnixScreen) ShowCursorAt ¶
func (screen *UnixScreen) ShowCursorAt(column int, row int)
ShowCursorAt() moves the cursor to the given screen position and makes sure it is visible.
If the position is outside of the screen, the cursor will be hidden.
func (*UnixScreen) ShowNLines ¶ added in v1.8.4
func (screen *UnixScreen) ShowNLines(height int)
func (*UnixScreen) Size ¶
func (screen *UnixScreen) Size() (width int, height int)
Returns screen width and height.
NOTE: Never cache this response! On window resizes you'll get an EventResize on the Screen.Events channel, and this method will start returning the new size instead.