-
Notifications
You must be signed in to change notification settings - Fork 75
/
utils.go
413 lines (361 loc) · 7.49 KB
/
utils.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package main
import (
"bytes"
"github.com/nsf/tulib"
"os"
"path/filepath"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
var invisible_rune_table = []rune{
'@', // 0
'A', // 1
'B', // 2
'C', // 3
'D', // 4
'E', // 5
'F', // 6
'G', // 7
'H', // 8
'I', // 9
'J', // 10
'K', // 11
'L', // 12
'M', // 13
'N', // 14
'O', // 15
'P', // 16
'Q', // 17
'R', // 18
'S', // 19
'T', // 20
'U', // 21
'V', // 22
'W', // 23
'X', // 24
'Y', // 25
'Z', // 26
'[', // 27
'\\', // 28
']', // 29
'^', // 30
'_', // 31
}
func make_godit_default_label_params() tulib.LabelParams {
lp := tulib.DefaultLabelParams
lp.Ellipsis = '~'
return lp
}
var default_label_params = make_godit_default_label_params()
// somewhat close to what wcwidth does, except rune_width doesn't return 0 or
// -1, it's always 1 or 2
func rune_width(r rune) int {
if r >= 0x1100 &&
(r <= 0x115f || r == 0x2329 || r == 0x232a ||
(r >= 0x2e80 && r <= 0xa4cf && r != 0x303f) ||
(r >= 0xac00 && r <= 0xd7a3) ||
(r >= 0xf900 && r <= 0xfaff) ||
(r >= 0xfe30 && r <= 0xfe6f) ||
(r >= 0xff00 && r <= 0xff60) ||
(r >= 0xffe0 && r <= 0xffe6) ||
(r >= 0x20000 && r <= 0x2fffd) ||
(r >= 0x30000 && r <= 0x3fffd)) {
return 2
}
return 1
}
func rune_advance_len(r rune, pos int) int {
switch {
case r == '\t':
return tabstop_length - pos%tabstop_length
case r < 32:
// for invisible chars like ^R ^@ and such, two cells
return 2
}
return rune_width(r)
}
func vlen(data []byte, pos int) int {
origin := pos
for len(data) > 0 {
r, rlen := utf8.DecodeRune(data)
data = data[rlen:]
pos += rune_advance_len(r, pos)
}
return pos - origin
}
func iter_nonspace_words(data []byte, cb func(word []byte)) {
for {
for len(data) > 0 && is_space(data[0]) {
data = data[1:]
}
if len(data) == 0 {
return
}
i := 0
for i < len(data) && !is_space(data[i]) {
i += 1
}
cb(data[:i])
data = data[i:]
}
}
func iter_words(data []byte, cb func(word []byte)) {
for {
if len(data) == 0 {
return
}
r, rlen := utf8.DecodeRune(data)
// skip non-word runes
for !is_word(r) {
data = data[rlen:]
if len(data) == 0 {
return
}
r, rlen = utf8.DecodeRune(data)
}
// must be on a word rune
i := 0
for is_word(r) && i < len(data) {
i += rlen
r, rlen = utf8.DecodeRune(data[i:])
}
cb(data[:i])
data = data[i:]
}
}
func iter_words_backward(data []byte, cb func(word []byte)) {
for {
if len(data) == 0 {
return
}
r, rlen := utf8.DecodeLastRune(data)
// skip non-word runes
for !is_word(r) {
data = data[:len(data)-rlen]
if len(data) == 0 {
return
}
r, rlen = utf8.DecodeLastRune(data)
}
// must be on a word rune
i := len(data)
for is_word(r) && i > 0 {
i -= rlen
r, rlen = utf8.DecodeLastRune(data[:i])
}
cb(data[i:])
data = data[:i]
}
}
func readdir_stat(dir string, f *os.File) ([]os.FileInfo, error) {
names, err := f.Readdirnames(-1)
if err != nil {
return nil, err
}
fis := make([]os.FileInfo, 0, len(names))
for _, name := range names {
fi, err := os.Stat(filepath.Join(dir, name))
if err != nil {
continue
}
fis = append(fis, fi)
}
return fis, nil
}
func index_first_non_space(s []byte) int {
for i := 0; i < len(s); i++ {
if s[i] != '\t' && s[i] != ' ' {
return i
}
}
return len(s)
}
func index_last_non_space(s []byte) int {
for i := len(s) - 1; i >= 0; i-- {
if s[i] != '\t' && s[i] != ' ' {
return i
}
}
return -1
}
func abs_path(filename string) string {
path, err := filepath.Abs(filename)
if err != nil {
panic(err)
}
return path
}
func grow_byte_slice(s []byte, desired_cap int) []byte {
if cap(s) < desired_cap {
ns := make([]byte, len(s), desired_cap)
copy(ns, s)
return ns
}
return s
}
func insert_bytes(s []byte, offset int, data []byte) []byte {
n := len(s) + len(data)
s = grow_byte_slice(s, n)
s = s[:n]
copy(s[offset+len(data):], s[offset:])
copy(s[offset:], data)
return s
}
func copy_byte_slice(dst, src []byte) []byte {
if cap(dst) < len(src) {
dst = clone_byte_slice(src)
}
dst = dst[:len(src)]
copy(dst, src)
return dst
}
func clone_byte_slice(s []byte) []byte {
c := make([]byte, len(s))
copy(c, s)
return c
}
// assumes the same line and a.boffset < b.offset order
func bytes_between(a, b cursor_location) []byte {
return a.line.data[a.boffset:b.boffset]
}
func is_word(r rune) bool {
return r == '_' || unicode.IsLetter(r) || unicode.IsNumber(r)
}
func is_space(b byte) bool {
return b == ' ' || b == '\t' || b == '\n'
}
func find_place_for_rect(win, pref tulib.Rect) tulib.Rect {
var vars [4]tulib.Rect
vars[0] = pref.Intersection(win)
if vars[0] == pref {
// this is just a common path, everything fits in
return pref
}
// If a rect doesn't fit in the window, try to select the most
// optimal position amongst mirrored variants.
// invert X
vars[1] = pref
vars[1].X = win.Width - pref.Width
vars[1] = vars[1].Intersection(win)
// invert Y
vars[2] = pref
vars[2].Y -= pref.Height + 1
vars[2] = vars[2].Intersection(win)
// invert X and Y
vars[3] = pref
vars[3].X = win.Width - pref.Width
vars[3].Y -= pref.Height + 1
vars[3] = vars[3].Intersection(win)
optimal_i, optimal_w, optimal_h := 0, 0, 0
// find optimal width
for i := 0; i < 4; i++ {
if vars[i].Width > optimal_w {
optimal_w = vars[i].Width
}
}
// find optimal height (amongst optimal widths) and its index
for i := 0; i < 4; i++ {
if vars[i].Width != optimal_w {
continue
}
if vars[i].Height > optimal_h {
optimal_h = vars[i].Height
optimal_i = i
}
}
return vars[optimal_i]
}
// Function will iterate 'data' contents, calling 'cb' on some data or on '\n',
// but never both. For example, given this data: "\n123\n123\n\n", it will call
// 'cb' 6 times: ['\n', '123', '\n', '123', '\n', '\n']
func iter_lines(data []byte, cb func([]byte)) {
offset := 0
for {
if offset == len(data) {
return
}
i := bytes.IndexByte(data[offset:], '\n')
switch i {
case -1:
cb(data[offset:])
return
case 0:
cb(data[offset : offset+1])
offset++
continue
}
cb(data[offset : offset+i])
cb(data[offset+i : offset+i+1])
offset += i + 1
}
}
var double_comma = []byte(",,")
func split_double_csv(data []byte) (a, b []byte) {
i := bytes.Index(data, double_comma)
if i == -1 {
return data, nil
}
return data[:i], data[i+2:]
}
type line_reader struct {
data []byte
offset int
}
func new_line_reader(data []byte) line_reader {
return line_reader{data, 0}
}
func (l *line_reader) read_line() []byte {
data := l.data[l.offset:]
i := bytes.Index(data, []byte{'\n'})
if i == -1 {
l.offset = len(l.data)
return data
}
l.offset += i + 1
return data[:i]
}
func atoi(data []byte) (int, error) {
return strconv.Atoi(string(data))
}
func substitute_home(path string) string {
if !strings.HasPrefix(path, "~") {
return path
}
home := os.Getenv("HOME")
if home == "" {
panic("HOME is not set")
}
return filepath.Join(home, path[1:])
}
func substitute_symlinks(path string) string {
if path == "" {
return ""
}
after, err := filepath.EvalSymlinks(path)
if err != nil {
return path
}
if strings.HasSuffix(path, string(filepath.Separator)) {
return after + string(filepath.Separator)
}
return after
}
func is_file_hidden(path string) bool {
if path == "." || path == ".." {
return true
}
if len(path) > 1 {
if strings.HasPrefix(path, "./") {
return false
}
if strings.HasPrefix(path, "..") {
return false
}
if strings.HasPrefix(path, ".") {
return true
}
}
return false
}