-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.go
187 lines (162 loc) · 3.83 KB
/
model.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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
var fileSeparator = string(filepath.Separator)
type model struct {
path string
prevPath string
entries []*entry
displayed int
exitCode int
exitStr string
error error
errorStr string
esc *remappedEscKey
search string
pathCache map[string]*cacheItem // Map path to cached state.
marks map[int]int // Map display index to entry index for marked entries.
c int // Cursor column position.
r int // Cursor row position.
columns int // Displayed columns.
rows int // Displayed columns.
width int // Terminal width.
height int // Terminal height.
modeColor bool
modeDebug bool
modeError bool
modeExit bool
modeFollowSymlink bool
modeHelp bool
modeHidden bool
modeList bool
modeMarks bool
modeSearch bool
modeSubshell bool
modeTrailing bool
hideStatusBar bool
}
func newModel() *model {
return &model{
width: 80,
height: 60,
esc: defaultEscRemapKey(),
pathCache: make(map[string]*cacheItem),
marks: make(map[int]int),
modeColor: true,
modeDebug: false,
modeError: false,
modeExit: false,
modeFollowSymlink: false,
modeHelp: false,
modeHidden: false,
modeList: false,
modeMarks: false,
modeSearch: false,
modeSubshell: false,
modeTrailing: true,
hideStatusBar: false,
}
}
func (m *model) normalMode() bool {
return !(m.modeSearch || m.modeDebug || m.modeHelp)
}
func (m *model) list() error {
files, err := os.ReadDir(m.path)
if err != nil {
return err
}
m.entries = []*entry{}
for _, file := range files {
ent, err := newEntry(file)
if err != nil {
return err
}
m.entries = append(m.entries, ent)
}
sortEntries(m.entries)
return nil
}
func (m *model) selected() (*entry, error) {
cache, ok := m.pathCache[m.path]
if !ok {
return nil, fmt.Errorf("cache item not found for %s", m.path)
}
idx, found := cache.lookupEntryIndex(m.displayIndex())
if !found {
return nil, errors.New("failed to map to valid entry index")
}
if idx > len(m.entries) {
return nil, fmt.Errorf("invalid index %d for entries with length %d", idx, len(m.entries))
}
return m.entries[idx], nil
}
func (m *model) location() string {
location := m.path
if userHomeDir, err := os.UserHomeDir(); err == nil {
location = strings.Replace(m.path, userHomeDir, "~", 1)
}
if runtime.GOOS == "windows" {
location = strings.ReplaceAll(strings.Replace(location, "\\/", fileSeparator, 1), "/", fileSeparator)
}
return location
}
func (m *model) displayNameOpts() []displayNameOption {
opts := []displayNameOption{}
if m.modeColor {
opts = append(opts, displayNameWithColor())
}
if m.modeFollowSymlink {
opts = append(opts, displayNameWithFollowSymlink(m.path))
}
if m.modeList {
opts = append(opts, displayNameWithList())
}
if m.modeTrailing {
opts = append(opts, displayNameWithTrailing())
}
return opts
}
func (m *model) displayIndex() int {
return index(m.c, m.r, m.rows)
}
func (m *model) setPath(path string) {
m.prevPath = m.path
m.path = path
}
func (m *model) restorePath() {
if m.prevPath != "" {
m.path = m.prevPath
m.prevPath = ""
}
}
func (m *model) setError(err error, status string) {
m.modeError = true
m.errorStr = status
m.error = err
}
func (m *model) clearError() {
m.modeError = false
m.errorStr = ""
m.error = nil
}
func (m *model) setExit(exitStr string) {
m.setExitWithCode(exitStr, 0)
}
func (m *model) setExitWithCode(exitStr string, exitCode int) {
m.modeExit = true
m.exitStr = exitStr
m.exitCode = exitCode
}
func (m *model) clearSearch() {
m.modeSearch = false
m.search = ""
}
func index(c int, r int, rows int) int {
return r + (c * rows)
}