-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
882 lines (828 loc) · 20.7 KB
/
main.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
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
package csvi
import (
"bufio"
"container/list"
"fmt"
"io"
"os"
"strings"
"time"
"github.com/mattn/go-runewidth"
"github.com/nyaosorg/go-readline-ny"
"github.com/nyaosorg/go-readline-ny/keys"
"github.com/hymkor/csvi/internal/nonblock"
"github.com/hymkor/csvi/uncsv"
)
const (
_ANSI_CURSOR_OFF = "\x1B[?25l"
_ANSI_CURSOR_ON = "\x1B[?25h"
_ANSI_YELLOW = "\x1B[0;33;1m"
_ANSI_RESET = "\x1B[0m"
_ANSI_ERASE_LINE = "\x1B[0m\x1B[0K"
_ANSI_ERASE_SCRN_AFTER = "\x1B[0m\x1B[0J"
_ANSI_UNDERLINE_ON = "\x1B[4m"
_ANSI_UNDERLINE_OFF = "\x1B[24m"
)
type _ColorStyle struct {
Cursor [2]string
Even [2]string
Odd [2]string
}
var bodyColorStyle = _ColorStyle{
Cursor: [...]string{"\x1B[107;30;22m", "\x1B[40;37m"},
Even: [...]string{"\x1B[48;5;235;37;1m", "\x1B[22;40m"},
Odd: [...]string{"\x1B[40;37;1m", "\x1B[22m"},
}
var headColorStyle = _ColorStyle{
Cursor: [...]string{"\x1B[107;30;22m", "\x1B[40;36m"},
Even: [...]string{"\x1B[48;5;235;36;1m", "\x1B[22;40m"},
Odd: [...]string{"\x1B[40;36;1m", "\x1B[22m"},
}
var replaceTable = strings.NewReplacer(
"\r", "\u240D",
"\x1B", "\u241B",
"\n", "\u240A",
"\t", "\u2409")
// See. en.wikipedia.org/wiki/Unicode_control_characters#Control_pictures
func sum(f func(n int) int, from, to int) int {
result := 0
for i := from; i < to; i++ {
result += f(i)
}
return result
}
func drawLine(
csvs []uncsv.Cell,
cellWidth func(int) int,
screenWidth int,
cursorPos int,
reverse bool,
style *_ColorStyle,
out io.Writer) {
if len(csvs) <= 0 && cursorPos >= 0 {
io.WriteString(out, style.Cursor[0])
io.WriteString(out, "\x1B[K")
io.WriteString(out, style.Cursor[1])
return
}
i := 0
if reverse {
io.WriteString(out, style.Odd[0])
defer io.WriteString(out, style.Odd[1])
} else {
io.WriteString(out, style.Even[0])
defer io.WriteString(out, style.Even[1])
}
io.WriteString(out, "\x1B[K")
for len(csvs) > 0 {
cursor := csvs[0]
text := cursor.Text()
csvs = csvs[1:]
nextI := i + 1
cw := cellWidth(i)
for len(csvs) > 0 && csvs[0].Text() == "" && nextI != cursorPos {
cw += cellWidth(nextI)
csvs = csvs[1:]
nextI++
}
if cw > screenWidth || len(csvs) <= 0 {
cw = screenWidth
}
text = replaceTable.Replace(text)
ss, _ := cutStrInWidth(text, cw)
if i == cursorPos {
io.WriteString(out, style.Cursor[0])
}
if cursor.Modified() {
io.WriteString(out, _ANSI_UNDERLINE_ON)
}
io.WriteString(out, ss)
if cursor.Modified() {
io.WriteString(out, _ANSI_UNDERLINE_OFF)
}
if i == cursorPos {
io.WriteString(out, "\x1B[K")
if reverse {
io.WriteString(out, style.Odd[0])
} else {
io.WriteString(out, style.Even[0])
}
}
screenWidth -= cw
if screenWidth <= 0 {
break
}
fmt.Fprintf(out, "\x1B[%dG", sum(cellWidth, 0, nextI)+1)
if i == cursorPos {
io.WriteString(out, "\x1B[K")
}
i = nextI
}
}
func up(n int, out io.Writer) {
if n == 0 {
out.Write([]byte{'\r'})
} else if n == 1 {
io.WriteString(out, "\r\x1B[A")
} else {
fmt.Fprintf(out, "\r\x1B[%dA", n)
}
}
func drawPage(page func(func([]uncsv.Cell) bool), cellWidth func(int) int, csrpos, csrlin, w, h int, style *_ColorStyle, cache map[int]string, out io.Writer) int {
reverse := false
count := 0
lfCount := 0
page(func(record []uncsv.Cell) bool {
if count >= h {
return false
}
if count > 0 {
lfCount++
io.WriteString(out, "\r\n") // "\r" is for Linux and go-tty
}
cursorPos := -1
if count == csrlin {
cursorPos = csrpos
}
var buffer strings.Builder
drawLine(record, cellWidth, w, cursorPos, reverse, style, &buffer)
line := buffer.String()
if f := cache[count]; f != line {
io.WriteString(out, line)
cache[count] = line
}
reverse = !reverse
count++
return true
})
io.WriteString(out, "\r\n") // \r is for Linux & go-tty
lfCount++
return lfCount
}
func cellsAfter(cells []uncsv.Cell, n int) []uncsv.Cell {
if n <= len(cells) {
return cells[n:]
} else {
return []uncsv.Cell{}
}
}
type _View struct {
headCache map[int]string
bodyCache map[int]string
}
func newView() *_View {
return &_View{
headCache: map[int]string{},
bodyCache: map[int]string{},
}
}
func (v *_View) clearCache() {
clear(v.headCache)
clear(v.bodyCache)
}
func (v *_View) Draw(header, startRow, cursorRow *RowPtr, _cellWidth func(int) int, headerLines, startCol, cursorCol, screenHeight, screenWidth int, out io.Writer) int {
// print header
lfCount := 0
cellWidth := func(n int) int {
return _cellWidth(n + startCol)
}
if h := headerLines; h > 0 {
enum := func(callback func([]uncsv.Cell) bool) {
for i := 0; i < h && header != nil; i++ {
if !callback(cellsAfter(header.Cell, startCol)) {
return
}
header = header.Next()
}
}
lfCount = drawPage(enum, cellWidth, cursorCol-startCol, cursorRow.lnum, screenWidth-1, h, &headColorStyle, v.headCache, out)
}
if startRow.lnum < headerLines {
for i := 0; i < headerLines && startRow != nil; i++ {
startRow = startRow.Next()
}
}
if startRow == nil {
return lfCount
}
p := startRow.Clone()
// print body
enum := func(callback func([]uncsv.Cell) bool) {
for p != nil {
if !callback(cellsAfter(p.Cell, startCol)) {
return
}
p = p.Next()
}
}
style := &bodyColorStyle
if headerLines%2 == 1 {
style = &_ColorStyle{
Cursor: bodyColorStyle.Cursor,
Even: bodyColorStyle.Odd,
Odd: bodyColorStyle.Even,
}
}
return lfCount + drawPage(enum, cellWidth, cursorCol-startCol, cursorRow.lnum-startRow.lnum, screenWidth-1, screenHeight-1, style, v.bodyCache, out)
}
func (app *_Application) YesNo(message string) bool {
fmt.Fprintf(app, "%s\r%s%s", _ANSI_YELLOW, message, _ANSI_ERASE_LINE)
io.WriteString(app, _ANSI_CURSOR_ON)
ch, err := app.GetKey()
io.WriteString(app, _ANSI_CURSOR_OFF)
return err == nil && ch == "y"
}
func first[T any](value T, _ error) T {
return value
}
func printStatusLine(out io.Writer, mode *uncsv.Mode, cursorRow *RowPtr, cursorCol int, screenWidth int) {
n := 0
if mode.Comma == '\t' {
n += first(io.WriteString(out, "[TSV]"))
} else if mode.Comma == ',' {
n += first(io.WriteString(out, "[CSV]"))
}
switch cursorRow.Term {
case "\r\n":
n += first(io.WriteString(out, "[CRLF]"))
case "\n":
n += first(io.WriteString(out, "[LF]"))
case "":
n += first(io.WriteString(out, "[EOF]"))
}
if mode.HasBom() {
n += first(io.WriteString(out, "[BOM]"))
}
if mode.NonUTF8 {
if mode.IsUTF16LE() {
n += first(io.WriteString(out, "[16LE]"))
} else if mode.IsUTF16BE() {
n += first(io.WriteString(out, "[16BE]"))
} else {
n += first(io.WriteString(out, "[ANSI]"))
}
}
if 0 <= cursorCol && cursorCol < len(cursorRow.Cell) {
n += first(fmt.Fprintf(out, "(%d,%d/%d): ",
cursorCol+1,
cursorRow.lnum+1,
cursorRow.list.Len()))
var buffer strings.Builder
buffer.WriteString(cursorRow.Cell[cursorCol].SourceText(mode))
if cursorCol < len(cursorRow.Cell)-1 {
buffer.WriteByte(mode.Comma)
} else if term := cursorRow.Term; term != "" {
buffer.WriteString(term)
} else { // EOF
buffer.WriteString("\u2592")
}
io.WriteString(out, runewidth.Truncate(replaceTable.Replace(buffer.String()), screenWidth-n, "..."))
}
}
type Pilot interface {
Size() (int, int, error)
Calibrate() error
GetKey() (string, error)
ReadLine(io.Writer, string, string, Candidate) (string, error)
GetFilename(io.Writer, string, string) (string, error)
Close() error
}
type CommandResult struct {
Message string
Quit bool
}
type CellValidatedEvent struct {
Text string
Row int
Col int
}
type KeyEventArgs struct {
*_Application
CursorRow *RowPtr
CursorCol int
}
type Config struct {
*uncsv.Mode
CellWidth func(int) int
HeaderLines int
Pilot Pilot
FixColumn bool
ReadOnly bool
ProtectHeader bool
Message string
KeyMap map[string]func(*KeyEventArgs) (*CommandResult, error)
OnCellValidated func(*CellValidatedEvent) (string, error)
Titles []string
}
func (cfg Config) validate(row *RowPtr, col int, text string) (string, error) {
if cfg.OnCellValidated == nil {
return text, nil
}
return cfg.OnCellValidated(&CellValidatedEvent{
Row: row.lnum,
Col: col,
Text: text,
})
}
// Deprecated: use Config.Edit
func (cfg Config) Main(mode *uncsv.Mode, in io.Reader, out io.Writer) (*Result, error) {
cfg.Mode = mode
return cfg.Edit(in, out)
}
func (cfg Config) Edit(in io.Reader, out io.Writer) (*Result, error) {
if in == nil {
return cfg.edit(nil, out)
}
reader, ok := in.(*bufio.Reader)
if !ok {
reader = bufio.NewReader(in)
}
return cfg.edit(func() (*uncsv.Row, error) {
return uncsv.ReadLine(reader, cfg.Mode)
}, out)
}
func isEmptyRow(row *uncsv.Row) bool {
switch len(row.Cell) {
case 0:
return true
case 1:
if len(row.Cell[0].Original()) <= 0 {
return true
}
}
return false
}
const (
msgReadOnly = "Read Only Mode !"
msgProtectHeader = "Header is protected"
msgColumnFixed = "The order of Columns is fixed !"
)
func (cfg *Config) checkWriteProtect(cursorRow *RowPtr) string {
if cfg.ProtectHeader && cursorRow.lnum < cfg.HeaderLines {
return msgProtectHeader
}
if cfg.ReadOnly {
return msgReadOnly
}
return ""
}
func (cfg *Config) checkWriteProtectAndColumn(cursorRow *RowPtr) string {
if m := cfg.checkWriteProtect(cursorRow); m != "" {
return m
}
if cfg.FixColumn {
return msgColumnFixed
}
return ""
}
func (app *_Application) readlineAndValidate(prompt, text string, row *RowPtr, col int) (string, error) {
candidates := makeCandidate(row.lnum-1, col, row)
for {
var err error
text, err = app.Config.Pilot.ReadLine(app.out, prompt, text, candidates)
if err != nil {
return "", err
}
tx, err := app.Config.validate(row, col, text)
if err == nil {
return tx, nil
}
prompt = fmt.Sprintf("%s: Re-enter>", err.Error())
}
}
func (cfg *Config) edit(fetch func() (*uncsv.Row, error), out io.Writer) (*Result, error) {
if cfg.KeyMap == nil {
cfg.KeyMap = make(map[string]func(*KeyEventArgs) (*CommandResult, error))
}
mode := cfg.Mode
if mode == nil {
mode = &uncsv.Mode{}
}
cellWidth := cfg.CellWidth
if cellWidth == nil {
cellWidth = func(int) int {
return 14
}
}
pilot := cfg.Pilot
if pilot == nil {
var err error
pilot, err = newManualCtl()
if err != nil {
return nil, err
}
defer pilot.Close()
cfg.Pilot = pilot
}
if _, ok := out.(*os.File); ok {
if err := pilot.Calibrate(); err != nil {
return nil, err
}
}
app := &_Application{
Config: cfg,
csvLines: list.New(),
out: out,
Pilot: pilot,
}
if fetch != nil {
for i := 0; i < 100; i++ {
row, err := fetch()
if err != nil {
if err != io.EOF {
return nil, err
}
fetch = nil
if isEmptyRow(row) {
break
}
}
app.Push(row)
if err == io.EOF {
break
}
}
}
startRow := app.Front()
startCol := 0
if startRow == nil {
newRow := uncsv.NewRow(mode)
app.Push(&newRow)
startRow = app.Front()
}
cursorCol := 0
cursorRow := app.Front()
lastSearch := searchForward
lastSearchRev := searchBackward
lastWord := ""
var lastWidth, lastHeight int
keyWorker := nonblock.New(func() (string, error) {
return pilot.GetKey()
})
defer keyWorker.Close()
view := newView()
screenWidth, _screenHeight, err := pilot.Size()
if err != nil {
return nil, err
}
for _, title := range cfg.Titles {
s, _ := cutStrInWidth(title, screenWidth-1)
fmt.Fprintln(out, s)
}
message := cfg.Message
var killbuffer string
for {
screenHeight := _screenHeight - len(cfg.Titles)
screenHeight -= cfg.HeaderLines
if lastWidth != screenWidth || lastHeight != screenHeight {
view.clearCache()
lastWidth = screenWidth
lastHeight = screenHeight
io.WriteString(out, _ANSI_CURSOR_OFF)
}
lfCount := view.Draw(app.Front(), startRow, cursorRow, cellWidth, cfg.HeaderLines, startCol, cursorCol, screenHeight, screenWidth, out)
repaint := func() {
up(lfCount, out)
lfCount = view.Draw(app.Front(), startRow, cursorRow, cellWidth, cfg.HeaderLines, startCol, cursorCol, screenHeight, screenWidth, out)
}
io.WriteString(out, _ANSI_YELLOW)
if message != "" {
io.WriteString(out, runewidth.Truncate(message, screenWidth-1, ""))
} else if 0 <= cursorRow.lnum && cursorRow.lnum < app.Len() {
printStatusLine(out, mode, cursorRow, cursorCol, screenWidth)
}
io.WriteString(out, _ANSI_RESET)
io.WriteString(out, _ANSI_ERASE_SCRN_AFTER)
const interval = 4
displayUpdateTime := time.Now().Add(time.Second / interval)
ch, err := keyWorker.GetOr(func() bool {
if fetch == nil {
return false
}
row, err := fetch()
if err != nil {
fetch = nil
if err != io.EOF || isEmptyRow(row) {
return false
}
}
app.Push(row)
if message == "" && (err == io.EOF || time.Now().After(displayUpdateTime)) {
io.WriteString(out, "\r"+_ANSI_YELLOW)
printStatusLine(out, mode, cursorRow, cursorCol, screenWidth)
io.WriteString(out, _ANSI_RESET)
io.WriteString(out, _ANSI_ERASE_SCRN_AFTER)
displayUpdateTime = time.Now().Add(time.Second / interval)
}
return err != io.EOF
})
if err != nil {
return nil, err
}
message = ""
if handler, ok := cfg.KeyMap[ch]; ok {
e := &KeyEventArgs{
CursorRow: cursorRow,
CursorCol: cursorCol,
_Application: app,
}
cmdResult, err := handler(e)
if err != nil || cmdResult.Quit {
return &Result{_Application: app}, err
}
message = cmdResult.Message
} else {
switch ch {
case keys.CtrlL:
view.clearCache()
case "q", keys.Escape:
if cfg.ReadOnly || app.YesNo("Quit Sure ? [y/n]") {
io.WriteString(out, "\n")
return &Result{_Application: app}, nil
}
case "j", keys.Down, keys.CtrlN, keys.Enter:
if next := cursorRow.Next(); next != nil {
cursorRow = next
}
case "k", keys.Up, keys.CtrlP:
if prev := cursorRow.Prev(); prev != nil {
cursorRow = prev
}
case "h", keys.Left, keys.CtrlB, keys.ShiftTab:
if cursorCol > 0 {
cursorCol--
}
case "l", keys.Right, keys.CtrlF, keys.CtrlI:
cursorCol++
case "0", "^", keys.CtrlA:
cursorCol = 0
case "$", keys.CtrlE:
cursorCol = len(cursorRow.Cell) - 1
case "<":
cursorRow = app.Front()
startRow = app.Front()
cursorCol = 0
startCol = 0
case ">", "G":
cursorRow = app.Back()
case "n":
if lastWord == "" {
break
}
r, c := lastSearch(cursorRow, cursorCol, lastWord)
if r == nil {
message = fmt.Sprintf("%s: not found", lastWord)
break
}
cursorRow = r
cursorCol = c
case "N":
if lastWord == "" {
break
}
r, c := lastSearchRev(cursorRow, cursorCol, lastWord)
if r == nil {
message = fmt.Sprintf("%s: not found", lastWord)
break
}
cursorRow = r
cursorCol = c
case "/", "?":
var err error
view.clearCache()
lastWord, err = pilot.ReadLine(out, ch, "", nil)
if err != nil {
if err != readline.CtrlC {
message = err.Error()
}
break
}
if ch == "/" {
lastSearch = searchForward
lastSearchRev = searchBackward
} else {
lastSearch = searchBackward
lastSearchRev = searchForward
}
r, c := lastSearch(cursorRow, cursorCol, lastWord)
if r == nil {
message = fmt.Sprintf("%s: not found", lastWord)
break
}
cursorRow = r
cursorCol = c
case "o":
if cfg.ProtectHeader && cursorRow.lnum+1 < cfg.HeaderLines {
message = msgProtectHeader
break
}
if cfg.ReadOnly {
message = msgReadOnly
break
}
newRow := uncsv.NewRow(mode)
newRow.Term = cursorRow.Term
if cursorRow.Term == "" {
cursorRow.Term = mode.DefaultTerm
}
if cfg.FixColumn {
for len(newRow.Cell) < len(cursorRow.Cell) {
newRow.Insert(0, "", mode)
}
}
cursorRow = cursorRow.InsertAfter(&newRow)
repaint()
view.clearCache()
newCol := cursorCol
if cursorCol >= len(cursorRow.Cell) {
newCol = len(cursorRow.Cell) - 1
}
if text, err := app.readlineAndValidate("new line>", "", cursorRow, newCol); err == nil {
cursorRow.Replace(newCol, text, mode)
}
case "O":
if m := cfg.checkWriteProtect(cursorRow); m != "" {
message = m
break
}
startPrevP := startRow.Prev()
newRow := uncsv.NewRow(mode)
if cfg.FixColumn {
for len(newRow.Cell) < len(cursorRow.Cell) {
newRow.Insert(0, "", mode)
}
}
cursorRow = cursorRow.InsertBefore(&newRow)
if startPrevP != nil {
startRow = startPrevP.Next()
} else {
startRow = app.Front()
}
repaint()
view.clearCache()
newCol := cursorCol
if cursorCol >= len(cursorRow.Cell) {
newCol = len(cursorRow.Cell) - 1
}
if text, err := app.readlineAndValidate("new line>", "", cursorRow, newCol); err == nil {
cursorRow.Replace(newCol, text, mode)
}
case "D":
if m := cfg.checkWriteProtect(cursorRow); m != "" {
message = m
break
}
if app.Len() <= 1 {
break
}
startPrevP := startRow.Prev()
prevP := cursorRow.Prev()
removedRow := cursorRow.Remove()
app.removedRows = append(app.removedRows, removedRow)
if prevP == nil {
cursorRow = app.Front()
} else if next := prevP.Next(); next != nil {
cursorRow = next
} else {
cursorRow = prevP
cursorRow.Term = removedRow.Term
}
if startPrevP == nil {
startRow = app.Front()
} else {
startRow = startPrevP.Next()
}
case "i":
if m := cfg.checkWriteProtectAndColumn(cursorRow); m != "" {
message = m
break
}
view.clearCache()
if text, err := app.readlineAndValidate("insert cell>", "", cursorRow, cursorCol); err == nil {
if cells := cursorRow.Cell; len(cells) == 1 && cells[0].Text() == "" {
cursorRow.Replace(cursorCol, text, mode)
} else {
cursorRow.Insert(cursorCol, text, mode)
cursorCol++
}
}
case "a":
if m := cfg.checkWriteProtectAndColumn(cursorRow); m != "" {
message = m
break
}
if cells := cursorRow.Cell; len(cells) == 1 && cells[0].Text() == "" {
// current column is the last one and it is empty
view.clearCache()
if text, err := app.readlineAndValidate("append cell>", "", cursorRow, cursorCol); err == nil {
cursorRow.Replace(cursorCol, text, mode)
}
} else {
cursorCol++
cursorRow.Insert(cursorCol, "", mode)
repaint()
view.clearCache()
if text, err := app.readlineAndValidate("append cell>", "", cursorRow, cursorCol); err != nil {
// cancel
cursorRow.Delete(cursorCol)
cursorCol--
} else {
cursorRow.Replace(cursorCol, text, mode)
}
}
case "r", "R", keys.F2:
if m := cfg.checkWriteProtect(cursorRow); m != "" {
message = m
break
}
cursor := &cursorRow.Cell[cursorCol]
q := cursor.IsQuoted()
view.clearCache()
if text, err := app.readlineAndValidate("replace cell>", cursor.Text(), cursorRow, cursorCol); err == nil {
cursorRow.Replace(cursorCol, text, mode)
if q {
*cursor = cursor.Quote(mode)
}
}
case "u":
cursorRow.Cell[cursorCol].Restore(mode)
case "y":
killbuffer = cursorRow.Cell[cursorCol].Text()
message = "yanked the current cell: " + killbuffer
case "p":
if m := cfg.checkWriteProtect(cursorRow); m != "" {
message = m
break
}
cursorRow.Replace(cursorCol, killbuffer, mode)
message = "pasted: " + killbuffer
case "d", "x":
if m := cfg.checkWriteProtectAndColumn(cursorRow); m != "" {
message = m
break
}
if len(cursorRow.Cell) <= 1 {
cursorRow.Replace(0, "", mode)
} else {
cursorRow.Delete(cursorCol)
}
case "\"":
cursor := &cursorRow.Cell[cursorCol]
if cursor.IsQuoted() {
cursorRow.Replace(cursorCol, cursor.Text(), mode)
} else {
*cursor = cursor.Quote(mode)
}
case "w":
if fetch != nil {
io.WriteString(out, _ANSI_YELLOW+"\rw: Wait a moment for reading all data..."+_ANSI_ERASE_LINE)
for {
row, err := fetch()
if err != nil && err != io.EOF {
return nil, err
}
app.Push(row)
if err == io.EOF {
break
}
}
}
if err := cmdWrite(app); err != nil {
message = err.Error()
}
view.clearCache()
}
}
if L := len(cursorRow.Cell); L <= 0 {
cursorCol = 0
} else if cursorCol >= L {
cursorCol = L - 1
}
if cursorRow.lnum < startRow.lnum {
startRow = cursorRow.Clone()
} else if cursorRow.lnum >= startRow.lnum+screenHeight-1 {
goal := cursorRow.lnum - (screenHeight - 1) + 1
for startRow = cursorRow.Clone(); startRow.lnum > goal; {
startRow = startRow.Prev()
}
}
if cursorCol < startCol {
startCol = cursorCol
} else {
cellWidth := cfg.CellWidth
if cellWidth == nil {
cellWidth = func(int) int {
return 14
}
}
for {
w := sum(cellWidth, startCol, cursorCol+1)
if w <= screenWidth {
break
}
startCol++
}
}
up(lfCount, out)
}
}