-
Notifications
You must be signed in to change notification settings - Fork 2
/
history.go
169 lines (136 loc) · 3.88 KB
/
history.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
package main
//lint:file-ignore ST1005 some user-visible messages are stored in error values and thus occasionally require capitalization
import (
"errors"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"sync"
)
type History struct {
history []string
historyMutex sync.Mutex
}
// Returns a full filepath (path joined with the next history entry), passing an empty path returns an error
func (h *History) GetHistoryEntryForPath(path string, hiddenFiles bool) (string, error) {
if path == "" {
return "", errors.New("The path argument passed was an empty string")
}
h.historyMutex.Lock()
defer h.historyMutex.Unlock()
for _, e := range h.history {
if !hiddenFiles && strings.HasPrefix(filepath.Base(e), ".") {
continue
}
sub, err := filepath.Rel(path, e)
if err != nil {
continue
}
// HACKY
if strings.HasPrefix(sub, "..") {
continue
}
if strings.HasPrefix(e, path) {
if len(path) >= len(e) {
continue
}
if runtime.GOOS == "windows" {
drivePath := filepath.VolumeName(path) + string(os.PathSeparator)
if path == drivePath {
e = e[len(drivePath):]
} else {
e = e[len(path)+1:]
}
} else {
if len(path) == 1 {
e = e[1:]
} else {
e = e[len(path)+1:]
}
}
nextSlashIdx := strings.Index(e, string(os.PathSeparator))
if nextSlashIdx == -1 {
return filepath.Join(path, e), nil
}
return filepath.Join(path, e[:nextSlashIdx]), nil
}
}
return "", errors.New("No entry found")
}
// Returns the full history filepath entry (guaranteed to be the furthest down the history), passing an empty path returns an error
func (h *History) GetHistoryFullPath(path string, hiddenFiles bool) (string, error) {
if path == "" {
return "", errors.New("The path argument passed was an empty string")
}
pathFurthestDownHistory := path
for i := 0; ; i++ {
pathFurtherDown, err := h.GetHistoryFirstFullPathFound(pathFurthestDownHistory, hiddenFiles)
if i == 0 && err != nil {
return "", errors.New("No entry found")
}
if err != nil || filepath.Clean(pathFurtherDown) == filepath.Clean(pathFurthestDownHistory) {
break
}
pathFurthestDownHistory = pathFurtherDown
}
return pathFurthestDownHistory, nil
}
// Returns the first full history filepath entry found (not guaranteed to be the furthest down the history), passing an empty path returns an error
func (h *History) GetHistoryFirstFullPathFound(path string, hiddenFiles bool) (string, error) {
if path == "" {
return "", errors.New("The path argument passed was an empty string")
}
h.historyMutex.Lock()
defer h.historyMutex.Unlock()
for _, e := range h.history {
if !hiddenFiles && strings.HasPrefix(filepath.Base(e), ".") {
continue
}
sub, err := filepath.Rel(path, e)
if err != nil {
continue
}
// HACKY
if strings.HasPrefix(sub, "..") {
continue
}
if strings.HasPrefix(e, path) {
if len(path) >= len(e) {
continue
}
return e, nil
}
}
return "", errors.New("No entry found")
}
// TODO: Disallow adding like, parents of existing stuff
func (h *History) AddToHistory(path string) {
h.historyMutex.Lock()
defer h.historyMutex.Unlock()
if index := slices.Index(h.history, path); index != -1 {
if h.history[0] == path {
return
}
// Found in the history, but we need to move it to the front again
h.history = append(h.history[:index], h.history[index+1:]...)
h.history = append([]string{path}, h.history...)
return
}
h.history = append([]string{path}, h.history...)
}
func (h *History) RemoveFromHistory(path string) error {
h.historyMutex.Lock()
defer h.historyMutex.Unlock()
if index := slices.Index(h.history, path); index != -1 {
h.history = append(h.history[:index], h.history[index+1:]...)
return nil
}
return errors.New("path was not found in history")
}
func (h *History) ClearHistory() {
h.historyMutex.Lock()
defer h.historyMutex.Unlock()
h.history = []string{}
}