Debian Codegraph @

debian/vendor/github.com/micro-editor/terminal/vt_other.go

raw
1 //go:build plan9 || nacl || windows
2
3 package terminal
4
5 import (
6 "bufio"
7 "bytes"
8 "errors"
9 "io"
10 "os"
11 "os/exec"
12 "unicode"
13 "unicode/utf8"
14 )
15
16 // VT represents the virtual terminal emulator.
17 type VT struct {
18 dest *State
19 rc io.ReadCloser
20 br *bufio.Reader
21 pty *os.File
22 }
23
24 // Start initializes a virtual terminal emulator with the target state
25 // and a new pty file by starting the *exec.Command. The returned
26 // *os.File is the pty file.
27 func Start(state *State, cmd *exec.Cmd) (*VT, *os.File, error) {
28 return nil, nil, errors.New("Unsupported operating system")
29 }
30
31 // Create initializes a virtual terminal emulator with the target state
32 // and io.ReadCloser input.
33 func Create(state *State, rc io.ReadCloser) (*VT, error) {
34 t := &VT{
35 dest: state,
36 rc: rc,
37 }
38 t.init()
39 return t, nil
40 }
41
42 func (t *VT) init() {
43 t.br = bufio.NewReader(t.rc)
44 t.dest.numlock = true
45 t.dest.state = t.dest.parse
46 t.dest.cur.attr.fg = DefaultFG
47 t.dest.cur.attr.bg = DefaultBG
48 t.Resize(80, 24)
49 t.dest.reset()
50 }
51
52 // File returns the pty file.
53 func (t *VT) File() *os.File {
54 return t.pty
55 }
56
57 // Write parses input and writes terminal changes to state.
58 func (t *VT) Write(p []byte) (int, error) {
59 var written int
60 r := bytes.NewReader(p)
61 t.dest.lock()
62 defer t.dest.unlock()
63 for {
64 c, sz, err := r.ReadRune()
65 if err != nil {
66 if err == io.EOF {
67 break
68 }
69 return written, err
70 }
71 written += sz
72 if c == unicode.ReplacementChar && sz == 1 {
73 if r.Len() == 0 {
74 // not enough bytes for a full rune
75 return written - 1, nil
76 }
77 t.dest.logln("invalid utf8 sequence")
78 continue
79 }
80 t.dest.put(c)
81 }
82 return written, nil
83 }
84
85 // Close closes the pty or io.ReadCloser.
86 func (t *VT) Close() error {
87 return t.rc.Close()
88 }
89
90 // Parse blocks on read on pty or io.ReadCloser, then parses sequences until
91 // buffer empties. State is locked as soon as first rune is read, and unlocked
92 // when buffer is empty.
93 // TODO: add tests for expected blocking behavior
94 func (t *VT) Parse() error {
95 var locked bool
96 defer func() {
97 if locked {
98 t.dest.unlock()
99 }
100 }()
101 for {
102 c, sz, err := t.br.ReadRune()
103 if err != nil {
104 return err
105 }
106 if c == unicode.ReplacementChar && sz == 1 {
107 t.dest.logln("invalid utf8 sequence")
108 break
109 }
110 if !locked {
111 t.dest.lock()
112 locked = true
113 }
114
115 // put rune for parsing and update state
116 t.dest.put(c)
117
118 // break if our buffer is empty, or if buffer contains an
119 // incomplete rune.
120 n := t.br.Buffered()
121 if n == 0 || (n < 4 && !fullRuneBuffered(t.br)) {
122 break
123 }
124 }
125 return nil
126 }
127
128 func fullRuneBuffered(br *bufio.Reader) bool {
129 n := br.Buffered()
130 buf, err := br.Peek(n)
131 if err != nil {
132 return false
133 }
134 return utf8.FullRune(buf)
135 }
136
137 // Resize reports new size to pty and updates state.
138 func (t *VT) Resize(cols, rows int) {
139 t.dest.lock()
140 defer t.dest.unlock()
141 _ = t.dest.resize(cols, rows)
142 t.ptyResize()
143 }