1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
package vt10x
import (
"bufio"
"fmt"
"io"
"io/ioutil"
)
// Terminal represents the virtual terminal emulator.
type Terminal interface {
// View displays the virtual terminal.
View
// Write parses input and writes terminal changes to state.
io.Writer
// Parse blocks on read on pty or io.Reader, then parses sequences until
// buffer empties. State is locked as soon as first rune is read, and unlocked
// when buffer is empty.
Parse(bf *bufio.Reader) error
}
// View represents the view of the virtual terminal emulator.
type View interface {
// String dumps the virtual terminal contents.
fmt.Stringer
// Size returns the size of the virtual terminal.
Size() (cols, rows int)
// Resize changes the size of the virtual terminal.
Resize(cols, rows int)
// Mode returns the current terminal mode.//
Mode() ModeFlag
// Title represents the title of the console window.
Title() string
// Cell returns the glyph containing the character code, foreground color, and
// background color at position (x, y) relative to the top left of the terminal.
Cell(x, y int) Glyph
// Cursor returns the current position of the cursor.
Cursor() Cursor
// CursorVisible returns the visible state of the cursor.
CursorVisible() bool
// Lock locks the state object's mutex.
Lock()
// Unlock resets change flags and unlocks the state object's mutex.
Unlock()
}
type TerminalOption func(*TerminalInfo)
type TerminalInfo struct {
w io.Writer
cols, rows int
}
func WithWriter(w io.Writer) TerminalOption {
return func(info *TerminalInfo) {
info.w = w
}
}
func WithSize(cols, rows int) TerminalOption {
return func(info *TerminalInfo) {
info.cols = cols
info.rows = rows
}
}
// New returns a new virtual terminal emulator.
func New(opts ...TerminalOption) Terminal {
info := TerminalInfo{
w: ioutil.Discard,
cols: 80,
rows: 24,
}
for _, opt := range opts {
opt(&info)
}
return newTerminal(info)
}
|