-
Notifications
You must be signed in to change notification settings - Fork 1
/
select.go
126 lines (114 loc) · 2.68 KB
/
select.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
package main
import (
"errors"
"fmt"
"path/filepath"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
func (m *model) selectAction() (*model, tea.Cmd) {
selected, err := m.selected()
if err != nil {
m.setError(err, "failed to select entry")
return m, nil
}
m.saveCursor()
if selected.hasMode(entryModeFile) {
m.setExit(sanitizeOutputPath(filepath.Join(m.path, selected.Name())))
if m.modeSubshell {
fmt.Print(m.exitStr)
}
return m, tea.Quit
}
if selected.hasMode(entryModeSymlink) {
sl, err := followSymlink(m.path, selected)
if err != nil {
m.setError(err, "failed to evaluate symlink")
return m, nil
}
if !sl.info.IsDir() {
// The symlink points to a file.
m.setExit(sanitizeOutputPath(sl.absPath))
if m.modeSubshell {
fmt.Print(m.exitStr)
}
return m, tea.Quit
}
m.setPath(sl.absPath)
} else if selected.hasMode(entryModeDir) {
path, err := filepath.Abs(filepath.Join(m.path, selected.Name()))
if err != nil {
m.setError(err, "failed to evaluate path")
return m, nil
}
m.setPath(path)
} else {
m.setError(
errors.New("selection is not a file, directory, or symlink"),
"unexpected file type",
)
return m, nil
}
err = m.list()
if err != nil {
m.restorePath()
m.setError(err, err.Error())
return m, nil
}
m.clearSearch()
// Return to ensure the cursor is not re-saved using the updated path.
return m, nil
}
func (m *model) searchSelectAction() (*model, tea.Cmd) {
selected, err := m.selected()
if err != nil {
m.setError(err, "failed to select entry")
m.clearSearch()
return m, nil
}
if selected.hasMode(entryModeFile) {
m.setExit(sanitizeOutputPath(filepath.Join(m.path, selected.Name())))
if m.modeSubshell {
fmt.Print(m.exitStr)
}
return m, tea.Quit
}
if selected.hasMode(entryModeSymlink) {
sl, err := followSymlink(m.path, selected)
if err != nil {
m.setError(err, "failed to evaluate symlink")
return m, nil
}
if !sl.info.IsDir() {
// The symlink points to a file.
m.setExit(sanitizeOutputPath(sl.absPath))
if m.modeSubshell {
fmt.Print(m.exitStr)
}
return m, tea.Quit
}
m.setPath(sl.absPath)
} else if selected.hasMode(entryModeDir) {
m.setPath(m.path + fileSeparator + selected.Name())
} else {
m.setError(
errors.New("selection is not a file, directory, or symlink"),
"unexpected file type",
)
return m, nil
}
// Trim repeated leading file separator characters that occur from searching back
// to the root directory.
if strings.HasPrefix(m.path, "//") {
m.path = m.path[1:]
}
m.search = ""
err = m.list()
if err != nil {
m.restorePath()
m.setError(err, err.Error())
m.clearSearch()
return m, nil
}
return m, nil
}