forked from antonmedv/walk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk.go
886 lines (774 loc) · 21.2 KB
/
walk.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
package walk
import (
"fmt"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
. "strings"
"time"
"unicode/utf8"
"github.com/antonmedv/clipboard"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/sahilm/fuzzy"
)
var version = "v2.2.0"
func Version() string { return version }
const separator = " " // Separator between columns.
var (
fileSeparator = string(filepath.Separator)
showIcons = false
)
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.
field *field // Bubble Tea Huh form field.
keys *keyMap // Key bindings.
st *Styles // Rendering attributes.
cmdline []string // Command line to open 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.
status 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
}
type position struct {
c, r int
offset int
}
type toDelete struct {
path string
at time.Time
}
type (
clearSearchMsg int
toBeDeletedMsg int
)
// New returns a new Model with the given options applied.
func New(options ...Option[*Model]) *Model {
m := (&Model{positions: make(map[string]position)}).With(options...)
// Use the default key bindings if none provided.
if m.keys == nil {
m.keys = m.keys.Default()
}
// Use the default style if none provided.
if m.st == nil {
m.st = m.st.Default()
}
return m
}
// Path returns an Option that sets the path for a Model.
func Path(path string) Option[*Model] {
return func(m *Model) *Model { return m.WithPath(path) }
}
// Size returns an Option that sets the width and height of a Model.
func Size(width, height int) Option[*Model] {
return func(m *Model) *Model { return m.WithSize(width, height) }
}
// Icons returns an Option that enables file type icons for a Model.
func Icons() Option[*Model] {
return func(m *Model) *Model { return m.WithIcons() }
}
// Command returns an Option that sets the "open file" command with for a Model.
func Command(cmd string) Option[*Model] {
return func(m *Model) *Model { return m.WithCommand(cmd) }
}
// Style returns an Option that sets the rendering attributes for a Model.
func Style(styles *Styles) Option[*Model] {
return func(m *Model) *Model { return m.WithStyle(styles) }
}
// Keys returns an Option that sets the key bindings for a Model.
func Keys(keys *keyMap) Option[*Model] {
return func(m *Model) *Model { return m.WithKeys(keys) }
}
// Field returns an Option that configures Model as a form field.
//
// This Option must be provided to initialize Model as a form field.
func Field(options ...Option[*field]) Option[*Model] {
return func(m *Model) *Model { return m.withField(options...) }
}
// Kill exits the program with the given exit status.
func Kill(status int) { os.Exit(status) }
// Init initializes the receiver.
//
// Init is a required method of the Bubble Tea framework's Model interface.
func (m *Model) Init() tea.Cmd {
if m.path == "" {
var err error
m.path, err = os.Getwd()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr,
m.st.Danger.Render("error: failed to get working directory"))
}
}
m.list()
return nil
}
// Update updates the receiver with the given message and returns the updated
// Model and any command to pass on to the Bubble Tea runtime.
//
// Update is a required method of the Bubble Tea framework's Model interface.
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
// 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:
if m.searchMode {
if key.Matches(msg, m.keys.Search) {
m.searchMode = false
return m, nil
} else if key.Matches(msg, m.keys.Back) {
if len(m.search) > 0 {
m.search = m.search[:len(m.search)-1]
return m, nil
}
} else if msg.Type == tea.KeyRunes {
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()
// Save search id to clear only current search after delay.
// User may have already started typing next search.
searchId := m.searchId
return m, tea.Tick(2*time.Second, func(time.Time) tea.Msg {
return clearSearchMsg(searchId)
})
}
}
switch {
case key.Matches(msg, m.keys.ForceQuit):
// _, _ = fmt.Fprintln(os.Stderr) // Keep last item visible after prompt.
m.status = 2
m.dontDoPendingDeletions()
return m, tea.Quit
case key.Matches(msg, m.keys.Quit, m.keys.QuitQ):
// _, _ = fmt.Fprintln(os.Stderr) // Keep last item visible after prompt.
fmt.Println(m.path) // Write to cd.
m.status = 0
m.performPendingDeletions()
return m, tea.Quit
case key.Matches(msg, m.keys.Open):
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.openCommand()
}
case key.Matches(msg, m.keys.Back):
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, m.keys.Up):
m.moveUp()
case key.Matches(msg, m.keys.Top, m.keys.PageUp, m.keys.VimTop):
m.moveTop()
case key.Matches(msg, m.keys.Bottom, m.keys.PageDown, m.keys.VimBottom):
m.moveBottom()
case key.Matches(msg, m.keys.Leftmost):
m.moveLeftmost()
case key.Matches(msg, m.keys.Rightmost):
m.moveRightmost()
case key.Matches(msg, m.keys.Home):
m.moveStart()
case key.Matches(msg, m.keys.End):
m.moveEnd()
case key.Matches(msg, m.keys.VimUp):
if !m.searchMode {
m.moveUp()
}
case key.Matches(msg, m.keys.Down):
m.moveDown()
case key.Matches(msg, m.keys.VimDown):
if !m.searchMode {
m.moveDown()
}
case key.Matches(msg, m.keys.Left):
m.moveLeft()
case key.Matches(msg, m.keys.VimLeft):
if !m.searchMode {
m.moveLeft()
}
case key.Matches(msg, m.keys.Right):
m.moveRight()
case key.Matches(msg, m.keys.VimRight):
if !m.searchMode {
m.moveRight()
}
case key.Matches(msg, m.keys.Search):
m.searchMode = true
m.searchId++
m.search = ""
case key.Matches(msg, m.keys.Preview):
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
}
m.previewContent = ""
return m, tea.ExitAltScreen
case key.Matches(msg, m.keys.Delete):
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)
})
}
m.deleteCurrentFile = true
}
return m, nil
case key.Matches(msg, m.keys.Undo):
if len(m.toBeDeleted) > 0 {
m.toBeDeleted = m.toBeDeleted[:len(m.toBeDeleted)-1]
m.list()
m.previewContent = ""
return m, nil
}
case key.Matches(msg, m.keys.Yank):
// copy path to clipboard
clipboard.WriteAll(m.path)
m.yankSuccess = true
return m, nil
} // 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.searchMode = false
}
case toBeDeletedMsg:
toBeDeleted := make([]toDelete, 0)
for _, td := range m.toBeDeleted {
if td.at.After(time.Now()) {
toBeDeleted = append(toBeDeleted, td)
} else {
_ = os.RemoveAll(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
}
// View returns a string representation of the receiver's current state
// including all markup, styling, terminal escape sequences, etc.
//
// View is a required method of the Bubble Tea framework's Model interface.
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 := len(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, len(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] = m.st.Danger.Render(names[i][j])
} else {
row[i] = m.st.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 := m.st.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 {
location = TrimSuffix(location, fileSeparator)
filter = fileSeparator + m.search
}
barLen := len(location) + len(filter)
if barLen > outputWidth {
location = location[min(barLen-outputWidth, len(location)):]
}
barStr := m.st.Bar.Render(location) + m.st.Search.Render(filter)
main := barStr + "\n" + Join(output, "\n")
if m.err != nil {
main = barStr + "\n" + m.st.Warning.Render(m.err.Error())
} else if len(m.files) == 0 {
main = barStr + "\n" + m.st.Warning.Render("No files")
}
// Delete bar.
if len(m.toBeDeleted) > 0 {
toDelete := m.toBeDeleted[len(m.toBeDeleted)-1]
timeLeft := int(time.Until(toDelete.at).Seconds())
deleteBar := fmt.Sprintf("%v deleted. (u)ndo %v", path.Base(toDelete.path), timeLeft)
main += "\n" + m.st.Danger.Render(deleteBar)
}
// Yank success.
if m.yankSuccess {
yankBar := fmt.Sprintf("yanked path to clipboard: %v", m.path)
main += "\n" + m.st.Bar.Render(yankBar)
}
if m.previewMode {
return lipgloss.JoinHorizontal(
lipgloss.Top,
main,
m.st.Preview.
MaxHeight(m.height).
Render(previewPane),
)
}
return main
}
// Exit exits the program with the receiver's current exit status.
func (m *Model) Exit() { Kill(m.status) }
// Field returns the receiver's field used in a form.
func (m *Model) Field() *field { return m.field }
// Value returns the path of the currently selected file.
func (m *Model) Value() string {
path, _ := m.filePath()
return path
}
// With returns the receiver with the given options applied.
func (m *Model) With(options ...Option[*Model]) *Model {
for _, option := range options {
m = option(m)
}
return m
}
// WithPath returns the receiver with the given path set.
func (m *Model) WithPath(path string) *Model {
m.path = path
return m
}
// WithSize returns the receiver with the given width and height set.
func (m *Model) WithSize(width, height int) *Model {
m.width = width
m.height = height
return m
}
// Size returns the width and height of the receiver.
func (m *Model) Size() (width, height int) {
return m.width, m.height
}
// WithIcons returns the receiver with file type icons enabled.
func (m *Model) WithIcons() *Model {
showIcons = true
parseIcons()
return m
}
// WithCommand returns the receiver with the given "open file" command set.
func (m *Model) WithCommand(cmd string) *Model {
m.cmdline = Fields(cmd)
return m
}
// WithStyle returns the receiver with the given rendering attributes set.
func (m *Model) WithStyle(styles *Styles) *Model {
m.st = styles
return m
}
// WithKeys returns the receiver with the given key bindings set.
func (m *Model) WithKeys(keys *keyMap) *Model {
m.keys = keys
return m
}
// withField returns the receiver with the given options applied to its field.
//
// This method must be called to initialize walk as a form field, and it must
// be called when initializing a new Model by providing a field Option.
//
// The types Model and field both implement different interfaces from the
// Bubble Tea framework. field is a specialization of Model that allows walk to
// be used as a discrete field in a Bubble Tea "huh" form application:
//
// | Interface | `field` | `Model` |
// |------------:|:-------:|:-------:|
// | `tea.Model` | ✓ | ✓ |
// | `huh.Field` | ✓ | |
func (m *Model) withField(options ...Option[*field]) *Model {
m.field = (&field{
Model: m,
value: new(FilePath),
validate: func(FilePath) error { return nil },
filter: textinput.New(),
}).With(options...).(*field)
return m
}
func (m *Model) AsField(options ...Option[*field]) *field {
return m.withField(options...).field
}
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.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) 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
}
m.err = nil
files:
for _, file := range 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
}
}
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) openCommand() tea.Cmd {
filePath, ok := m.filePath()
if !ok {
return nil
}
cmdline := m.cmdline
if len(cmdline) == 0 || cmdline[0] == "" {
cmdline = Fields(lookup([]string{"LK_COMMAND", "EDITOR"}, "less"))
}
var replace bool
for i, s := range cmdline {
if replace = Contains(s, "{}"); replace {
cmdline[i] = ReplaceAll(s, "{}", filePath)
break
}
}
if !replace {
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 {
return
}
fileInfo, err := os.Stat(filePath)
if err != nil {
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 = err.Error()
}
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 = m.st.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 = m.st.Warning.Render("No preview available")
}
}
func (m *Model) dontDoPendingDeletions() {
for _, toDelete := range m.toBeDeleted {
fmt.Fprintf(os.Stderr, "Was not deleted: %v\n", toDelete.path)
}
}
func (m *Model) performPendingDeletions() {
for _, toDelete := range m.toBeDeleted {
_ = os.RemoveAll(toDelete.path)
}
m.toBeDeleted = nil
}