/
draw.go
101 lines (93 loc) · 2.62 KB
/
draw.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
package main
import (
"github.com/gdamore/tcell/v2"
"github.com/kybin/tor/cell"
"github.com/kybin/tor/syntax"
"github.com/mattn/go-runewidth"
)
func SetCell(s tcell.Screen, l, o int, r rune, style tcell.Style) {
s.SetContent(o, l, r, nil, style)
}
// draw text inside of window at mainarea.
func drawScreen(s tcell.Screen, norm *NormalMode) {
w := norm.area.Win
// parse syntax
if norm.dirty {
norm.parser.ClearFrom(cell.Pt{L: w.Min().L, O: 0})
norm.dirty = false
}
norm.parser.ParseTo(cell.Pt{L: w.Max().L + 1, O: 0})
// draw
for l, ln := range norm.text.lines {
if l < w.Min().L || l >= w.Max().L {
continue
}
origStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
o := 0
for b, r := range ln.data {
if o >= w.Max().O {
break
}
style := origStyle
for _, m := range norm.parser.Matches {
if m.Range.Contains(cell.Pt{l, b}) {
attr, ok := syntax.DefaultTheme[m.Type]
if ok {
style = tcell.StyleDefault.Background(attr.Bg).Foreground(attr.Fg)
}
break
}
}
if norm.selection.Contains(cell.Pt{l, b}) {
style = tcell.StyleDefault.Background(tcell.ColorGreen).Foreground(tcell.ColorReset)
}
if r == '\t' {
for i := 0; i < norm.text.tabWidth; i++ {
if o >= w.Min().O {
SetCell(s, l-w.Min().L, o-w.Min().O+norm.area.min.O, rune(' '), style)
}
o += 1
}
} else {
width := runewidth.RuneWidth(r)
if width == 0 {
// It's illegal character in tor. Shade the character so we can see it easily.
// See vlen function in stringutil.go as well.
r = '\u2591'
width = 1
}
if o >= w.Min().O {
SetCell(s, l-w.Min().L, o-w.Min().O+norm.area.min.O, rune(r), style)
}
o += width
}
}
// set original color to the last cell. (white and black)
// if not set, the cursor's color will look different.
SetCell(s, l-w.Min().L, o-w.Min().O+norm.area.min.O, rune(' '), origStyle)
}
}
// drawStatus draws current status of m at bottom of terminal.
// If m has Error, it will printed with red background.
func drawStatus(s tcell.Screen, m Mode) {
var style tcell.Style
var status string
if m.Error() != "" {
style = tcell.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack)
status = m.Error()
} else {
style = tcell.StyleDefault.Background(tcell.ColorWhite).Foreground(tcell.ColorBlack)
status = m.Status()
}
termw, termh := s.Size()
statusLine := termh - 1
// clear and draw
for i := 0; i < termw; i++ {
SetCell(s, statusLine, i, ' ', style)
}
o := 0
for _, r := range status {
SetCell(s, statusLine, o, r, style)
o += runewidth.RuneWidth(r)
}
}