/
glint_term.go
181 lines (140 loc) · 3.03 KB
/
glint_term.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package terminal
import (
"context"
"io"
"os"
"strings"
"sync"
"unicode"
"github.com/lab47/vterm/parser"
"github.com/lab47/vterm/screen"
"github.com/lab47/vterm/state"
"github.com/mitchellh/go-glint"
)
type glintTerm struct {
mu sync.Mutex
w io.Writer
scr *screen.Screen
ctx context.Context
cancel func()
output [][]rune
height, width int
wg sync.WaitGroup
parseErr error
scrollback [][]rune
full bool
}
func (t *glintTerm) Body(ctx context.Context) glint.Component {
t.mu.Lock()
defer t.mu.Unlock()
var cs []glint.Component
if t.full {
for _, row := range t.scrollback {
s := strings.TrimRightFunc(string(row), unicode.IsSpace)
cs = append(cs, glint.Layout(glint.Text(" │ "), glint.Text(s)).Row())
}
}
for _, row := range t.output {
cs = append(cs, glint.Layout(
glint.Text(" │ "),
glint.Style(
glint.Text(strings.TrimRightFunc(string(row), unicode.IsSpace)),
glint.Color("lightBlue"),
),
).Row())
}
return glint.Fragment(cs...)
}
func (t *glintTerm) DamageDone(r state.Rect, cr screen.CellReader) error {
t.mu.Lock()
defer t.mu.Unlock()
for row := r.Start.Row; row <= r.End.Row; row++ {
for col := r.Start.Col; col <= r.End.Col; col++ {
cell := cr.GetCell(row, col)
for len(t.output) <= row {
t.output = append(t.output, make([]rune, t.width))
}
if cell == nil {
t.output[row][col] = ' '
} else {
val, _ := cell.Value()
if val == 0 {
t.output[row][col] = ' '
} else {
t.output[row][col] = val
}
}
}
}
return nil
}
func (t *glintTerm) MoveCursor(p state.Pos) error {
// Ignore it.
return nil
}
func (t *glintTerm) SetTermProp(attr state.TermAttr, val interface{}) error {
// Ignore it.
return nil
}
func (t *glintTerm) Output(data []byte) error {
// Ignore it.
return nil
}
func (t *glintTerm) StringEvent(kind string, data []byte) error {
// Ignore them.
return nil
}
func newGlintTerm(ctx context.Context, height, width int) (*glintTerm, error) {
term := &glintTerm{
height: height,
width: width,
}
scr, err := screen.NewScreen(height, width, term)
if err != nil {
return nil, err
}
term.scr = scr
st, err := state.NewState(height, width, scr)
if err != nil {
return nil, err
}
r, w, err := os.Pipe()
if err != nil {
return nil, err
}
term.w = w
prs, err := parser.NewParser(r, st)
if err != nil {
return nil, err
}
term.ctx, term.cancel = context.WithCancel(ctx)
term.wg.Add(1)
go func() {
defer term.wg.Done()
err := prs.Drive(term.ctx)
if err != nil && err != context.Canceled {
term.parseErr = err
}
}()
return term, nil
}
func (t *glintTerm) Write(b []byte) (int, error) {
return t.w.Write(b)
}
func (t *glintTerm) AddScrollBack(line []rune) error {
t.mu.Lock()
defer t.mu.Unlock()
t.scrollback = append(t.scrollback, line)
return nil
}
func (t *glintTerm) showFull() {
t.mu.Lock()
defer t.mu.Unlock()
t.full = true
}
var _ screen.ScrollBack = (*glintTerm)(nil)
func (t *glintTerm) Close() error {
t.cancel()
t.wg.Wait()
return t.parseErr
}