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
|
package main
import (
"github.com/gdamore/tcell"
)
type View struct {
x, y int // position
w, h int
rows []int // cursor offset for beginning of each row
style tcell.Style
text *Text
scrollpos int // bytes to skip when drawing content
opos int // overflow
tabstop int
history History
focused bool
dirty bool
}
func (b *View) Write(p []byte) (int, error) {
n, err := b.text.Write(p)
if err != nil {
return 0, err
}
b.dirty = true
return n, err
}
func (b *View) Delete() (int, error) {
// Do not allow deletion beyond what we can see.
// This forces the user to scroll to visible content.
if b.text.cursorpos == b.scrollpos {
return 0, nil //silent return
}
n, err := b.text.Delete()
if err != nil {
return n, err
}
b.dirty = true
return n, nil
}
func (b *View) SetStyle(style tcell.Style) {
b.style = style
}
func (b *View) Resize(x, y, w, h int) {
b.x = x
b.y = y
b.w = w
b.h = h
}
func (b *View) Size() (x, y, w, h int) {
return b.x, b.y, b.w, b.h
}
// Byte returns the current byte under the cursor.
func (b *View) Byte() byte {
c, _ := b.text.buf.ByteAt(b.text.cursorpos)
return c
}
func (b *View) Overflow() int {
return b.opos
}
func (b *View) Cursor() int {
return b.text.cursorpos
}
func (b *View) SetCursor(pos, whence int) {
b.text.SetCursor(pos, whence)
// // scroll to cursor if out of screen
// if b.Cursor() < b.scrollpos || b.Cursor() >= b.Overflow() {
// //if b.Cursor() != b.buf.Len() { // do not autoscroll on +1 last byte
// b.ScrollTo(b.Cursor())
// //}
// }
}
// XYToOffset translates mouse coordinates in a 2D terminal to the correct byte offset in buffer.
func (b *View) XYToOffset(x, y int) int {
offset := b.scrollpos
for y-b.y > 0 {
c, _ := b.text.buf.ByteAt(offset)
if c == '\n' {
offset++
y--
continue
}
xw := 0
for ; c != '\n' && xw < b.x+b.w; offset++ {
c, _ = b.text.buf.ByteAt(offset)
xw++
}
y--
}
for x-b.x > 0 {
c, _ := b.text.buf.ByteAt(offset)
if c == '\n' {
break
}
if c == '\t' {
x -= b.tabstop - 1
}
offset++
x--
}
return offset
}
// Scroll will move the visible part of the buffer in number of lines. Negative means upwards.
func (b *View) Scroll(n int) {
offset := 0
if n > 0 {
for n > 0 {
if c, _ := b.text.buf.ByteAt(b.scrollpos + offset); c == '\n' {
offset++
} else {
offset += b.text.NextNL(b.scrollpos+offset) + 1
}
n--
}
b.scrollpos += offset
}
if n < 0 {
offset += b.text.PrevNL(b.scrollpos)
for n < 0 {
offset += b.text.PrevNL(b.scrollpos - offset)
n++
}
if b.scrollpos-offset > 0 {
b.scrollpos -= offset - 1
} else {
b.scrollpos = 0
}
}
// boundaries
if b.scrollpos < 0 {
b.scrollpos = 0
}
if b.scrollpos > b.text.buf.Len() {
b.scrollpos = b.text.buf.Len()
}
}
// ScrollTo will scroll to an absolute byte offset in the buffer and backwards to the nearest previous newline.
func (b *View) ScrollTo(offset int) {
offset -= b.text.PrevNL(offset)
if offset > 0 {
offset += 1
}
b.scrollpos = offset
b.Scroll(-(b.h / 3)) // scroll a third page more for context
}
func (b *View) Draw() {
x, y := b.x, b.y
// clear
for y := b.y; y <= b.h; y++ {
for x := b.x; x < b.w; x++ {
//draw vertical line separator
if b.x > 0 && x == b.x {
screen.SetContent(x, y, '|', nil, separatorStyle)
} else {
screen.SetContent(x, y, ' ', nil, b.style)
}
}
}
if b.text.buf.Len() > 0 {
b.opos = b.scrollpos // keep track of last visible char/overflow
for i := b.scrollpos; i < b.text.buf.Len(); i++ {
// line wrap
if x == b.x+b.w {
y += 1
x = b.x
}
// jump past separator if needed
// if b.x > 0 && x == b.x {
// x++
// }
// default style
style := b.style
// highlight cursor
if i == b.text.cursorpos && b.focused {
style = cursorStyle
}
// draw byte from buffer
c, err := b.text.buf.ByteAt(i)
if err != nil {
screen.SetContent(x, y, '?', nil, style)
printMsg("index out of range [%d]\n", i)
break
}
switch c {
case '\n': // linebreak
screen.SetContent(x, y, '\n', nil, style)
for j := x + 1; j < b.w; j++ {
screen.SetContent(j, y, ' ', nil, defStyle) // fill rest of line with blanks
}
y += 1
if y >= b.y+b.h {
break
}
x = b.x + b.w
for x > b.x {
screen.SetContent(x, y, ' ', nil, b.style)
x--
}
case '\t': // show tab in tabstop width
screen.SetContent(x, y, '\t', nil, style)
x++
for j := 0; j < b.tabstop-1; j++ {
screen.SetContent(x, y, ' ', nil, b.style)
x++
}
default:
screen.SetContent(x, y, rune(c), nil, style)
x += 1
}
b.opos++ // increment last visible char/overflow
// stop at bottom of box
if y >= b.y+b.h {
break
}
}
}
// remove visual cursor clutter and fill out last line
for w := b.w; w >= x; w-- {
screen.SetContent(w, y, ' ', nil, b.style)
}
// show cursor on EOF
if b.text.cursorpos == b.text.buf.Len() && b.focused {
screen.SetContent(x, y, ' ', nil, cursorStyle)
}
// //begin empty lines with tilde, if more than one
// if b.h > 1 {
// y++
// for ; y <= b.y+b.h; y++ {
// screen.SetContent(0, y, '~', nil, defStyle)
// //remove visual trailing clutter on line below
// x = b.x + b.w
// for x > 0 {
// screen.SetContent(x, y, ' ', nil, defStyle)
// x--
// }
// }
// }
}
|