-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathapp.go
More file actions
775 lines (672 loc) · 17.9 KB
/
app.go
File metadata and controls
775 lines (672 loc) · 17.9 KB
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
package main
import (
"bufio"
"cmp"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"slices"
"strings"
"syscall"
"time"
)
type app struct {
ui *ui // ui state (screen, windows, input)
nav *nav // navigation state (dirs, cursor, selections, preview, caches)
ticker *time.Ticker // refresh ticker if `period` > 0
quitChan chan struct{} // signals main loop to exit
cmd *exec.Cmd // currently running % (shell-pipe) command
cmdIn io.WriteCloser // stdin writer for running % command
cmdOutBuf []byte // output of running % command
cmdHistory []string // command history entries
cmdHistoryBeg int // index where commands from this session start in cmdHistory
cmdHistoryInd int // history navigation offset from most recent
cmdHistoryInput *string // initial input used as prefix filter while browsing history
menuCompActive bool // whether completion cycling is active
menuCompTmp []string // token snapshot taken when completion cycling starts, used for `cmd-menu-discard`
menuComps []compMatch // completion candidates for active prompt
menuCompInd int // index of selected completion candidate (-1: none selected)
selectionOut []string // paths to output on exit, used for `-print-selection` and `-selection-path`
watch *watch // fs watcher if `watch` is enabled
quitting bool // guard to prevent re-entering quit logic
}
func newApp(ui *ui, nav *nav) *app {
quitChan := make(chan struct{}, 1)
app := &app{
ui: ui,
nav: nav,
ticker: new(time.Ticker),
quitChan: quitChan,
watch: newWatch(nav.dirChan, nav.fileChan, nav.delChan),
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
for {
switch <-sigChan {
case os.Interrupt:
case syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM:
app.quit()
os.Exit(3)
return
}
}
}()
return app
}
func (app *app) quit() {
// Using synchronous shell commands for `on-quit` can cause this to be
// called again, so a guard variable is introduced here to prevent an
// infinite loop.
if app.quitting {
return
}
app.quitting = true
onQuit(app)
if gOpts.history {
if err := app.writeHistory(); err != nil {
log.Printf("writing history file: %s", err)
}
}
if !gSingleMode {
if _, err := remote(fmt.Sprintf("drop %d", gClientID)); err != nil {
log.Printf("dropping connection: %s", err)
}
if gOpts.autoquit {
if _, err := remote("quit"); err != nil {
log.Printf("auto quitting server: %s", err)
}
}
}
}
func (app *app) readFile(path string) {
log.Printf("reading file: %s", path)
f, err := os.Open(path)
if err != nil {
app.ui.echoerrf("opening file: %s", err)
return
}
defer f.Close()
p := newParser(f)
for p.parse() {
p.expr.eval(app, nil)
}
if p.err != nil {
app.ui.echoerrf("%s", p.err)
}
}
func loadFiles() (clipboard clipboard, err error) {
files, err := os.Open(gFilesPath)
if os.IsNotExist(err) {
err = nil
return
}
if err != nil {
err = fmt.Errorf("opening file selections file: %w", err)
return
}
defer files.Close()
s := bufio.NewScanner(files)
if !s.Scan() {
err = fmt.Errorf("scanning file list: %w", cmp.Or(s.Err(), io.EOF))
return
}
switch s.Text() {
case "copy":
clipboard.mode = clipboardCopy
case "move":
clipboard.mode = clipboardCut
default:
err = fmt.Errorf("unexpected option to copy file(s): %s", s.Text())
return
}
for s.Scan() && s.Text() != "" {
clipboard.paths = append(clipboard.paths, s.Text())
}
if s.Err() != nil {
err = fmt.Errorf("scanning file list: %w", s.Err())
return
}
log.Printf("loading clipboard: %v", clipboard.paths)
return
}
func saveFiles(clipboard clipboard) error {
if err := os.MkdirAll(filepath.Dir(gFilesPath), os.ModePerm); err != nil {
return fmt.Errorf("creating data directory: %w", err)
}
files, err := os.Create(gFilesPath)
if err != nil {
return fmt.Errorf("opening file selections file: %w", err)
}
defer files.Close()
log.Printf("saving files: %v", clipboard.paths)
var clipboardModeStr string
if clipboard.mode == clipboardCopy {
clipboardModeStr = "copy"
} else {
clipboardModeStr = "move"
}
if _, err := fmt.Fprintln(files, clipboardModeStr); err != nil {
return fmt.Errorf("write clipboard mode to file: %w", err)
}
for _, path := range clipboard.paths {
if _, err := fmt.Fprintln(files, path); err != nil {
return fmt.Errorf("write path to file: %w", err)
}
}
return files.Sync()
}
func (app *app) readHistory() error {
f, err := os.Open(gHistoryPath)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return fmt.Errorf("opening history file: %w", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
cmd := scanner.Text()
if len(cmd) < 1 || !slices.Contains([]string{":", "$", "!", "%", "&"}, cmd[:1]) {
continue
}
app.cmdHistory = append(app.cmdHistory, cmd)
}
app.cmdHistoryBeg = len(app.cmdHistory)
if err := scanner.Err(); err != nil {
return fmt.Errorf("reading history file: %w", err)
}
return nil
}
func (app *app) writeHistory() error {
if len(app.cmdHistory) == 0 {
return nil
}
local := slices.Clone(app.cmdHistory[app.cmdHistoryBeg:])
app.cmdHistory = nil
if err := app.readHistory(); err != nil {
return fmt.Errorf("reading history file: %w", err)
}
app.cmdHistory = append(app.cmdHistory, local...)
if len(app.cmdHistory) > 1000 {
app.cmdHistory = app.cmdHistory[len(app.cmdHistory)-1000:]
}
if err := os.MkdirAll(filepath.Dir(gHistoryPath), os.ModePerm); err != nil {
return fmt.Errorf("creating data directory: %w", err)
}
f, err := os.Create(gHistoryPath)
if err != nil {
return fmt.Errorf("creating history file: %w", err)
}
defer f.Close()
for _, cmd := range app.cmdHistory {
if _, err = fmt.Fprintln(f, cmd); err != nil {
return fmt.Errorf("writing history file: %w", err)
}
}
return nil
}
// loop is the main event loop of the application. Expressions are read from
// the client and the server on separate goroutines and sent here over channels
// for evaluation. Similarly directories and regular files are also read in
// separate goroutines and sent here for update.
func (app *app) loop() {
go app.nav.preloadLoop(app.ui)
go app.nav.previewLoop(app.ui)
var serverChan <-chan expr
if !gSingleMode {
serverChan = readExpr()
}
go app.ui.readEvents()
if gConfigPath != "" {
if _, err := os.Stat(gConfigPath); !os.IsNotExist(err) {
app.readFile(gConfigPath)
} else {
log.Printf("config file does not exist: %s", err)
}
} else {
for _, path := range gConfigPaths {
if _, err := os.Stat(path); !os.IsNotExist(err) {
app.readFile(path)
}
}
}
for _, cmd := range gCommands {
p := newParser(strings.NewReader(cmd))
for p.parse() {
p.expr.eval(app, nil)
}
if p.err != nil {
app.ui.echoerrf("%s", p.err)
}
}
app.nav.addJumpList()
if gSelect != "" {
go func() {
lstat, err := os.Lstat(gSelect)
if err != nil {
app.ui.exprChan <- &callExpr{"echoerr", []string{err.Error()}, 1}
} else if lstat.IsDir() {
app.ui.exprChan <- &callExpr{"cd", []string{gSelect}, 1}
} else {
app.ui.exprChan <- &callExpr{"select", []string{gSelect}, 1}
}
}()
}
for {
select {
case <-app.quitChan:
if app.nav.copyJobs > 0 {
app.ui.echoerr("quit: copy operation in progress")
continue
}
if app.nav.moveTotal > 0 {
app.ui.echoerr("quit: move operation in progress")
continue
}
if app.nav.deleteTotal > 0 {
app.ui.echoerr("quit: delete operation in progress")
continue
}
app.quit()
app.nav.previewChan <- ""
log.Printf("*************** closing client, PID: %d ***************", gClientID)
return
case n := <-app.nav.copyJobsChan:
app.nav.copyJobs += n
app.ui.draw(app.nav)
case n := <-app.nav.copyBytesChan:
app.nav.copyBytes += n
// n is usually 32*1024B (default io.Copy() buffer) so update roughly per 32KB x 128 = 4MB copied
if app.nav.copyUpdate++; app.nav.copyUpdate >= 128 {
app.nav.copyUpdate = 0
app.ui.draw(app.nav)
}
case n := <-app.nav.copyTotalChan:
app.nav.copyTotal += n
if n < 0 {
app.nav.copyBytes += n
}
if app.nav.copyTotal == 0 {
app.nav.copyUpdate = 0
}
app.ui.draw(app.nav)
case n := <-app.nav.moveCountChan:
app.nav.moveCount += n
if app.nav.moveUpdate++; app.nav.moveUpdate >= 1000 {
app.nav.moveUpdate = 0
app.ui.draw(app.nav)
}
case n := <-app.nav.moveTotalChan:
app.nav.moveTotal += n
if n < 0 {
app.nav.moveCount += n
}
if app.nav.moveTotal == 0 {
app.nav.moveUpdate = 0
}
app.ui.draw(app.nav)
case n := <-app.nav.deleteCountChan:
app.nav.deleteCount += n
if app.nav.deleteUpdate++; app.nav.deleteUpdate >= 1000 {
app.nav.deleteUpdate = 0
app.ui.draw(app.nav)
}
case n := <-app.nav.deleteTotalChan:
app.nav.deleteTotal += n
if n < 0 {
app.nav.deleteCount += n
}
if app.nav.deleteTotal == 0 {
app.nav.deleteUpdate = 0
}
app.ui.draw(app.nav)
case d := <-app.nav.dirChan:
var oldCurrPath string
if curr := app.nav.currFile(); curr != nil {
oldCurrPath = curr.path
}
prev, ok := app.nav.dirCache[d.path]
if ok {
d.ind = prev.ind
d.pos = prev.pos
d.visualAnchor = min(prev.visualAnchor, len(d.files)-1)
d.visualWrap = prev.visualWrap
d.filter = prev.filter
d.sort()
d.sel(prev.name(), app.nav.height)
}
app.nav.dirCache[d.path] = d
app.nav.position()
if curr := app.nav.currFile(); curr != nil {
if curr.path != oldCurrPath {
app.ui.loadFile(app, true)
}
}
app.watchDir(d)
// Avoid flickering UI and multiple, unnecessary `on-load` calls
// triggered by Git commands executed inside the users `on-load`
// command (often used to add git symbols using `addcustominfo`).
// TODO: Should `watch` also ignore `.git` directories?
if filepath.Base(d.path) != ".git" {
paths := make([]string, len(d.allFiles))
for i, file := range d.allFiles {
paths[i] = file.path
}
onLoad(app, paths)
}
if d.path == app.nav.currDir().path {
app.nav.preload()
}
app.ui.draw(app.nav)
case r := <-app.nav.regChan:
app.nav.regCache[r.path] = r
if curr := app.nav.currFile(); curr != nil {
if r.path == curr.path {
app.ui.sxScreen.forceClear = true
if gOpts.preload && r.volatile {
app.ui.loadFile(app, true)
}
}
}
app.ui.draw(app.nav)
case f := <-app.nav.fileChan:
for _, dir := range app.nav.dirCache {
if dir.path != filepath.Dir(f.path) {
continue
}
for i := range dir.allFiles {
if dir.allFiles[i].path == f.path {
dir.allFiles[i] = f
break
}
}
name := dir.name()
dir.sort()
dir.sel(name, app.nav.height)
}
delete(app.nav.regCache, f.path)
app.ui.loadFile(app, false)
onLoad(app, []string{f.path})
app.ui.draw(app.nav)
case path := <-app.nav.delChan:
deletePathRecursive(app.nav.selections, path)
if len(app.nav.selections) == 0 {
app.nav.selectionInd = 0
}
deletePathRecursive(app.nav.regCache, path)
deletePathRecursive(app.nav.dirCache, path)
for _, dirPath := range app.nav.dirPaths {
if dirPath == path {
if err := app.nav.cd(filepath.Dir(path)); err != nil {
log.Print(err)
}
break
}
}
case ev := <-app.ui.evChan:
e := app.ui.readEvent(ev, app.nav)
if e == nil {
continue
}
e.eval(app, nil)
loop:
for {
select {
case ev := <-app.ui.evChan:
e = app.ui.readEvent(ev, app.nav)
if e == nil {
continue
}
e.eval(app, nil)
default:
break loop
}
}
app.ui.draw(app.nav)
case e := <-app.ui.exprChan:
e.eval(app, nil)
app.ui.draw(app.nav)
case e := <-serverChan:
e.eval(app, nil)
app.ui.draw(app.nav)
case <-app.ticker.C:
app.nav.renew()
app.ui.loadFile(app, false)
case <-app.nav.previewTimer.C:
app.ui.draw(app.nav)
case <-app.nav.preloadTimer.C:
app.nav.preload()
}
}
}
func (app *app) runCmdSync(cmd *exec.Cmd, pauseAfter bool) {
app.nav.previewChan <- ""
if err := app.ui.suspend(); err != nil {
log.Printf("suspend: %s", err)
}
defer func() {
if err := app.ui.resume(); err != nil {
app.quit()
os.Exit(3)
}
}()
if err := cmd.Run(); err != nil {
app.ui.echoerrf("running shell: %s", err)
}
if pauseAfter {
anyKey()
}
app.ui.loadFile(app, true)
app.nav.renew()
}
// runShell is used to run a shell command. Modes are as follows:
//
// Prefix Wait Async Stdin Stdout Stderr UI action
// $ No No Yes Yes Yes Pause and then resume
// % No No Yes Yes Yes Statline for input/output
// ! Yes No Yes Yes Yes Pause and then resume
// & No Yes No No No Do nothing
func (app *app) runShell(s string, args []string, prefix string) {
app.nav.exportFiles()
app.ui.exportSizes()
app.exportMode()
exportLfPath()
exportOpts()
gState.mutex.Lock()
gState.data["maps"] = listBinds(map[string]map[string]expr{
"n": gOpts.nkeys,
"v": gOpts.vkeys,
})
gState.data["nmaps"] = listBinds(map[string]map[string]expr{
"n": gOpts.nkeys,
})
gState.data["vmaps"] = listBinds(map[string]map[string]expr{
"v": gOpts.vkeys,
})
gState.data["cmaps"] = listBinds(map[string]map[string]expr{
"c": gOpts.cmdkeys,
})
gState.data["cmds"] = listCmds(gOpts.cmds)
gState.data["jumps"] = listJumps(app.nav.jumpList, app.nav.jumpListInd)
gState.data["history"] = listHistory(app.cmdHistory)
gState.data["files"] = listFilesInCurrDir(app.nav)
gState.mutex.Unlock()
cmd := shellCommand(s, args)
switch prefix {
case "$", "!":
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
app.runCmdSync(cmd, prefix == "!")
return
}
// We are running the command asynchronously
var inReader, inWriter, outReader, outWriter *os.File
if prefix == "%" {
if app.ui.cmdPrefix == ">" {
return
}
// [exec.Cmd.StdoutPipe] cannot be used as it requires the output to be fully
// read before calling [exec.Cmd.Wait], however in this case Cmd.Wait should
// only wait for the command to finish executing regardless of whether the
// output has been fully read or not.
inReader, inWriter, err := os.Pipe()
if err != nil {
log.Printf("creating input pipe: %s", err)
return
}
cmd.Stdin = inReader
app.cmdIn = inWriter
outReader, outWriter, err = os.Pipe()
if err != nil {
log.Printf("creating output pipe: %s", err)
return
}
cmd.Stdout = outWriter
cmd.Stderr = outWriter
}
shellSetPG(cmd)
if err := cmd.Start(); err != nil {
app.ui.echoerrf("running shell: %s", err)
}
switch prefix {
case "%":
normal(app)
app.cmd = cmd
app.cmdOutBuf = nil
app.ui.cmdPrefix = ">"
app.ui.echo("")
go func() {
reader := bufio.NewReader(outReader)
for {
b, err := reader.ReadByte()
if err != nil {
if !errors.Is(err, io.EOF) && !errors.Is(err, fs.ErrClosed) {
log.Printf("reading command output: %s", err)
}
break
}
app.cmdOutBuf = append(app.cmdOutBuf, b)
if reader.Buffered() == 0 {
app.ui.exprChan <- &callExpr{"echo", []string{string(app.cmdOutBuf)}, 1}
}
if b == '\n' || b == '\r' {
app.cmdOutBuf = nil
}
}
}()
go func() {
if err := cmd.Wait(); err != nil {
log.Printf("running shell: %s", err)
}
inReader.Close()
inWriter.Close()
outReader.Close()
outWriter.Close()
app.cmd = nil
app.ui.cmdPrefix = ""
app.ui.exprChan <- &callExpr{"load", nil, 1}
}()
case "&":
go func() {
if err := cmd.Wait(); err != nil {
log.Printf("running shell: %s", err)
}
app.ui.exprChan <- &callExpr{"load", nil, 1}
}()
}
}
func (app *app) doComplete() (matches []compMatch) {
var longest string
switch app.ui.cmdPrefix {
case ":":
matches, longest = completeCmd(app.ui.cmdAccLeft)
case "$", "%", "!", "&":
matches, longest = completeShell(app.ui.cmdAccLeft)
case "/", "?":
matches, longest = completeSearch(app.ui.cmdAccLeft)
}
app.ui.cmdAccLeft = longest
app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, matches, -1)
return
}
func (app *app) menuComplete(direction int) {
if !app.menuCompActive {
app.menuCompTmp = tokenize(app.ui.cmdAccLeft)
app.menuComps = app.doComplete()
if len(app.menuComps) > 1 {
app.menuCompInd = -1
app.menuCompActive = true
}
} else {
app.menuCompInd += direction
if app.menuCompInd == len(app.menuComps) {
app.menuCompInd = 0
} else if app.menuCompInd < 0 {
app.menuCompInd = len(app.menuComps) - 1
}
toks := slices.Clone(app.menuCompTmp)
toks[len(toks)-1] = app.menuComps[app.menuCompInd].result
app.ui.cmdAccLeft = strings.Join(toks, " ")
}
app.ui.menu, app.ui.menuSelect = listMatches(app.ui.screen, app.menuComps, app.menuCompInd)
}
func (app *app) watchDir(dir *dir) {
if !gOpts.watch {
return
}
app.watch.add(dir.path)
// ensure dircounts are updated for child directories
for _, file := range dir.allFiles {
if file.IsDir() {
app.watch.add(file.path)
}
}
}
func (app *app) exportMode() {
getMode := func() string {
if app.menuCompActive {
return "compmenu"
}
if strings.HasPrefix(app.ui.cmdPrefix, "delete") {
return "delete"
}
if strings.HasPrefix(app.ui.cmdPrefix, "replace") || strings.HasPrefix(app.ui.cmdPrefix, "create") {
return "rename"
}
switch app.ui.cmdPrefix {
case "filter: ":
return "filter"
case "find: ", "find-back: ":
return "find"
case "mark-save: ", "mark-load: ", "mark-remove: ":
return "mark"
case "rename: ":
return "rename"
case "/", "?":
return "search"
case ":":
return "command"
case "$", "%", "!", "&":
return "shell"
case ">":
return "pipe"
case "":
if app.nav.isVisualMode() {
return "visual"
}
return "normal"
default:
return "unknown"
}
}
os.Setenv("lf_mode", getMode())
}