-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.go
453 lines (402 loc) · 11.2 KB
/
main.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
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/mattn/go-isatty"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/sahilm/fuzzy"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
var (
helpText = strings.TrimSpace(`
Nap is a code snippet manager for your terminal.
https://github.com/maaslalani/nap
Usage:
nap - for interactive mode
nap list - list all snippets
nap <snippet> - print snippet to stdout
Create:
nap < main.go - save snippet from stdin
nap example/main.go < main.go - save snippet with name`)
)
func main() {
runCLI(os.Args[1:])
}
func runCLI(args []string) {
config := readConfig()
snippets := readSnippets(config)
snippets = migrateSnippets(config, snippets)
snippets = scanSnippets(config, snippets)
stdin := readStdin()
if stdin != "" {
saveSnippet(stdin, args, config, snippets)
return
}
if len(args) > 0 {
switch args[0] {
case "list":
listSnippets(snippets)
case "-h", "--help":
fmt.Println(helpText)
default:
snippet := findSnippet(args[0], snippets)
fmt.Print(snippet.Content(isatty.IsTerminal(os.Stdout.Fd())))
}
return
}
err := runInteractiveMode(config, snippets)
if err != nil {
fmt.Println("Alas, there's been an error", err)
}
}
// parseName returns a folder, name, and language for the given name.
// this is useful for parsing file names when passed as command line arguments.
//
// Example:
//
// Notes/Hello.go -> (Notes, Hello, go)
// Hello.go -> (Misc, Hello, go)
// Notes/Hello -> (Notes, Hello, go)
func parseName(s string) (string, string, string) {
var (
folder = defaultSnippetFolder
name = defaultSnippetName
language = defaultLanguage
remaining string
)
tokens := strings.Split(s, "/")
if len(tokens) > 1 {
folder = tokens[0]
remaining = tokens[1]
} else {
remaining = tokens[0]
}
tokens = strings.Split(remaining, ".")
if len(tokens) > 1 {
name = tokens[0]
language = tokens[1]
} else {
name = tokens[0]
}
return folder, name, language
}
// readStdin returns the stdin that is piped in to the command line interface.
func readStdin() string {
stat, err := os.Stdin.Stat()
if err != nil {
return ""
}
if stat.Mode()&os.ModeCharDevice != 0 {
return ""
}
reader := bufio.NewReader(os.Stdin)
var b strings.Builder
for {
r, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
_, err = b.WriteRune(r)
if err != nil {
return ""
}
}
return b.String()
}
// readSnippets returns all the snippets read from the snippets.json file.
func readSnippets(config Config) []Snippet {
var snippets []Snippet
file := filepath.Join(config.Home, config.File)
dir, err := os.ReadFile(file)
if err != nil {
// File does not exist, create one.
err := os.MkdirAll(config.Home, os.ModePerm)
if err != nil {
fmt.Printf("Unable to create directory %s, %+v", config.Home, err)
}
f, err := os.Create(file)
if err != nil {
fmt.Printf("Unable to create file %s, %+v", file, err)
}
defer f.Close()
dir = []byte("[]")
_, _ = f.Write(dir)
}
err = json.Unmarshal(dir, &snippets)
if err != nil {
fmt.Printf("Unable to unmarshal %s file, %+v\n", file, err)
return snippets
}
return snippets
}
// migrateSnippets migrates any legacy snippet <dir>-<file> format to the new <dir>/<file> format
func migrateSnippets(config Config, snippets []Snippet) []Snippet {
var migrated bool
for idx, snippet := range snippets {
legacyPath := filepath.Join(config.Home, snippet.LegacyPath())
if _, err := os.Stat(legacyPath); err != nil {
if !errors.Is(err, fs.ErrNotExist) {
fmt.Printf("could not access %q: %v\n", legacyPath, err)
}
continue
}
file := strings.TrimPrefix(snippet.LegacyPath(), fmt.Sprintf("%s-", snippet.Folder))
newDir := filepath.Join(config.Home, snippet.Folder)
newPath := filepath.Join(newDir, file)
if err := os.MkdirAll(newDir, os.ModePerm); err != nil {
fmt.Printf("could not create %q: %v\n", newDir, err)
continue
}
if err := os.Rename(legacyPath, newPath); err != nil {
fmt.Printf("could not move %q to %q: %v\n", legacyPath, newPath, err)
}
migrated = true
snippet.File = file
snippets[idx] = snippet
}
if migrated {
writeSnippets(config, snippets)
}
return snippets
}
// scanSnippets scans for any new/removed snippets and adds them to snippets.json
func scanSnippets(config Config, snippets []Snippet) []Snippet {
var modified bool
snippetExists := func(path string) bool {
for _, snippet := range snippets {
if path == snippet.Path() {
return true
}
}
return false
}
homeEntries, err := os.ReadDir(config.Home)
if err != nil {
fmt.Printf("could not scan config home: %v\n", err)
return snippets
}
for _, homeEntry := range homeEntries {
if !homeEntry.IsDir() {
continue
}
if strings.HasPrefix(homeEntry.Name(), ".") {
continue
}
folderPath := filepath.Join(config.Home, homeEntry.Name())
folderEntries, err := os.ReadDir(folderPath)
if err != nil {
fmt.Printf("could not scan %q: %v\n", folderPath, err)
continue
}
for _, folderEntry := range folderEntries {
if folderEntry.IsDir() {
continue
}
snippetPath := filepath.Join(homeEntry.Name(), folderEntry.Name())
if !snippetExists(snippetPath) {
name := folderEntry.Name()
ext := filepath.Ext(name)
snippets = append(snippets, Snippet{
Folder: homeEntry.Name(),
Date: time.Now(),
Name: strings.TrimSuffix(name, ext),
File: name,
Language: strings.TrimPrefix(ext, "."),
Tags: make([]string, 0),
})
modified = true
}
}
}
var idx int
for _, snippet := range snippets {
snippetPath := filepath.Join(config.Home, snippet.Path())
if _, err := os.Stat(snippetPath); !errors.Is(err, fs.ErrNotExist) {
snippets[idx] = snippet
idx++
modified = true
}
}
snippets = snippets[:idx]
if modified {
writeSnippets(config, snippets)
}
return snippets
}
func saveSnippet(content string, args []string, config Config, snippets []Snippet) {
// Save snippet to location
name := defaultSnippetName
if len(args) > 0 {
name = strings.Join(args, " ")
}
folder, name, language := parseName(name)
file := fmt.Sprintf("%s.%s", name, language)
filePath := filepath.Join(config.Home, folder, file)
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
fmt.Println("unable to create folder")
return
}
err := os.WriteFile(filePath, []byte(content), 0o644)
if err != nil {
fmt.Println("unable to create snippet")
return
}
// Add snippet metadata
snippet := Snippet{
Folder: folder,
Date: time.Now(),
Name: name,
File: file,
Language: language,
}
snippets = append([]Snippet{snippet}, snippets...)
writeSnippets(config, snippets)
}
func writeSnippets(config Config, snippets []Snippet) {
b, err := json.Marshal(snippets)
if err != nil {
fmt.Println("Could not marshal latest snippet data.", err)
return
}
err = os.WriteFile(filepath.Join(config.Home, config.File), b, os.ModePerm)
if err != nil {
fmt.Println("Could not save snippets file.", err)
}
}
func listSnippets(snippets []Snippet) {
for _, snippet := range snippets {
fmt.Println(snippet)
}
}
func findSnippet(search string, snippets []Snippet) Snippet {
matches := fuzzy.FindFrom(search, Snippets{snippets})
if len(matches) > 0 {
return snippets[matches[0].Index]
}
return Snippet{}
}
func runInteractiveMode(config Config, snippets []Snippet) error {
if len(snippets) == 0 {
// welcome to nap!
snippets = append(snippets, defaultSnippet)
}
state := readState()
folders := make(map[Folder][]list.Item)
for _, snippet := range snippets {
folders[Folder(snippet.Folder)] = append(folders[Folder(snippet.Folder)], list.Item(snippet))
}
defaultStyles := DefaultStyles(config)
var folderItems []list.Item
foldersSlice := maps.Keys(folders)
slices.Sort(foldersSlice)
for _, folder := range foldersSlice {
folderItems = append(folderItems, list.Item(folder))
}
if len(folderItems) <= 0 {
folderItems = append(folderItems, list.Item(Folder(defaultSnippetFolder)))
}
folderList := list.New(folderItems, folderDelegate{defaultStyles.Folders.Blurred}, 0, 0)
folderList.Title = "Folders"
folderList.SetShowHelp(false)
folderList.SetFilteringEnabled(false)
folderList.SetShowStatusBar(false)
folderList.DisableQuitKeybindings()
folderList.Styles.NoItems = lipgloss.NewStyle().Margin(0, 2).Foreground(lipgloss.Color(config.GrayColor))
folderList.SetStatusBarItemName("folder", "folders")
for idx, folder := range foldersSlice {
if string(folder) == state.CurrentFolder {
folderList.Select(idx)
break
}
}
content := viewport.New(80, 0)
lists := map[Folder]*list.Model{}
currentFolder := folderList.SelectedItem().(Folder)
for folder, items := range folders {
snippetList := newList(items, 20, defaultStyles.Snippets.Focused)
if folder == currentFolder {
for idx, item := range snippetList.Items() {
if s, ok := item.(Snippet); ok && s.File == state.CurrentSnippet {
snippetList.Select(idx)
break
}
}
}
lists[folder] = snippetList
}
m := &Model{
Lists: lists,
Folders: folderList,
Code: content,
ContentStyle: defaultStyles.Content.Blurred,
ListStyle: defaultStyles.Snippets.Focused,
FoldersStyle: defaultStyles.Folders.Blurred,
keys: DefaultKeyMap,
help: help.New(),
config: config,
inputs: []textinput.Model{
newTextInput(defaultSnippetFolder + " "),
newTextInput(defaultSnippetName + " "),
newTextInput(config.DefaultLanguage),
},
tagsInput: newTextInput("Tags"),
}
p := tea.NewProgram(m, tea.WithAltScreen())
model, err := p.Run()
if err != nil {
return err
}
fm, ok := model.(*Model)
if !ok {
return err
}
var allSnippets []list.Item
for _, list := range fm.Lists {
allSnippets = append(allSnippets, list.Items()...)
}
b, err := json.Marshal(allSnippets)
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(config.Home, config.File), b, os.ModePerm)
if err != nil {
return err
}
return nil
}
func newList(items []list.Item, height int, styles SnippetsBaseStyle) *list.Model {
snippetList := list.New(items, snippetDelegate{styles, navigatingState}, 25, height)
snippetList.SetShowHelp(false)
snippetList.SetShowFilter(false)
snippetList.SetShowTitle(false)
snippetList.Styles.StatusBar = lipgloss.NewStyle().Margin(1, 2).Foreground(lipgloss.Color("240")).MaxWidth(35 - 2)
snippetList.Styles.NoItems = lipgloss.NewStyle().Margin(0, 2).Foreground(lipgloss.Color("8")).MaxWidth(35 - 2)
snippetList.FilterInput.Prompt = "Find: "
snippetList.FilterInput.PromptStyle = styles.Title
snippetList.SetStatusBarItemName("snippet", "snippets")
snippetList.DisableQuitKeybindings()
snippetList.Styles.Title = styles.Title
snippetList.Styles.TitleBar = styles.TitleBar
return &snippetList
}
func newTextInput(placeholder string) textinput.Model {
i := textinput.New()
i.Prompt = ""
i.PromptStyle = lipgloss.NewStyle().Margin(0, 1)
i.Placeholder = placeholder
return i
}