-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
view.v
909 lines (847 loc) · 18.6 KB
/
view.v
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license
// that can be found in the LICENSE file.
module main
import os
import strings
import gg
struct View {
mut:
padding_left int
from int
x int
y int
prev_x int
path string
short_path string
prev_path string // for tt
lines []string
page_height int
vstart int
vend int // visual borders
changed bool
error_y int
ved &Ved = unsafe { nil }
prev_y int
hash_comment bool
hl_on bool
breakpoints []int // line numbers with breakpoints
}
fn (ved &Ved) new_view() View {
res := View{
padding_left: 0
path: ''
from: 0
y: 0
x: 0
prev_x: 0
page_height: ved.page_height
vstart: -1
vend: -1
ved: ved
error_y: -1
prev_y: -1
}
return res
}
// `mut res := word.clone()` ==>
// ['mut' 'res' 'word' 'clone']
fn get_clean_words(line string) []string {
mut res := []string{}
mut i := 0
for i < line.len {
// Skip bad first
for i < line.len && !is_alpha_underscore(int(line[i])) {
i++
}
// Read all good
start2 := i
for i < line.len && is_alpha_underscore(int(line[i])) {
i++
}
// End of word, save it
word := line[start2..i]
res << word
i++
}
return res
}
fn (mut view View) open_file(path string, line_nr int) {
println('open file "${path}"')
if path == '' {
return
}
// This path is in current workspace? Trim it. /code/v/file.v => file.v
if path.starts_with(view.ved.workspace + '/') {
view.short_path = path[view.ved.workspace.len..]
if view.short_path.starts_with('/') {
view.short_path = view.short_path[1..]
}
} else {
// Optimize space, replace /Users/username with ~
if path.starts_with(home_dir) {
view.short_path = path.replace(home_dir, '~')
} else {
view.short_path = path
}
}
mut ved := view.ved
ved.set_current_syntax_idx(os.file_ext(path))
// if os.exists(view.short_path) &&
if view.short_path !in ['out', ''] && view.short_path !in ved.open_paths[ved.workspace_idx] {
if ved.open_paths[ved.workspace_idx].len == 0 {
ved.open_paths[ved.workspace_idx] = []string{cap: ved.nr_splits}
}
ved.open_paths[ved.workspace_idx] << view.short_path
}
if path != view.path {
// Save cursor pos (Y)
view.ved.file_y_pos[view.path] = view.y
view.prev_path = view.path
}
/*
mut lines := []string{}
if rlines := os.read_lines(path) {
lines = rlines
}
view.lines = lines
*/
view.lines = os.read_lines(path) or { []string{} }
// get words map
if view.lines.len < 1000 {
println('getting words')
// ticks := glfw.get_time()
for line in view.lines {
// words := line.split(' ')
words := get_clean_words(line)
for word in words {
// clean_word := get_clean_word(word)
// if clean_word == '' {
// continue
// }
if word !in ved.words {
ved.words << word
}
}
}
// took := glfw.get_time() - ticks
}
// Empty file, handle it
if view.lines.len == 0 {
view.lines << ''
}
view.path = path
// view.short_path = path.replace(view.ved.workspace, '')
// Calc padding_left
nr_lines := view.lines.len
s := '${nr_lines}'
view.padding_left = s.len * ved.cfg.char_width + 8
view.ved.save_session()
// Go to old y for this file
y := view.ved.file_y_pos[view.path]
if y > 0 {
view.set_y(y)
if path != view.path {
view.zz()
}
}
// Call zz() if it's out of bounds
if view.from > view.y || view.from + view.ved.page_height < view.y {
// view.zz()
}
if line_nr != 0 {
view.move_to_line(line_nr)
}
view.l()
view.h() // so that cursor pos is correct and doesn't point to no longer existing text
view.hash_comment = !view.path.ends_with('.v')
view.hl_on = !view.path.ends_with('.md') && !view.path.ends_with('.txt')
&& view.path.contains('.')
view.changed = false
view.ved.gg.refresh_ui()
// go view.ved.write_changes_in_file_every_5s()
}
fn (mut view View) reopen() {
view.open_file(view.path, 0)
view.changed = false
}
fn (mut view View) save_file() {
if view.path == '' {
return
}
path := view.path
view.ved.file_y_pos[view.path] = view.y
println('saving file "${path}"')
println('lines.len=${view.lines.len}')
// line0 := view.lines[0]
// println('line[0].len=$line0.len')
mut file := os.create(path) or { panic('fail') }
for line in view.lines {
file.writeln(line.trim_right(' \t')) or { panic(err) }
}
file.close()
spawn view.format_file()
// If another split has the same file open, update it
for mut v in view.ved.views {
if v.path == view.path {
v.reopen()
}
}
}
fn (mut view View) format_file() {
view.ved.load_config2()
if view.ved.cfg.disable_fmt {
return
}
path := view.path
// Run formatters
fmt_cmd := view.ved.syntaxes[view.ved.current_syntax_idx].fmt_cmd.replace('<PATH>',
os.quoted_path(path))
if path.ends_with('.go') {
println('running goimports')
os.system('goimports -w "${path}"')
} else if path.ends_with('.scss') {
css := path.replace('.scss', '.css')
os.system('sassc "${path}" > "${css}"')
} else if path.ends_with('.js') {
os.system('prettier --use-tabs -w "${path}"')
} else if fmt_cmd != '' {
os.system(fmt_cmd)
}
view.reopen()
// update git diff
view.ved.get_git_diff()
view.changed = false
// println('end of save file()')
// println('_lines.len=$view.lines.len')
// line0_ := view.lines[0]
// println('_line[0].len=$line0_.len')
}
fn (view &View) line() string {
if view.y < 0 || view.y >= view.lines.len {
return ''
}
return view.lines[view.y]
}
fn (view &View) uline() []rune {
return view.line().runes()
}
fn (view &View) char() int {
line := view.line()
if line.len > 0 && view.x < line.len {
return int(line[view.x])
}
return 0
}
fn (mut view View) set_line(newline string) {
if view.y + 1 > view.lines.len {
view.lines << newline
} else {
view.lines[view.y] = newline
}
view.changed = true
}
fn (mut view View) set_y(new_y int) {
view.y = new_y
view.ved.update_cur_fn_name()
}
fn (mut view View) j() {
if view.lines.len == 0 {
return
}
line0 := view.line()
view.set_y(view.y + 1)
// Reached end
if view.y >= view.lines.len {
view.set_y(view.lines.len - 1)
return
}
// Scroll
if view.y >= view.from + view.page_height {
view.from++
}
// Correct x if there are tabs on the next line
_, nr_tabs1 := nr_spaces_and_tabs_in_line(line0)
line := view.line()
_, nr_tabs2 := nr_spaces_and_tabs_in_line(line)
// println('tabs1,2=${nr_tabs1},${nr_tabs2}')
// println(view.ved.cfg.tab_size)
if nr_tabs2 > nr_tabs1 {
view.x -= (nr_tabs2 - nr_tabs1) * view.ved.cfg.tab_size - 1
if view.x < 0 {
view.x = 0
}
}
// Line below is shorter, move to the end of it
if view.x > line.len - 1 {
view.prev_x = view.x
view.x = line.len - 1
if view.x < 0 {
view.x = 0
}
}
}
fn (mut view View) k() {
if view.y >= view.lines.len {
view.y = view.lines.len - 1
}
if view.y <= 0 {
return
}
line0 := view.line()
view.set_y(view.y - 1)
// Scroll
if view.y < view.from && view.y >= 0 {
view.from--
}
// Correct x if there are tabs on the prev line
_, nr_tabs1 := nr_spaces_and_tabs_in_line(line0)
line := view.line()
_, nr_tabs2 := nr_spaces_and_tabs_in_line(line)
// println('tabs1,2=${nr_tabs1},${nr_tabs2}')
// println(view.ved.cfg.tab_size)
if nr_tabs2 < nr_tabs1 {
view.x -= (nr_tabs1 - nr_tabs2) * view.ved.cfg.tab_size - 1
if view.x < 0 {
view.x = 0
}
}
// Line above is shorter, move to the end of it
if view.x > line.len - 1 {
view.prev_x = view.x
view.x = line.len - 1
if view.x < 0 {
view.x = 0
}
}
}
fn (mut view View) shift_h() {
view.set_y(view.from)
}
fn (mut view View) move_to_page_bot() {
view.set_y(view.from + view.page_height - 1)
}
fn (mut view View) l() {
line := view.line()
if view.x < line.len {
view.x++
}
}
fn (mut view View) h() {
if view.x > 0 {
view.x--
}
line := view.line()
// Cursor is outside the line, move it to the end of it
if view.x > line.len {
view.x = line.len
}
}
fn (mut view View) shift_g() {
view.set_y(view.lines.len - 1)
view.from = view.y - view.page_height + 1
if view.from < 0 {
view.from = 0
}
}
fn (mut view View) shift_a() {
line := view.line()
view.set_line('${line} ')
view.x = view.uline().len - 1
}
fn (mut view View) shift_i() {
view.x = 0
for view.char() == view.ved.cfg.tab {
view.x++
}
}
fn (mut view View) gg() {
view.from = 0
view.set_y(0)
}
fn (mut view View) shift_f() {
view.from += view.page_height
if view.from >= view.lines.len {
view.from = view.lines.len - 1
}
view.set_y(view.from)
}
fn (mut view View) shift_b() {
view.from -= view.page_height
if view.from < 0 {
view.from = 0
}
view.set_y(view.from)
}
fn (mut view View) dd() {
if view.lines.len == 0 {
return
}
mut ved := view.ved
ved.prev_key = gg.KeyCode.invalid
ved.prev_cmd = 'dd'
ved.ylines = []
ved.ylines << view.line()
view.lines.delete(view.y)
if view.y == view.lines.len {
view.k()
}
view.changed = true
}
fn (mut view View) shift_right() {
// No selection, shift current line
if view.vstart == -1 {
view.set_line('\t${view.line()}')
return
}
for i := view.vstart; i <= view.vend; i++ {
line := view.lines[i]
view.lines[i] = '\t${line}'
}
}
fn (mut view View) shift_left() {
if view.vstart == -1 {
line := view.line()
if !line.starts_with('\t') {
return
}
view.set_line(line[1..])
return
}
for i := view.vstart; i <= view.vend; i++ {
line := view.lines[i]
if !line.starts_with('\t') {
continue
}
view.lines[i] = line[1..]
}
}
fn (mut v View) delete_char() {
u := v.uline()
if u.len < 1 || v.x >= u.len {
return
}
mut new_line := unsafe { u[..v.x] }
right := u[v.x + 1..]
new_line << right
v.set_line(new_line.string())
if v.x >= new_line.len {
v.x = new_line.len - 1
}
}
fn (mut view View) shift_c() string {
line := view.line()
s := line[..view.x]
deleted := line[view.x..]
view.set_line('${s} ')
view.x = s.len
return deleted
}
fn (mut view View) insert_text(s string) {
line := view.line()
if line.len == 0 {
view.set_line('${s} ')
} else {
if view.x > line.len {
view.x = line.len
}
uline := line.runes()
if view.x > uline.len {
return
}
left := uline[..view.x].string()
right := uline[view.x..uline.len].string()
// Insert char in the middle
res := '${left}${s}${right}'
view.set_line(res)
}
view.x += s.runes().len
view.changed = true
// Show autocomplete window on `.`
if s == '.' {
println('DOOOT, SHOW WINDOW')
view.ved.mode = .autocomplete
view.ved.refresh = true
view.ved.gg.refresh_ui()
go view.ved.get_line_info()
}
}
fn (mut view View) backspace() {
if view.x == 0 {
if view.ved.cfg.backspace_go_up && view.y > 0 {
view.x = 0
view.y--
view.x = view.lines[view.y].len
view.lines.delete(view.y + 1)
view.changed = true
}
return
}
// line := view.line()
uline := view.uline()
// println('line="$line" uline="$uline.string()"')
left := uline[..view.x - 1].string()
mut right := ''
if view.x < uline.len {
right = uline[view.x..].string()
}
view.set_line('${left}${right}')
view.x--
if view.ved.prev_insert.len > 0 {
view.ved.prev_insert = view.ved.prev_insert[..view.ved.prev_insert.len - 1] // TODO runes
}
}
fn (mut view View) yy() {
view.ved.ylines = [view.line()]
}
fn (mut view View) p() {
for line in view.ved.ylines {
view.o()
view.set_line(line)
}
}
fn (mut view View) shift_o() {
view.o_generic(0)
}
fn (mut view View) o() {
view.o_generic(1)
}
fn (mut view View) o_generic(delta int) {
view.y += delta
// Insert the same amount of spaces/tabs as in prev line
prev_line := if view.lines.len == 0 || view.y == 0 { '' } else { view.lines[view.y - 1] }
nr_spaces, nr_tabs := nr_spaces_and_tabs_in_line(prev_line)
mut new_line := strings.repeat(`\t`, nr_tabs) + strings.repeat(` `, nr_spaces)
if prev_line.ends_with('{') || prev_line.ends_with('{ ') {
new_line += '\t '
} else if !new_line.ends_with(' ') {
new_line += ' '
}
view.x = new_line.len - 1
if view.y >= view.lines.len {
view.lines << new_line
} else {
view.lines.insert(view.y, new_line)
}
view.changed = true
}
fn (mut view View) enter() {
// Create new line
// And move everything to the right of the cursor to it
pos := view.x
line := view.line()
if pos >= line.len - 1 && line != '' && line != ' ' {
// {} insertion
if line.ends_with('{ ') {
view.o()
view.x--
view.insert_text('}')
view.y--
view.o()
// view.insert_text('\t')
// view.x = 0
} else {
view.o()
}
return
}
// if line == '' {
// view.o()
// return
// }
uline := line.runes()
mut right := ''
if pos < uline.len {
right = uline[pos..].string()
}
left := uline[..pos].string()
view.set_line(left)
view.o()
view.set_line(right)
view.x = 0
}
fn (mut view View) join() {
if view.y == view.lines.len - 1 {
return
}
// Add next line to current line
line := view.line()
second_line := view.lines[view.y + 1]
joined := line + second_line
view.set_line(joined)
view.y++
view.dd()
view.y--
// view.prev_cmd = "J"
}
fn (mut v View) y_visual() {
mut ylines := []string{}
vtop, vbot := if v.vstart < v.vend { v.vstart, v.vend } else { v.vend, v.vstart }
for i := vtop; i <= vbot; i++ {
ylines << v.lines[i]
}
mut ved := v.ved
ved.ylines = ylines
// Copy YY to clipboard if +(=) was pressed before
if ved.prev_key == .equal {
ved.cb.copy(ylines.join('\n'))
}
v.vstart = -1
v.vend = -1
}
fn (mut view View) d_visual() {
vtop := if view.vstart < view.vend { view.vstart } else { view.vend }
view.y_visual()
for i := 0; i < view.ved.ylines.len; i++ {
view.lines.delete(vtop)
}
// Move cursor to a valid position using k
if view.y >= view.lines.len {
view.y = view.lines.len
} else {
view.y += 1
}
view.k()
}
fn (mut view View) cw() {
mut ved := view.ved
ved.prev_insert = ''
view.dw(false)
ved.prev_cmd = 'cw'
// view.ved.set_insert() // don't call this since it resets prev_insert
ved.mode = .insert
ved.just_switched = true
}
// returns the removed word
fn (mut view View) dw(del_whitespace bool) { // string {
mut ved := view.ved
typ := is_alpha(u8(view.char()))
// While cur char has the same type - delete it
for {
line := view.line()
if view.x < 0 || view.x >= line.len - 1 {
break
}
if typ == is_alpha(u8(view.char())) {
// println('del x=$view.x len=$line.len')
view.delete_char()
} else {
break
}
}
// Delete whitespace after the deleted word
if del_whitespace {
for is_whitespace(u8(view.char())) {
line := view.line()
if view.x < 0 || view.x >= line.len {
break
}
view.delete_char()
}
}
ved.prev_cmd = 'dw'
}
// returns the removed word
fn (mut view View) db(del_whitespace bool) { // string {
mut ved := view.ved
typ := is_alpha(u8(view.char()))
// While cur char has the same type - delete it
for {
line := view.line()
if view.x < 0 || view.x >= line.len - 1 {
break
}
view.x--
if typ == is_alpha_underscore(u8(view.char())) {
// println('del x=$view.x len=$line.len')
view.delete_char()
} else {
break
}
}
view.x++
// Delete whitespace after the deleted word
/*
if del_whitespace {
for is_whitespace(u8(view.char())) {
line := view.line()
if view.x < 0 || view.x >= line.len {
break
}
view.delete_char()
}
}
*/
ved.prev_cmd = 'db'
}
// TODO COPY PASTA
// same as cw but deletes underscores
fn (mut view View) ce() {
mut ved := view.ved
view.de()
ved.prev_cmd = 'ce'
view.ved.set_insert()
}
fn (mut view View) w() {
line := view.line()
typ := is_alpha_underscore(view.char())
// Go to end of current word
for view.x < line.len - 1 && typ == is_alpha_underscore(view.char()) {
view.x++
}
// Go to start of next word
for view.x < line.len - 1 && view.char() == 32 {
view.x++
}
}
fn (mut view View) b() {
// line := view.line()
// Go to start of prev word
for view.x > 0 && view.char() == 32 {
view.x--
}
typ := is_alpha_underscore(view.char())
// Go to start of current word
for view.x > 0 && typ == is_alpha_underscore(view.char()) {
view.x--
}
}
fn (mut view View) de() {
mut ved := view.ved
typ := is_alpha_underscore(view.char())
// While cur char has the same type - delete it
for {
line := view.line()
if view.x >= 0 && view.x < line.len && typ == is_alpha_underscore(view.char()) {
view.delete_char()
} else {
break
}
}
ved.prev_cmd = 'de'
}
// delete all characters before and after the cursor inside '', "", () etc
fn (mut view View) ci(key gg.KeyCode) {
mut ved := view.ved
line := view.line()
defer {
ved.prev_cmd = ''
}
match key {
.apostrophe {
if !line.contains("'") {
return
}
mut start := view.x
for line[start] != `'` {
start--
}
mut end := view.x
for line[end] != `'` {
end++
}
view.set_line(line[..start + 1] + line[end..])
view.x = start + 1
view.ved.set_insert()
}
._9 {}
else {}
}
// match
// view.dw()
}
fn (mut view View) zz() {
view.from = view.y - view.ved.page_height / 2
if view.from < 0 {
view.from = 0
}
}
fn (mut view View) r(s string) {
view.delete_char()
view.insert_text(s)
view.x--
}
fn (mut view View) tt() {
if view.prev_path == '' {
return
}
mut ved := view.ved
ved.prev_key = .invalid
view.open_file(view.prev_path, 0)
}
fn (mut view View) move_to_line(line int) {
view.prev_y = view.y
view.from = line
view.set_y(line)
view.zz()
}
// Fit lines into 80 chars
fn (mut view View) gq() {
/*
mut ved := view.ved
if ved.mode != .visual {
return
}
view.y_visual()
max := ved.max_chars(0)
// Join all selected lines into a single string
joined := ved.ylines.join('\n')
// Delete what we selected
for yline in ved.ylines {
if yline == '' {
continue
}
view.lines.delete(view.y)
}
new_lines := break_text(joined, max - 1)
for line in new_lines {
view.insert_text(line)
view.o()
}
ved.mode = .normal
*/
}
// ctrl+a - increase number by one
fn (mut view View) super_a(diff int) {
line := view.line()
mut num_start_pos := -1
for i, r in line {
if r >= `0` && r <= `9` {
num_start_pos = i
break
}
}
if num_start_pos == -1 {
return
}
s := line[num_start_pos..]
vals := s.fields()
number := vals[0].int()
new_line := line.replace_once(number.str(), (number + diff).str())
view.set_line(new_line)
}
fn is_alpha(r u8) bool {
return (r >= `a` && r <= `z`) || (r >= `A` && r <= `Z`) || (r >= `0` && r <= `9`)
}
fn is_whitespace(r u8) bool {
return r == ` ` || r == `\t`
}
fn is_alpha_underscore(r int) bool {
return is_alpha(u8(r)) || u8(r) == `_` || u8(r) == `#` || u8(r) == `$`
}
fn break_text(s string, max int) []string {
mut lines := []string{}
mut start := 0
for i := 0; i < s.len; i++ {
if i == s.len - 1 {
// Include the very last char
lines << s[start..i + 1]
break
}
if i - start >= max {
lines << s[start..i]
start = i
}
}
return lines
}