-
Notifications
You must be signed in to change notification settings - Fork 3
/
pathos.go
380 lines (314 loc) · 7.95 KB
/
pathos.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
package main
import (
"fmt"
"io"
"log"
"os"
"strings"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var duplicatePaths map[string]struct{}
// this is an enum for Go
type sessionState uint
const (
listView sessionState = iota
inputView
)
type savePathMsg struct {
path string
cursor int
}
type deletePathMsg int
type saveShellSourceMsg struct {
m model
}
type errMsg error
// TODO Show color legend
const listHeight = 35
// Colors: https://www.ditig.com/256-colors-cheat-sheet
var (
titleStyle = lipgloss.NewStyle().MarginLeft(2)
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("11")) // Xterm Yellow (SYSTEM)
doesNotExistItemStyle = lipgloss.NewStyle().PaddingLeft(4).Foreground(lipgloss.Color("9")) // Xterm Red (SYSTEM)
selectedAndDoesNotExistItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("9"))
duplicateItemStyle = lipgloss.NewStyle().PaddingLeft(4).Foreground(lipgloss.Color("14")) // Xterm Aqua (SYSTEM)
selectedAndDuplicateItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("14"))
quitTextStyle = lipgloss.NewStyle().Margin(1, 0, 2, 4)
)
type item string
func (i item) FilterValue() string { return "" }
type itemDelegate struct{}
func (d itemDelegate) Height() int { return 1 }
func (d itemDelegate) Spacing() int { return 0 }
func (d itemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd { return nil }
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(item)
if !ok {
return
}
str := string(i)
fn := itemStyle.Render
if !directoryExists(str) {
fn = doesNotExistItemStyle.Render
} else if duplicatePath(str) {
fn = duplicateItemStyle.Render
}
if index == m.Index() {
fn = func(s string) string {
if directoryExists(s) {
return selectedItemStyle.Render("> " + s)
} else if duplicatePath(str) {
return selectedAndDuplicateItemStyle.Render("> " + s)
} else {
return selectedAndDoesNotExistItemStyle.Render("> " + s)
}
}
}
fmt.Fprint(w, fn(str))
}
type model struct {
keys HelpKeyMap
help help.Model
list list.Model
items []item
textInput textinput.Model
msg tea.Msg
err error
state sessionState
showPagination bool
}
func additionalKeys() []key.Binding {
return []key.Binding{
keys.NewPath,
keys.DeletePath,
keys.SaveShellSource,
}
}
func initialModel() model {
ti := setupTextInput()
items := createPaths()
duplicatePaths = findDuplicatePaths(items)
const defaultWidth = 60
l := list.New(items, itemDelegate{}, defaultWidth, listHeight)
l.Title = "pathos - CLI for editing a PATH env variable"
l.SetShowHelp(true)
l.SetShowStatusBar(true)
l.SetFilteringEnabled(false)
l.Styles.Title = titleStyle
l.AdditionalFullHelpKeys = additionalKeys
l.AdditionalShortHelpKeys = additionalKeys
m := model{
keys: keys,
help: help.New(),
list: l,
textInput: ti,
err: nil,
state: listView,
showPagination: false,
}
return m
}
func directoryExists(dir string) bool {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return false
}
return true
}
func savePathCmd(cursor int, path string) tea.Cmd {
return func() tea.Msg {
return savePathMsg{path: path, cursor: cursor}
}
}
func deletePathCmd(m model, id int) tea.Cmd {
return func() tea.Msg {
return deletePathMsg(id)
}
}
func saveShellSourceCmd(m model) tea.Cmd {
return func() tea.Msg {
return saveShellSourceMsg{m: m}
}
}
func saveShellSource(m model) (int, error) {
s := []string{}
for _, listItem := range m.list.Items() {
i, _ := listItem.(item)
path := string(i)
if path != "" {
s = append(s, path)
}
}
data := "export PATH=" + strings.Join(s, ":")
HOME := os.Getenv("HOME")
filename := HOME + "/pathos.sh"
file, err := os.Create(filename)
if err != nil {
return -1, err
}
defer file.Close()
return file.WriteString(data)
}
func (m model) Init() tea.Cmd {
return tea.EnterAltScreen
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.list.SetWidth(msg.Width)
return m, nil
case savePathMsg:
m.list.InsertItem(msg.cursor, item(msg.path))
duplicatePaths = findDuplicatePaths(m.list.Items())
return m, nil
case deletePathMsg:
m.list.RemoveItem(int(msg))
duplicatePaths = findDuplicatePaths(m.list.Items())
return m, nil
case saveShellSourceMsg:
saveShellSource(m)
return m, nil
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Quit):
return m, tea.Quit
case key.Matches(msg, keys.Enter):
if m.state == inputView {
text := strings.TrimSpace(m.textInput.Value())
if text != "" {
cursor := m.list.Cursor()
value := m.textInput.Value()
cmds = append(cmds, savePathCmd(cursor, value))
m.state = listView
}
}
case key.Matches(msg, keys.NewPath):
m.state = inputView
return m, nil
case key.Matches(msg, keys.DeletePath):
if m.state == listView {
i := m.list.Index()
cmds = append(cmds, deletePathCmd(m, i))
}
case key.Matches(msg, keys.SaveShellSource):
cmds = append(cmds, saveShellSourceCmd(m))
}
// We handle errors just like any other message
case errMsg:
m.err = msg
return m, nil
}
// Update different view states
switch m.state {
case inputView:
m.textInput, cmd = m.textInput.Update(msg)
case listView:
m.list, cmd = m.list.Update(msg)
}
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m model) View() string {
switch m.state {
case inputView:
return m.textInput.View()
default:
return m.list.View()
}
}
func getPaths() []string {
PATH := os.Getenv("PATH")
return strings.Split(PATH, ":")
}
func createPaths() []list.Item {
paths := getPaths()
items := make([]list.Item, len(paths))
for i, path := range paths {
items[i] = item(path)
}
return items
}
func setupTextInput() textinput.Model {
ti := textinput.New()
ti.Prompt = "Enter directory: "
ti.Placeholder = "/"
ti.SetValue("")
ti.Blink()
ti.Focus()
ti.CharLimit = 156
ti.Width = 50
return ti
}
func duplicatePath(path string) bool {
_, isPresent := duplicatePaths[path]
return isPresent
}
func findDuplicatePaths(items []list.Item) map[string]struct{} {
pathMap := make(map[string]int)
duplicates := make(map[string]struct{})
for _, listItem := range items {
i, ok := listItem.(item)
if ok {
path := string(i)
if value, ok := pathMap[path]; ok {
pathMap[path] = value + 1
} else {
pathMap[path] = 0
}
}
}
for path, count := range pathMap {
if count > 1 {
duplicates[path] = struct{}{}
}
}
return duplicates
}
type HelpKeyMap struct {
Help key.Binding
Quit key.Binding
NewPath key.Binding
DeletePath key.Binding
SaveShellSource key.Binding
Enter key.Binding
}
var keys = HelpKeyMap{
Enter: key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "submit new path"),
),
NewPath: key.NewBinding(
key.WithKeys("N"),
key.WithHelp("N", "new"),
),
DeletePath: key.NewBinding(
key.WithKeys("D"),
key.WithHelp("D", "delete"),
),
SaveShellSource: key.NewBinding(
key.WithKeys("S"),
key.WithHelp("S", "save paths"),
),
}
func main() {
if os.Getenv("HELP_DEBUG") != "" {
if f, err := tea.LogToFile("debug.log", "help"); err != nil {
fmt.Println("Couldn't open a file for logging:", err)
os.Exit(1)
} else {
log.SetOutput(f)
defer f.Close()
}
}
if err := tea.NewProgram(initialModel()).Start(); err != nil {
fmt.Printf("Could not start program :(\n%v\n", err)
os.Exit(1)
}
}