-
Notifications
You must be signed in to change notification settings - Fork 155
/
terminal.go
274 lines (231 loc) · 5.73 KB
/
terminal.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package jid
import (
"fmt"
"io"
"regexp"
"strings"
runewidth "github.com/mattn/go-runewidth"
termbox "github.com/nsf/termbox-go"
"github.com/nwidger/jsoncolor"
)
type Terminal struct {
defaultY int
prompt string
formatter *jsoncolor.Formatter
monochrome bool
outputArea *[][]termbox.Cell
}
type TerminalDrawAttributes struct {
Query string
Contents []string
CandidateIndex int
ContentsOffsetY int
Complete string
Candidates []string
CursorOffset int
}
func NewTerminal(prompt string, defaultY int, monochrome bool) *Terminal {
t := &Terminal{
prompt: prompt,
defaultY: defaultY,
monochrome: monochrome,
outputArea: &[][]termbox.Cell{},
formatter: nil,
}
if !monochrome {
t.formatter = t.initColorizeFormatter()
}
return t
}
func (t *Terminal) Draw(attr *TerminalDrawAttributes) error {
query := attr.Query
complete := attr.Complete
rows := attr.Contents
candidates := attr.Candidates
candidateidx := attr.CandidateIndex
contentOffsetY := attr.ContentsOffsetY
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
y := t.defaultY
_, h := termbox.Size()
t.drawFilterLine(query, complete)
if len(candidates) > 0 {
y = t.drawCandidates(0, t.defaultY, candidateidx, candidates)
}
cellsArr, err := t.rowsToCells(rows)
if err != nil {
return err
}
for idx, cells := range cellsArr {
i := idx - contentOffsetY
if i >= 0 {
t.drawCells(0, i+y, cells)
}
if i > h {
break
}
}
termbox.SetCursor(len(t.prompt)+attr.CursorOffset, 0)
termbox.Flush()
return nil
}
func (t *Terminal) drawFilterLine(qs string, complete string) error {
fs := t.prompt + qs
cs := complete
str := fs + cs
color := termbox.ColorDefault
backgroundColor := termbox.ColorDefault
var cells []termbox.Cell
match := []int{len(fs), len(fs + cs)}
var c termbox.Attribute
for i, s := range str {
c = color
if i >= match[0] && i < match[1] {
c = termbox.ColorGreen
}
cells = append(cells, termbox.Cell{
Ch: s,
Fg: c,
Bg: backgroundColor,
})
}
t.drawCells(0, 0, cells)
return nil
}
type termboxSprintfFuncer struct {
fg termbox.Attribute
bg termbox.Attribute
outputArea *[][]termbox.Cell
}
func (tsf *termboxSprintfFuncer) SprintfFunc() func(format string, a ...interface{}) string {
return func(format string, a ...interface{}) string {
cells := tsf.outputArea
idx := len(*cells) - 1
str := fmt.Sprintf(format, a...)
for _, s := range str {
if s == '\n' {
*cells = append(*cells, []termbox.Cell{})
idx++
continue
}
(*cells)[idx] = append((*cells)[idx], termbox.Cell{
Ch: s,
Fg: tsf.fg,
Bg: tsf.bg,
})
}
return "dummy"
}
}
func (t *Terminal) initColorizeFormatter() *jsoncolor.Formatter {
formatter := jsoncolor.NewFormatter()
regular := &termboxSprintfFuncer{
fg: termbox.ColorDefault,
bg: termbox.ColorDefault,
outputArea: t.outputArea,
}
bold := &termboxSprintfFuncer{
fg: termbox.AttrBold,
bg: termbox.ColorDefault,
outputArea: t.outputArea,
}
blueBold := &termboxSprintfFuncer{
fg: termbox.ColorBlue | termbox.AttrBold,
bg: termbox.ColorDefault,
outputArea: t.outputArea,
}
green := &termboxSprintfFuncer{
fg: termbox.ColorGreen,
bg: termbox.ColorDefault,
outputArea: t.outputArea,
}
blackBold := &termboxSprintfFuncer{
fg: termbox.ColorBlack | termbox.AttrBold,
bg: termbox.ColorDefault,
outputArea: t.outputArea,
}
formatter.SpaceColor = regular
formatter.CommaColor = bold
formatter.ColonColor = bold
formatter.ObjectColor = bold
formatter.ArrayColor = bold
formatter.FieldQuoteColor = blueBold
formatter.FieldColor = blueBold
formatter.StringQuoteColor = green
formatter.StringColor = green
formatter.TrueColor = regular
formatter.FalseColor = regular
formatter.NumberColor = regular
formatter.NullColor = blackBold
return formatter
}
func (t *Terminal) rowsToCells(rows []string) ([][]termbox.Cell, error) {
*t.outputArea = [][]termbox.Cell{[]termbox.Cell{}}
var err error
if t.formatter != nil {
err = t.formatter.Format(io.Discard, []byte(strings.Join(rows, "\n")))
}
cells := *t.outputArea
if err != nil || t.monochrome {
cells = [][]termbox.Cell{}
for _, row := range rows {
var cls []termbox.Cell
for _, char := range row {
cls = append(cls, termbox.Cell{
Ch: char,
Fg: termbox.ColorDefault,
Bg: termbox.ColorDefault,
})
}
cells = append(cells, cls)
}
}
return cells, nil
}
func (t *Terminal) drawCells(x int, y int, cells []termbox.Cell) {
i := 0
for _, c := range cells {
termbox.SetCell(x+i, y, c.Ch, c.Fg, c.Bg)
w := runewidth.RuneWidth(c.Ch)
if w == 0 || w == 2 && runewidth.IsAmbiguousWidth(c.Ch) {
w = 1
}
i += w
}
}
func (t *Terminal) drawCandidates(x int, y int, index int, candidates []string) int {
color := termbox.ColorBlack
backgroundColor := termbox.ColorWhite
w, _ := termbox.Size()
ss := candidates[index]
re := regexp.MustCompile("[[:space:]]" + regexp.QuoteMeta(ss) + "[[:space:]]")
var rows []string
var str string
for _, word := range candidates {
combine := " "
if l := len(str); l+len(word)+1 >= w {
rows = append(rows, str+" ")
str = ""
}
str += combine + word
}
rows = append(rows, str+" ")
for i, row := range rows {
match := re.FindStringIndex(row)
var c termbox.Attribute
ii := 0
for k, s := range row {
c = color
backgroundColor = termbox.ColorMagenta
if match != nil && k >= match[0]+1 && k < match[1]-1 {
backgroundColor = termbox.ColorWhite
}
termbox.SetCell(x+ii, y+i, s, c, backgroundColor)
w := runewidth.RuneWidth(s)
if w == 0 || w == 2 && runewidth.IsAmbiguousWidth(s) {
w = 1
}
ii += w
}
}
return y + len(rows)
}