-
Notifications
You must be signed in to change notification settings - Fork 2
/
render.go
179 lines (151 loc) · 3.47 KB
/
render.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
package midterm
import (
"fmt"
"io"
"strings"
"time"
"github.com/muesli/termenv"
)
func (vt *Terminal) Render(w io.Writer) error {
vt.mut.Lock()
defer vt.mut.Unlock()
for i := 0; i < vt.Height; i++ {
if i > 0 {
fmt.Fprintln(w)
}
err := vt.renderLine(w, i)
if err != nil {
return err
}
}
return nil
}
func (vt *Terminal) RenderLine(w io.Writer, row int) error {
vt.mut.Lock()
defer vt.mut.Unlock()
return vt.renderLine(w, row)
}
type Line struct {
Content []rune
Format []Format
}
func (line Line) Display() string {
out := ""
var lastFormat Format
for col, r := range line.Content {
f := line.Format[col]
if f != lastFormat {
lastFormat = f
out += f.Render()
}
out += string(r)
}
return out
}
var ReverseFormat = Format{Properties: ReverseBit}
func (vt *Terminal) renderLine(w io.Writer, row int) error {
if row >= len(vt.Content) {
return fmt.Errorf("line %d exceeds content height", row)
}
var pos int
lastFormat := EmptyFormat
format := func(f Format) {
if lastFormat != f {
// TODO: this is probably a sane thing to do, but it makes picky tests
// fail; what if the last format set Italic? we need to reset it if the
// new format doesn't also set it.
// if lastFormat != EmptyFormat {
// fmt.Fprint(w, resetSeq)
// }
fmt.Fprint(w, f.Render())
lastFormat = f
}
}
for region := range vt.Format.Regions(row) {
line := vt.Content[row]
showCursor := vt.CursorVisible &&
row == vt.Cursor.Y &&
vt.Cursor.X >= pos &&
vt.Cursor.X < pos+region.Size &&
(vt.CursorBlinkEpoch == nil ||
int(time.Since(*vt.CursorBlinkEpoch).Seconds())%2 == 0)
if showCursor {
before := string(line[pos:vt.Cursor.X])
cursor := string(line[vt.Cursor.X])
after := string(line[vt.Cursor.X+1 : pos+region.Size])
if len(before) > 0 {
format(region.F)
fmt.Fprint(w, before)
}
invert := region.F
invert.SetReverse(!region.F.IsReverse())
format(invert)
fmt.Fprint(w, cursor)
if len(after) > 0 {
format(region.F)
fmt.Fprint(w, after)
}
} else {
format(region.F)
content := string(line[pos : pos+region.Size])
fmt.Fprint(w, content)
}
pos += region.Size
}
_, err := fmt.Fprint(w, resetSeq)
return err
}
const resetSeq = termenv.CSI + termenv.ResetSeq + "m"
func brighten(color termenv.Color) termenv.Color {
if ansi, ok := color.(termenv.ANSIColor); ok && ansi < termenv.ANSIBrightBlack {
return ansi + termenv.ANSIBrightBlack
} else {
return color
}
}
func (f Format) Render() string {
styles := []string{}
if f.IsBold() {
styles = append(styles, termenv.BoldSeq)
f.Fg = brighten(f.Fg)
} else if f.IsFaint() {
styles = append(styles, termenv.FaintSeq)
}
if f.Fg != nil {
styles = append(styles, f.Fg.Sequence(false))
}
if f.Bg != nil {
styles = append(styles, f.Bg.Sequence(true))
}
if f.IsItalic() {
styles = append(styles, termenv.ItalicSeq)
}
if f.IsUnderline() {
styles = append(styles, termenv.UnderlineSeq)
}
if f.IsBlink() {
styles = append(styles, termenv.BlinkSeq)
}
if f.IsReverse() {
styles = append(styles, termenv.ReverseSeq)
}
if f.IsConceal() {
styles = append(styles, "8")
}
// if f.IsCrossOut() {
// styles = append(styles, termenv.CrossOutSeq)
// }
//
// if f.IsOverline() {
// styles = append(styles, termenv.OverlineSeq)
// }
//
var res string
if f.IsReset() || f == (Format{}) {
res = resetSeq
}
if len(styles) > 0 {
res += fmt.Sprintf("%s%sm", termenv.CSI, strings.Join(styles, ";"))
}
return res
}