-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender.go
260 lines (238 loc) · 6.05 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
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
package main
import (
"fmt"
"math"
"strconv"
"sync"
"unicode/utf8"
termutil "github.com/japanoise/termbox-util"
"github.com/mattn/go-runewidth"
"github.com/nsf/termbox-go"
)
var redrawLock *sync.Mutex
func init() {
redrawLock = &sync.Mutex{}
}
func nextTabStop(rx int) int {
return Global.Tabsize - rx%Global.Tabsize
}
func (row *EditorRow) cxToRx(cx int) int {
rx := 0
for i, rv := range row.Data {
if i >= cx {
break
}
if rv == '\t' {
rx += nextTabStop(rx)
} else {
rx += termutil.Runewidth(rv)
}
}
return rx
}
func editorRowCxToRx(row *EditorRow) int {
return row.cxToRx(Global.CurrentB.cx)
}
func editorRowRxToCx(row *EditorRow, rx int) int {
cur_rx := 0
var cx int
for cx = 0; cx < row.Size; {
rv, len := utf8.DecodeRuneInString(row.Data[cx:])
if rv == '\t' {
cur_rx += nextTabStop(rx)
} else {
cur_rx += termutil.Runewidth(rv)
}
if cur_rx > rx {
return cx
}
cx += len
}
return cx
}
func editorRefreshScreen() {
redrawLock.Lock()
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
sx, sy := termbox.Size()
Global.WindowTree.draw(0, 0, sx, sy-2)
editorDrawPrompt(sy)
termbox.Flush()
redrawLock.Unlock()
}
func trimString(s string, coloff int) (string, int) {
if coloff == 0 {
return s, 0
}
if coloff < len(s) {
rw := 0
for i, ru := range s {
if rw >= coloff {
return s[i:], i
}
rw += termutil.Runewidth(ru)
}
}
return "", 0
}
func editorDrawRows(startx, starty, sx, sy int, buf *EditorBuffer, gutsize int) {
for y := starty; y < sy; y++ {
filerow := (y - starty) + buf.rowoff
if filerow >= buf.NumRows {
if buf.hasMode("tilde-mode") {
termbox.SetCell(startx+gutsize, y, '~', termbox.ColorBlue, termbox.ColorDefault)
}
} else {
row := buf.Rows[filerow]
if gutsize > 0 {
termutil.Printstring(runewidth.FillLeft(LineNrToString(buf.Rows[filerow].idx+1), gutsize-2), startx, y)
termutil.PrintRune(startx+gutsize-2, y, '│', termbox.ColorDefault)
if row.coloff > 0 {
termutil.PrintRune(startx+gutsize-1, y, '←', termbox.ColorDefault)
}
}
if row.coloff < row.RenderSize {
ts, off := trimString(row.Render, row.coloff)
row.Print(startx+gutsize, y, row.coloff, off, sx-gutsize, ts, buf)
}
if row.coloff > 0 && gutsize == 0 {
termutil.PrintRune(startx, y, '←', termbox.ColorDefault)
}
}
}
}
func editorDrawRowsFocused(startx, starty, sx, sy int, buf *EditorBuffer, gutsize int) {
termbox.SetCursor(startx, starty)
for y := starty; y < sy; y++ {
filerow := (y - starty) + buf.rowoff
if filerow >= buf.NumRows {
if buf.hasMode("tilde-mode") {
termbox.SetCell(startx+gutsize, y, '~', termbox.ColorBlue, termbox.ColorDefault)
}
} else {
row := buf.Rows[filerow]
if gutsize > 0 {
termutil.Printstring(runewidth.FillLeft(LineNrToString(buf.Rows[filerow].idx+1), gutsize-2), startx, y)
termutil.PrintRune(startx+gutsize-2, y, '│', termbox.ColorDefault)
if row.coloff > 0 {
termutil.PrintRune(startx+gutsize-1, y, '←', termbox.ColorDefault)
}
}
if filerow == buf.cy {
termbox.SetCursor(startx+gutsize, y)
}
if row.coloff < row.RenderSize {
ts, off := trimString(row.Render, row.coloff)
if filerow == buf.cy {
row.PrintWCursor(startx+gutsize, y, row.coloff, off, sx-gutsize, ts, buf)
} else {
row.Print(startx+gutsize, y, row.coloff, off, sx-gutsize, ts, buf)
}
}
if row.coloff > 0 && gutsize == 0 {
termutil.PrintRune(startx, y, '←', termbox.ColorDefault)
}
}
}
}
func editorUpdateStatus(buf *EditorBuffer) string {
fn := buf.getRenderName()
dc := '-'
if buf.Dirty {
dc = '*'
}
if buf.hasMode("column-bytes-mode") || buf.NumRows == 0 {
return fmt.Sprintf("-%c %s - (%s) %d:%d", dc, fn, buf.MajorMode,
buf.cy+1, buf.cx)
}
return fmt.Sprintf("-%c %s - (%s) %d:%d", dc, fn, buf.MajorMode,
buf.cy+1, buf.Rows[buf.cy].cxToRx(buf.cx))
}
func GetScreenSize() (int, int) {
x, _ := termbox.Size()
return x, Global.CurrentBHeight
}
func editorDrawStatusLine(x, y, wx int, t *winTree) {
buf := t.buf
// Get & draw the standard Emacs line
line := editorUpdateStatus(buf)
rx := x
maxrx := x + wx
for _, ru := range line {
termbox.SetCell(rx, y, ru,
termbox.ColorDefault|termbox.AttrReverse,
termbox.ColorDefault)
rx += termutil.Runewidth(ru)
if rx >= maxrx {
// Truncate if it's too long
termbox.SetCell(rx, y, ' ',
termbox.ColorDefault|termbox.AttrReverse,
termbox.ColorDefault)
return
}
}
termbox.SetCell(rx, y, ' ', termbox.ColorDefault|termbox.AttrReverse,
termbox.ColorDefault)
// Draw some flexi space
var ix int
for ix = rx + 1; ix < maxrx-7; ix++ {
if t.focused {
termbox.SetCell(ix, y, '-',
termbox.ColorDefault|termbox.AttrReverse,
termbox.ColorDefault)
} else {
termbox.SetCell(ix, y, ' ',
termbox.ColorDefault|termbox.AttrReverse,
termbox.ColorDefault)
}
}
// Draw the end label (%age through the buffer)
el := calcEndLabel(buf)
for _, ru := range el {
termbox.SetCell(ix, y, ru,
termbox.ColorDefault|termbox.AttrReverse,
termbox.ColorDefault)
ix++
if ix >= maxrx {
// Truncate if it's too long
return
}
}
for ix < maxrx {
if t.focused {
termbox.SetCell(ix, y, '-',
termbox.ColorDefault|termbox.AttrReverse,
termbox.ColorDefault)
} else {
termbox.SetCell(ix, y, ' ',
termbox.ColorDefault|termbox.AttrReverse,
termbox.ColorDefault)
}
ix++
}
}
func calcEndLabel(buf *EditorBuffer) string {
if buf.NumRows == 0 {
return " Emp "
} else if Global.CurrentBHeight >= buf.NumRows {
return " All "
} else if buf.rowoff+Global.CurrentBHeight >= buf.NumRows {
return " Bot "
} else if buf.rowoff == 0 {
return " Top "
} else {
perc := float64(buf.rowoff) / float64(buf.NumRows)
return fmt.Sprintf(" %2d%% ", int(perc*100))
}
}
func editorDrawPrompt(y int) {
termutil.Printstring(Global.Prompt+"-> "+Global.Input, 0, y-1)
}
func NumStrWidth(num int) int {
return int(math.Log10(float64(num))) + 1
}
func GetGutterWidth(NumRows int) int {
return NumStrWidth(NumRows) + 2
}
func LineNrToString(num int) string {
return strconv.Itoa(num)
}