-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathmain.go
1068 lines (944 loc) · 24.8 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
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"fmt"
"io/fs"
"math"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
. "strings"
"sync"
"text/tabwriter"
"time"
"unicode/utf8"
"github.com/antonmedv/clipboard"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-runewidth"
"github.com/muesli/termenv"
"github.com/sahilm/fuzzy"
)
var Version = "v1.10.0"
const separator = " " // Separator between columns.
var (
bold = lipgloss.NewStyle().Bold(true)
warning = lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).PaddingLeft(1).PaddingRight(1)
preview = lipgloss.NewStyle().PaddingLeft(2)
cursor = lipgloss.NewStyle().Background(lipgloss.Color("#825DF2")).Foreground(lipgloss.Color("#FFFFFF"))
bar = lipgloss.NewStyle().Background(lipgloss.Color("#5C5C5C")).Foreground(lipgloss.Color("#FFFFFF"))
search = lipgloss.NewStyle().Background(lipgloss.Color("#499F1C")).Foreground(lipgloss.Color("#FFFFFF"))
danger = lipgloss.NewStyle().Background(lipgloss.Color("#FF0000")).Foreground(lipgloss.Color("#FFFFFF"))
fileSeparator = string(filepath.Separator)
showIcons = false
dirOnly = false
startPreviewMode = false
fuzzyByDefault = false
strlen = runewidth.StringWidth
)
var (
keyForceQuit = key.NewBinding(key.WithKeys("ctrl+c"))
keyQuit = key.NewBinding(key.WithKeys("esc"))
keyQuitQ = key.NewBinding(key.WithKeys("q"))
keyOpen = key.NewBinding(key.WithKeys("enter"))
keyBack = key.NewBinding(key.WithKeys("backspace"))
keyFnDelete = key.NewBinding(key.WithKeys("delete"))
keyUp = key.NewBinding(key.WithKeys("up"))
keyDown = key.NewBinding(key.WithKeys("down"))
keyLeft = key.NewBinding(key.WithKeys("left"))
keyRight = key.NewBinding(key.WithKeys("right"))
keyTop = key.NewBinding(key.WithKeys("shift+up"))
keyBottom = key.NewBinding(key.WithKeys("shift+down"))
keyLeftmost = key.NewBinding(key.WithKeys("shift+left"))
keyRightmost = key.NewBinding(key.WithKeys("shift+right"))
keyPageUp = key.NewBinding(key.WithKeys("pgup"))
keyPageDown = key.NewBinding(key.WithKeys("pgdown"))
keyHome = key.NewBinding(key.WithKeys("home"))
keyEnd = key.NewBinding(key.WithKeys("end"))
keyVimUp = key.NewBinding(key.WithKeys("k"))
keyVimDown = key.NewBinding(key.WithKeys("j"))
keyVimLeft = key.NewBinding(key.WithKeys("h"))
keyVimRight = key.NewBinding(key.WithKeys("l"))
keyVimTop = key.NewBinding(key.WithKeys("g"))
keyVimBottom = key.NewBinding(key.WithKeys("G"))
keySearch = key.NewBinding(key.WithKeys("/"))
keyPreview = key.NewBinding(key.WithKeys(" "))
keyDelete = key.NewBinding(key.WithKeys("d"))
keyUndo = key.NewBinding(key.WithKeys("u"))
keyYank = key.NewBinding(key.WithKeys("y"))
keyHidden = key.NewBinding(key.WithKeys("."))
)
func main() {
go emitCO2(time.Second)
startPath, err := os.Getwd()
if err != nil {
panic(err)
}
argsWithoutFlags := make([]string, 0)
for i := 1; i < len(os.Args); i++ {
if os.Args[i] == "--help" || os.Args[1] == "-h" {
usage()
}
if os.Args[i] == "--version" || os.Args[1] == "-v" {
version()
}
if os.Args[i] == "--icons" {
showIcons = true
parseIcons()
continue
}
if os.Args[i] == "--dir-only" {
dirOnly = true
continue
}
if os.Args[i] == "--preview" {
startPreviewMode = true
continue
}
if os.Args[i] == "--fuzzy" {
fuzzyByDefault = true
continue
}
argsWithoutFlags = append(argsWithoutFlags, os.Args[i])
}
if len(argsWithoutFlags) > 0 {
startPath, err = filepath.Abs(argsWithoutFlags[0])
if err != nil {
panic(err)
}
}
output := termenv.NewOutput(os.Stderr)
lipgloss.SetColorProfile(output.ColorProfile())
m := &model{
path: startPath,
width: 80,
height: 60,
positions: make(map[string]position),
previewMode: startPreviewMode,
}
m.list()
p := tea.NewProgram(m, tea.WithOutput(os.Stderr))
if _, err := p.Run(); err != nil {
panic(err)
}
os.Exit(m.exitCode)
}
type model struct {
path string // Current dir path we are looking at.
files []fs.DirEntry // Files we are looking at.
err error // Error while listing files.
c, r int // Selector position in columns and rows.
columns, rows int // Displayed amount of rows and columns.
width, height int // Terminal size.
offset int // Scroll position.
positions map[string]position // Map of cursor positions per path.
search string // Type to select files with this value.
searchMode bool // Whether type-to-select is active.
searchId int // Search id to indicate what search we are currently on.
matchedIndexes []int // List of char found indexes.
prevName string // Base name of previous directory before "up".
findPrevName bool // On View(), set c&r to point to prevName.
exitCode int // Exit code.
previewMode bool // Whether preview is active.
previewContent string // Content of preview.
deleteCurrentFile bool // Whether to delete current file.
toBeDeleted []toDelete // Map of files to be deleted.
yankSuccess bool // Show yank info
hideHidden bool // Hide hidden files
}
type position struct {
c, r int
offset int
}
type toDelete struct {
path string
at time.Time
}
type (
clearSearchMsg int
toBeDeletedMsg int
)
func (m *model) Init() tea.Cmd {
return nil
}
func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
if m.height < 3 {
m.height = 3
}
// Reset position history as c&r changes.
m.positions = make(map[string]position)
// Keep cursor at same place.
fileName, ok := m.fileName()
if ok {
m.prevName = fileName
m.findPrevName = true
}
// Also, m.c&r no longer point to the correct indexes.
m.c = 0
m.r = 0
return m, nil
case tea.KeyMsg:
// Make undo work even if we are in fuzzy mode.
if key.Matches(msg, keyUndo) && len(m.toBeDeleted) > 0 {
m.toBeDeleted = m.toBeDeleted[:len(m.toBeDeleted)-1]
m.list()
m.previewContent = ""
return m, nil
}
if fuzzyByDefault {
if key.Matches(msg, keyBack) {
if len(m.search) > 0 {
m.search = m.search[:strlen(m.search)-1]
return m, nil
}
} else if msg.Type == tea.KeyRunes {
m.updateSearch(msg)
// Save search id to clear only current search after delay.
// User may have already started typing next search.
m.searchId++
searchId := m.searchId
return m, tea.Tick(2*time.Second, func(time.Time) tea.Msg {
return clearSearchMsg(searchId)
})
}
} else if m.searchMode {
if key.Matches(msg, keySearch) {
m.searchMode = false
return m, nil
} else if key.Matches(msg, keyBack) {
if len(m.search) > 0 {
m.search = m.search[:strlen(m.search)-1]
} else {
m.searchMode = false
}
return m, nil
} else if msg.Type == tea.KeyRunes {
m.updateSearch(msg)
return m, nil
}
}
switch {
case key.Matches(msg, keyForceQuit):
_, _ = fmt.Fprintln(os.Stderr) // Keep last item visible after prompt.
m.exitCode = 2
m.dontDoPendingDeletions()
return m, tea.Quit
case key.Matches(msg, keyQuit, keyQuitQ):
_, _ = fmt.Fprintln(os.Stderr) // Keep last item visible after prompt.
fmt.Println(m.path) // Write to cd.
m.exitCode = 0
m.performPendingDeletions()
return m, tea.Quit
case key.Matches(msg, keyOpen):
m.search = ""
m.searchMode = false
filePath, ok := m.filePath()
if !ok {
return m, nil
}
if fi := fileInfo(filePath); fi.IsDir() {
// Enter subdirectory.
m.path = filePath
if p, ok := m.positions[m.path]; ok {
m.c = p.c
m.r = p.r
m.offset = p.offset
} else {
m.c = 0
m.r = 0
m.offset = 0
}
m.list()
} else {
// Open file. This will block until complete.
return m, m.openEditor()
}
case key.Matches(msg, keyBack):
m.search = ""
m.searchMode = false
m.prevName = filepath.Base(m.path)
m.path = filepath.Join(m.path, "..")
if p, ok := m.positions[m.path]; ok {
m.c = p.c
m.r = p.r
m.offset = p.offset
} else {
m.findPrevName = true
}
m.list()
return m, nil
case key.Matches(msg, keyUp):
m.moveUp()
case key.Matches(msg, keyTop, keyPageUp, keyVimTop):
m.moveTop()
case key.Matches(msg, keyBottom, keyPageDown, keyVimBottom):
m.moveBottom()
case key.Matches(msg, keyLeftmost):
m.moveLeftmost()
case key.Matches(msg, keyRightmost):
m.moveRightmost()
case key.Matches(msg, keyHome):
m.moveStart()
case key.Matches(msg, keyEnd):
m.moveEnd()
case key.Matches(msg, keyVimUp):
m.moveUp()
case key.Matches(msg, keyDown):
m.moveDown()
case key.Matches(msg, keyVimDown):
m.moveDown()
case key.Matches(msg, keyLeft):
m.moveLeft()
case key.Matches(msg, keyVimLeft):
m.moveLeft()
case key.Matches(msg, keyRight):
m.moveRight()
case key.Matches(msg, keyVimRight):
m.moveRight()
case key.Matches(msg, keySearch):
m.searchMode = true
m.searchId++
m.search = ""
case key.Matches(msg, keyPreview):
m.previewMode = !m.previewMode
// Reset position history as c&r changes.
m.positions = make(map[string]position)
// Keep cursor at same place.
fileName, ok := m.fileName()
if !ok {
return m, nil
}
m.prevName = fileName
m.findPrevName = true
if m.previewMode {
return m, tea.EnterAltScreen
} else {
m.previewContent = ""
return m, tea.ExitAltScreen
}
case key.Matches(msg, keyDelete, keyFnDelete):
filePathToDelete, ok := m.filePath()
if ok {
if m.deleteCurrentFile {
m.deleteCurrentFile = false
m.toBeDeleted = append(m.toBeDeleted, toDelete{
path: filePathToDelete,
at: time.Now().Add(6 * time.Second),
})
m.list()
m.previewContent = ""
return m, tea.Tick(time.Second, func(time.Time) tea.Msg {
return toBeDeletedMsg(0)
})
} else {
m.deleteCurrentFile = true
}
}
return m, nil
case key.Matches(msg, keyYank):
// copy path to clipboard
clipboard.WriteAll(m.path)
m.yankSuccess = true
return m, nil
case key.Matches(msg, keyHidden):
m.hideHidden = !m.hideHidden
m.list()
} // End of switch statement for key presses.
m.deleteCurrentFile = false
m.yankSuccess = false
m.updateOffset()
m.saveCursorPosition()
case clearSearchMsg:
if m.searchId == int(msg) {
m.search = ""
m.searchMode = false
}
case toBeDeletedMsg:
toBeDeleted := make([]toDelete, 0)
for _, td := range m.toBeDeleted {
if td.at.After(time.Now()) {
toBeDeleted = append(toBeDeleted, td)
} else {
remove(td.path)
}
}
m.toBeDeleted = toBeDeleted
if len(m.toBeDeleted) > 0 {
return m, tea.Tick(time.Second, func(time.Time) tea.Msg {
return toBeDeletedMsg(0)
})
}
}
return m, nil
}
func (m *model) updateSearch(msg tea.KeyMsg) {
m.search += string(msg.Runes)
names := make([]string, len(m.files))
for i, fi := range m.files {
names[i] = fi.Name()
}
matches := fuzzy.Find(m.search, names)
if len(matches) > 0 {
m.matchedIndexes = matches[0].MatchedIndexes
index := matches[0].Index
m.c = index / m.rows
m.r = index % m.rows
}
m.updateOffset()
m.saveCursorPosition()
}
func (m *model) View() string {
width := m.width
if m.previewMode {
width = m.width / 2
}
height := m.listHeight()
var names [][]string
names, m.rows, m.columns = wrap(m.files, width, height, func(name string, i, j int) {
if m.findPrevName && m.prevName == name {
m.c = i
m.r = j
}
})
// If we need to select previous directory on "up".
if m.findPrevName {
m.findPrevName = false
m.updateOffset()
m.saveCursorPosition()
}
// After we have updated offset and saved cursor position, we can
// preview currently selected file.
m.preview()
// Get output rows width before coloring.
outputWidth := strlen(path.Base(m.path)) // Use current dir name as default.
if m.previewMode {
row := make([]string, m.columns)
for i := 0; i < m.columns; i++ {
if len(names[i]) > 0 {
row[i] = names[i][0]
} else {
outputWidth = width
}
}
outputWidth = max(outputWidth, strlen(Join(row, separator)))
} else {
outputWidth = width
}
// Let's add colors to file names.
output := make([]string, m.rows)
for j := 0; j < m.rows; j++ {
row := make([]string, m.columns)
for i := 0; i < m.columns; i++ {
if i == m.c && j == m.r {
if m.deleteCurrentFile {
row[i] = danger.Render(names[i][j])
} else {
row[i] = cursor.Render(names[i][j])
}
} else {
row[i] = names[i][j]
}
}
output[j] = Join(row, separator)
}
if len(output) >= m.offset+height {
output = output[m.offset : m.offset+height]
}
// Preview pane.
fileName, _ := m.fileName()
previewPane := bar.Render(fileName) + "\n"
previewPane += m.previewContent
// Location bar (grey).
location := m.path
if userHomeDir, err := os.UserHomeDir(); err == nil {
location = Replace(m.path, userHomeDir, "~", 1)
}
if runtime.GOOS == "windows" {
location = ReplaceAll(Replace(location, "\\/", fileSeparator, 1), "/", fileSeparator)
}
// Filter bar (green).
filter := ""
if m.searchMode || fuzzyByDefault {
filter = fileSeparator + m.search
// If fuzzy is on and search is empty, don't show filter.
if fuzzyByDefault && m.search == "" {
filter = ""
}
}
barLen := strlen(location) + strlen(filter)
if barLen > outputWidth {
location = location[min(barLen-outputWidth, strlen(location)):]
}
barStr := bar.Render(location) + search.Render(filter)
main := barStr + "\n" + Join(output, "\n")
if m.err != nil {
main = barStr + "\n" + warning.Render(m.err.Error())
} else if len(m.files) == 0 {
main = barStr + "\n" + warning.Render("No files")
}
// Delete bar.
if len(m.toBeDeleted) > 0 {
toDelete := m.toBeDeleted[len(m.toBeDeleted)-1]
timeLeft := int(toDelete.at.Sub(time.Now()).Seconds())
deleteBar := fmt.Sprintf("%v deleted. (u)ndo %v", path.Base(toDelete.path), timeLeft)
main += "\n" + danger.Render(deleteBar)
}
// Yank success.
if m.yankSuccess {
yankBar := fmt.Sprintf("yanked path to clipboard: %v", m.path)
main += "\n" + bar.Render(yankBar)
}
if m.previewMode {
return lipgloss.JoinHorizontal(
lipgloss.Top,
main,
preview.
MaxHeight(m.height).
Render(previewPane),
)
} else {
return main
}
}
func (m *model) moveUp() {
m.r--
if m.r < 0 {
m.r = m.rows - 1
m.c--
}
if m.c < 0 {
m.r = m.rows - 1 - (m.columns*m.rows - len(m.files))
m.c = m.columns - 1
}
}
func (m *model) moveDown() {
m.r++
if m.r >= m.rows {
m.r = 0
m.c++
}
if m.c >= m.columns {
m.c = 0
}
if m.c == m.columns-1 && (m.columns-1)*m.rows+m.r >= len(m.files) {
m.r = 0
m.c = 0
}
}
func (m *model) moveLeft() {
m.c--
if m.c < 0 {
m.c = m.columns - 1
}
if m.c == m.columns-1 && (m.columns-1)*m.rows+m.r >= len(m.files) {
m.r = m.rows - 1 - (m.columns*m.rows - len(m.files))
m.c = m.columns - 1
}
}
func (m *model) moveRight() {
m.c++
if m.c >= m.columns {
m.c = 0
}
if m.c == m.columns-1 && (m.columns-1)*m.rows+m.r >= len(m.files) {
m.r = m.rows - 1 - (m.columns*m.rows - len(m.files))
m.c = m.columns - 1
}
}
func (m *model) moveTop() {
m.r = 0
}
func (m *model) moveBottom() {
m.r = m.rows - 1
if m.c == m.columns-1 && (m.columns-1)*m.rows+m.r >= len(m.files) {
m.r = m.rows - 1 - (m.columns*m.rows - len(m.files))
}
}
func (m *model) moveLeftmost() {
m.c = 0
}
func (m *model) moveRightmost() {
m.c = m.columns - 1
if (m.columns-1)*m.rows+m.r >= len(m.files) {
m.r = m.rows - 1 - (m.columns*m.rows - len(m.files))
}
}
func (m *model) moveStart() {
m.moveLeftmost()
m.moveTop()
}
func (m *model) moveEnd() {
m.moveRightmost()
m.moveBottom()
}
func (m *model) list() {
var err error
m.files = nil
// ReadDir already returns files and dirs sorted by filename.
files, err := os.ReadDir(m.path)
if err != nil {
m.err = err
return
} else {
m.err = nil
}
files:
for _, file := range files {
if m.hideHidden && HasPrefix(file.Name(), ".") {
continue files
}
if dirOnly && !file.IsDir() {
continue files
}
for _, toDelete := range m.toBeDeleted {
if path.Join(m.path, file.Name()) == toDelete.path {
continue files
}
}
m.files = append(m.files, file)
}
}
func (m *model) listHeight() int {
h := m.height - 1 // Subtract 1 for location bar.
if len(m.toBeDeleted) > 0 {
h-- // Subtract 1 for delete bar.
}
return h
}
func (m *model) updateOffset() {
height := m.listHeight()
// Scrolling down.
if m.r >= m.offset+height {
m.offset = m.r - height + 1
}
// Scrolling up.
if m.r < m.offset {
m.offset = m.r
}
// Don't scroll more than there are rows.
if m.offset > m.rows-height && m.rows > height {
m.offset = m.rows - height
}
if m.offset < 0 {
m.offset = 0
}
}
func (m *model) saveCursorPosition() {
m.positions[m.path] = position{
c: m.c,
r: m.r,
offset: m.offset,
}
}
func (m *model) fileName() (string, bool) {
i := m.c*m.rows + m.r
if i >= len(m.files) || i < 0 {
return "", false
}
return m.files[i].Name(), true
}
func (m *model) filePath() (string, bool) {
fileName, ok := m.fileName()
if !ok {
return fileName, false
}
return path.Join(m.path, fileName), true
}
func (m *model) openEditor() tea.Cmd {
filePath, ok := m.filePath()
if !ok {
return nil
}
cmdline := Split(lookup([]string{"WALK_EDITOR", "EDITOR"}, "less"), " ")
cmdline = append(cmdline, filePath)
execCmd := exec.Command(cmdline[0], cmdline[1:]...)
return tea.ExecProcess(execCmd, func(err error) tea.Msg {
// Note: we could return a message here indicating that editing is
// finished and altering our application about any errors. For now,
// however, that's not necessary.
return nil
})
}
func (m *model) preview() {
if !m.previewMode {
return
}
filePath, ok := m.filePath()
if !ok {
// Normally this should not happen
m.previewContent = warning.Render("Invalid file to preview")
return
}
fileInfo, err := os.Stat(filePath)
if err != nil {
m.previewContent = warning.Render(err.Error())
return
}
width := m.width / 2
height := m.height - 1 // Subtract 1 for name bar.
if fileInfo.IsDir() {
files, err := os.ReadDir(filePath)
if err != nil {
m.previewContent = warning.Render(err.Error())
return
}
if len(files) == 0 {
m.previewContent = warning.Render("No files")
return
}
names, rows, columns := wrap(files, width, height, nil)
output := make([]string, rows)
for j := 0; j < rows; j++ {
row := make([]string, columns)
for i := 0; i < columns; i++ {
row[i] = names[i][j]
}
output[j] = Join(row, separator)
}
if len(output) >= height {
output = output[0:height]
}
m.previewContent = Join(output, "\n")
return
}
if isImageExt(filePath) {
img, err := drawImage(filePath, width, height)
if err != nil {
m.previewContent = warning.Render("No image preview available")
return
}
m.previewContent = img
return
}
var content []byte
// If file is too big (> 100kb), read only first 100kb.
if fileInfo.Size() > 100*1024 {
file, err := os.Open(filePath)
if err != nil {
m.previewContent = err.Error()
return
}
defer file.Close()
content = make([]byte, 100*1024)
_, err = file.Read(content)
if err != nil {
m.previewContent = err.Error()
return
}
} else {
content, err = os.ReadFile(filePath)
if err != nil {
m.previewContent = err.Error()
return
}
}
switch {
case utf8.Valid(content):
m.previewContent = leaveOnlyAscii(content)
default:
m.previewContent = warning.Render("No preview available")
}
}
func leaveOnlyAscii(content []byte) string {
var result []byte
for _, b := range content {
if b == '\t' {
result = append(result, ' ', ' ', ' ', ' ')
} else if b == '\r' {
continue
} else if (b >= 32 && b <= 127) || b == '\n' { // '\n' is kept if newline needs to be retained
result = append(result, b)
}
}
return string(result)
}
// TODO: Write tests for this function.
func wrap(files []os.DirEntry, width int, height int, callback func(name string, i, j int)) ([][]string, int, int) {
// If the directory is empty, return no names, rows and columns.
if len(files) == 0 {
return nil, 0, 0
}
// If it's possible to fit all files in one column on a third of the screen,
// just use one column. Otherwise, let's squeeze listing in half of screen.
columns := len(files) / max(1, height/3)
if columns <= 0 {
columns = 1
}
// Max number of files to display in one column is 10 or 4 columns in total.
columnsEstimate := int(math.Ceil(float64(len(files)) / 10))
columns = max(columns, min(columnsEstimate, 4))
// For large lists, don't use more than 2 columns.
if len(files) > 100 {
columns = 2
}
// Fifteenth column is enough for everyone.
if columns > 15 {
columns = 15
}
start:
// Let's try to fit everything in terminal width with this many columns.
// If we are not able to do it, decrease column number and goto start.
rows := int(math.Ceil(float64(len(files)) / float64(columns)))
names := make([][]string, columns)
n := 0
for i := 0; i < columns; i++ {
names[i] = make([]string, rows)
maxNameSize := 0 // We will use this to determine max name size, and pad names in column with spaces.
for j := 0; j < rows; j++ {
if n >= len(files) {
break // No more files to display.
}
name := ""
if showIcons {
info, err := files[n].Info()
if err == nil {
icon := icons.getIcon(info)
if icon != "" {
name += icon + " "
}
}
}
name += files[n].Name()
if callback != nil {
callback(files[n].Name(), i, j)
}
if files[n].IsDir() {
// Dirs should have a slash at the end.
name += fileSeparator
}
n++ // Next file.
if maxNameSize < strlen(name) {
maxNameSize = strlen(name)
}
names[i][j] = name
}
// Append spaces to make all names in one column of same size.
for j := 0; j < rows; j++ {
names[i][j] += Repeat(" ", maxNameSize-strlen(names[i][j]))
}
}
// Let's verify was all columns have at least one file.
for i := 0; i < columns; i++ {
if names[i] == nil {
columns--
goto start
}
columnHaveAtLeastOneFile := false
for j := 0; j < rows; j++ {
if names[i][j] != "" {
columnHaveAtLeastOneFile = true
break
}
}
if !columnHaveAtLeastOneFile {
columns--
goto start
}
}
for j := 0; j < rows; j++ {
row := make([]string, columns)
for i := 0; i < columns; i++ {
row[i] = names[i][j]
}
if strlen(Join(row, separator)) > width && columns > 1 {
// Yep. No luck, let's decrease number of columns and try one more time.
columns--
goto start
}
}
return names, rows, columns
}
func emitCO2(duration time.Duration) {
ctx, cancel := context.WithTimeout(context.Background(), duration)
defer cancel()
numCPU := runtime.NumCPU()
runtime.GOMAXPROCS(numCPU)
var wg sync.WaitGroup
for i := 0; i < numCPU; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
default:
}
}
}()
}
wg.Wait()
}
func (m *model) dontDoPendingDeletions() {
for _, toDelete := range m.toBeDeleted {
fmt.Fprintf(os.Stderr, "Was not deleted: %v\n", toDelete.path)
}