-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
model.go
693 lines (625 loc) · 18.7 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
package main
import (
"bytes"
"fmt"
"math/rand"
"os"
"path/filepath"
"strings"
"time"
"github.com/alecthomas/chroma/v2/quick"
"github.com/atotto/clipboard"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
const maxPane = 3
type pane int
const (
snippetPane pane = iota
contentPane
folderPane
)
type state int
const (
navigatingState state = iota
deletingState
creatingState
copyingState
pastingState
quittingState
editingState
editingTagsState
)
type input int
const (
folderInput input = iota
nameInput
languageInput
)
// Model represents the state of the application.
// It contains all the snippets organized in folders.
type Model struct {
// the config map.
config Config
// the key map.
keys KeyMap
// the help model.
help help.Model
// the height of the terminal.
height int
// the working directory.
Workdir string
// the List of snippets to display to the user.
Lists map[Folder]*list.Model
// the list of Folders to display to the user.
Folders list.Model
// the viewport of the Code snippet.
Code viewport.Model
LineNumbers viewport.Model
// the input for snippet folder, name, language
activeInput input
inputs []textinput.Model
tagsInput textinput.Model
// the current active pane of focus.
pane pane
// the current state / action of the application.
state state
// stying for components
ListStyle SnippetsBaseStyle
FoldersStyle FoldersBaseStyle
ContentStyle ContentBaseStyle
}
// Init initialzes the application model.
func (m *Model) Init() tea.Cmd {
rand.Seed(time.Now().Unix())
m.Folders.Styles.Title = m.FoldersStyle.Title
m.Folders.Styles.TitleBar = m.FoldersStyle.TitleBar
m.updateKeyMap()
return func() tea.Msg {
return updateContentMsg(m.selectedSnippet())
}
}
// updateContentMsg tells the application to update the content view with the
// given snippet.
type updateContentMsg Snippet
// updateContent instructs the application to fetch the latest contents of the
// snippet file.
//
// This is useful after a Paste or Edit.
func (m *Model) updateContent() tea.Cmd {
return func() tea.Msg {
return updateContentMsg(m.selectedSnippet())
}
}
type updateFoldersMsg struct {
items []list.Item
selectedFolderIndex int
}
// updateFolders returns a Cmd to tell the application that there are possible
// folder changes to update.
func (m *Model) updateFolders() tea.Cmd {
return func() tea.Msg {
msg := m.updateFoldersView()
return msg
}
}
// changeStateMsg tells the application to enter a different state.
type changeStateMsg struct{ newState state }
// changeState returns a Cmd to enter a different state.
func changeState(newState state) tea.Cmd {
return func() tea.Msg {
return changeStateMsg{newState}
}
}
// Update updates the model based on user interaction.
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case updateFoldersMsg:
setItemsCmd := m.Folders.SetItems(msg.items)
m.Folders.Select(msg.selectedFolderIndex)
var cmd tea.Cmd
m.Folders, cmd = m.Folders.Update(msg)
return m, tea.Batch(setItemsCmd, cmd)
case updateContentMsg:
return m.updateContentView(msg)
case changeStateMsg:
m.List().SetDelegate(snippetDelegate{m.ListStyle, msg.newState})
var cmd tea.Cmd
if m.state == msg.newState {
break
}
wasEditing := m.state == editingState
wasPasting := m.state == pastingState
wasCreating := m.state == creatingState
m.state = msg.newState
m.updateKeyMap()
m.updateActivePane(msg)
switch msg.newState {
case navigatingState:
if wasPasting || wasCreating {
return m, m.updateContent()
}
if wasEditing {
m.blurInputs()
i := m.List().Index()
snippet := m.selectedSnippet()
if m.inputs[nameInput].Value() != "" {
snippet.Name = m.inputs[nameInput].Value()
} else {
snippet.Name = defaultSnippetName
}
if m.inputs[folderInput].Value() != "" {
snippet.Folder = m.inputs[folderInput].Value()
} else {
snippet.Folder = defaultSnippetFolder
}
if m.inputs[languageInput].Value() != "" {
snippet.Language = m.inputs[languageInput].Value()
} else {
snippet.Language = m.config.DefaultLanguage
}
file := fmt.Sprintf("%s.%s", snippet.Name, snippet.Language)
snippet.File = file
newPath := filepath.Join(m.config.Home, snippet.Path())
_ = os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
_ = os.Rename(m.selectedSnippetFilePath(), newPath)
setCmd := m.List().SetItem(i, snippet)
m.pane = snippetPane
cmd = tea.Batch(setCmd, m.updateFolders(), m.updateContent())
}
case pastingState:
content, err := clipboard.ReadAll()
if err != nil {
return m, changeState(navigatingState)
}
f, err := os.OpenFile(m.selectedSnippetFilePath(), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return m, changeState(navigatingState)
}
defer f.Close()
f.WriteString(content)
return m, changeState(navigatingState)
case deletingState:
m.state = deletingState
case editingState:
m.pane = contentPane
snippet := m.selectedSnippet()
m.inputs[folderInput].SetValue(snippet.Folder)
if snippet.Name == defaultSnippetName {
m.inputs[nameInput].SetValue("")
} else {
m.inputs[nameInput].SetValue(snippet.Name)
}
m.inputs[languageInput].SetValue(snippet.Language)
cmd = m.focusInput(m.activeInput)
case creatingState:
case copyingState:
m.pane = snippetPane
m.state = copyingState
m.updateActivePane(msg)
cmd = tea.Tick(time.Second, func(t time.Time) tea.Msg {
return changeStateMsg{navigatingState}
})
}
m.updateKeyMap()
m.updateActivePane(msg)
return m, cmd
case tea.WindowSizeMsg:
m.height = msg.Height - 4
for _, li := range m.Lists {
li.SetHeight(m.height)
}
m.Folders.SetHeight(m.height)
m.Code.Height = m.height
m.LineNumbers.Height = m.height
m.Code.Width = msg.Width - m.List().Width() - m.Folders.Width() - 20
m.LineNumbers.Width = 5
return m, nil
case tea.KeyMsg:
if m.List().FilterState() == list.Filtering {
break
}
if m.state == deletingState {
switch {
case key.Matches(msg, m.keys.Confirm):
_ = os.Remove(m.selectedSnippetFilePath())
m.List().RemoveItem(m.List().Index())
m.state = navigatingState
m.updateKeyMap()
return m, tea.Batch(changeState(navigatingState), func() tea.Msg {
return updateContentMsg(m.selectedSnippet())
})
case key.Matches(msg, m.keys.Quit, m.keys.Cancel):
return m, changeState(navigatingState)
}
return m, nil
} else if m.state == copyingState {
return m, changeState(navigatingState)
} else if m.state == editingState {
if msg.String() == "esc" || msg.String() == "enter" {
return m, changeState(navigatingState)
}
var cmd tea.Cmd
var cmds []tea.Cmd
for i := range m.inputs {
m.inputs[i], cmd = m.inputs[i].Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
switch {
case key.Matches(msg, m.keys.NextPane):
m.nextPane()
case key.Matches(msg, m.keys.PreviousPane):
m.previousPane()
case key.Matches(msg, m.keys.Quit):
m.saveState()
m.state = quittingState
return m, tea.Quit
case key.Matches(msg, m.keys.NewSnippet):
m.state = creatingState
return m, m.createNewSnippetFile()
case key.Matches(msg, m.keys.MoveSnippetDown):
m.moveSnippetDown()
case key.Matches(msg, m.keys.MoveSnippetUp):
m.moveSnippetUp()
case key.Matches(msg, m.keys.PasteSnippet):
return m, changeState(pastingState)
case key.Matches(msg, m.keys.RenameSnippet):
m.activeInput = nameInput
return m, changeState(editingState)
case key.Matches(msg, m.keys.ChangeFolder):
m.pane = snippetPane
cmd := m.updateActivePane(msg)
return m, cmd
case key.Matches(msg, m.keys.ToggleHelp):
m.help.ShowAll = !m.help.ShowAll
var newHeight int
if m.help.ShowAll {
newHeight = m.height - 4
} else {
newHeight = m.height
}
m.List().SetHeight(newHeight)
m.Folders.SetHeight(newHeight)
m.Code.Height = newHeight
m.LineNumbers.Height = newHeight
case key.Matches(msg, m.keys.SetFolder):
m.activeInput = folderInput
return m, changeState(editingState)
case key.Matches(msg, m.keys.SetLanguage):
m.activeInput = languageInput
return m, changeState(editingState)
case key.Matches(msg, m.keys.CopySnippet):
return m, func() tea.Msg {
content, err := os.ReadFile(m.selectedSnippetFilePath())
if err != nil {
return changeStateMsg{navigatingState}
}
clipboard.WriteAll(string(content))
return changeStateMsg{copyingState}
}
case key.Matches(msg, m.keys.DeleteSnippet):
m.pane = snippetPane
m.updateActivePane(msg)
m.List().Title = "Delete? (y/N)"
return m, changeState(deletingState)
case key.Matches(msg, m.keys.EditSnippet):
return m, m.editSnippet()
case key.Matches(msg, m.keys.Search):
m.pane = snippetPane
}
}
m.updateKeyMap()
cmd := m.updateActivePane(msg)
return m, cmd
}
// blurInputs blurs all the inputs.
func (m *Model) blurInputs() {
for i := range m.inputs {
m.inputs[i].Blur()
}
}
// focusInput focuses the speficied input and blurs the rest.
func (m *Model) focusInput(i input) tea.Cmd {
m.blurInputs()
m.inputs[i].CursorEnd()
return m.inputs[i].Focus()
}
// selectedSnippetFilePath returns the file path of the snippet that is
// currently selected.
func (m *Model) selectedSnippetFilePath() string {
return filepath.Join(m.config.Home, m.selectedSnippet().Path())
}
// nextPane sets the next pane to be active.
func (m *Model) nextPane() {
m.pane = (m.pane + 1) % maxPane
}
// previousPane sets the previous pane to be active.
func (m *Model) previousPane() {
m.pane--
if m.pane < 0 {
m.pane = maxPane - 1
}
}
// editSnippet opens the editor with the selected snippet file path.
func (m *Model) editSnippet() tea.Cmd {
return tea.ExecProcess(editorCmd(m.selectedSnippetFilePath()), func(err error) tea.Msg {
return updateContentMsg(m.selectedSnippet())
})
}
func (m *Model) noContentHints() []keyHint {
return []keyHint{
{m.keys.EditSnippet, "edit contents"},
{m.keys.PasteSnippet, "paste clipboard"},
{m.keys.RenameSnippet, "rename"},
{m.keys.SetFolder, "set folder"},
{m.keys.SetLanguage, "set language"},
}
}
// updateFolderView updates the folders list to display the current folders.
func (m *Model) updateFoldersView() tea.Msg {
var selectedFolder Folder
selectedFolderIndex := m.Folders.Index()
for folder, li := range m.Lists {
for i, item := range li.Items() {
snippet, ok := item.(Snippet)
if !ok {
continue
}
f := Folder(snippet.Folder)
_, ok = m.Lists[f]
if !ok {
m.Lists[f] = newList([]list.Item{}, m.height, m.ListStyle)
selectedFolder = f
}
if f != folder {
li.RemoveItem(i)
m.Lists[f].InsertItem(0, item)
selectedFolder = f
}
}
}
var folderItems []list.Item
foldersSlice := maps.Keys(m.Lists)
slices.Sort(foldersSlice)
for i, folder := range foldersSlice {
folderItems = append(folderItems, Folder(folder))
if folder == selectedFolder {
selectedFolderIndex = i
}
}
return updateFoldersMsg{
items: folderItems,
selectedFolderIndex: selectedFolderIndex,
}
}
// updateContentView updates the content view with the correct content based on
// the active snippet or display the appropriate error message / hint message.
func (m *Model) updateContentView(msg updateContentMsg) (tea.Model, tea.Cmd) {
if len(m.List().Items()) <= 0 {
m.displayKeyHint([]keyHint{
{m.keys.NewSnippet, "create a new snippet."},
})
return m, nil
}
var b bytes.Buffer
content, err := os.ReadFile(filepath.Join(m.config.Home, Snippet(msg).Path()))
if err != nil {
m.displayKeyHint(m.noContentHints())
return m, nil
}
if string(content) == "" {
m.displayKeyHint(m.noContentHints())
return m, nil
}
// b.WriteString(string(content))
err = quick.Highlight(&b, string(content), msg.Language, "terminal16m", m.config.Theme)
if err != nil {
m.displayError("Unable to highlight file.")
return m, nil
}
s := b.String()
m.writeLineNumbers(lipgloss.Height(s))
m.Code.SetContent(s)
return m, nil
}
type keyHint struct {
binding key.Binding
help string
}
// displayKeyHint updates the content viewport with instructions on the
// relevent key binding that the user should most likely press.
func (m *Model) displayKeyHint(hints []keyHint) {
m.LineNumbers.SetContent(strings.Repeat(" ~ \n", len(hints)))
var s strings.Builder
for _, hint := range hints {
s.WriteString(
fmt.Sprintf("%s %s\n",
m.ContentStyle.EmptyHintKey.Render(hint.binding.Help().Key),
m.ContentStyle.EmptyHint.Render("• "+hint.help),
))
}
m.Code.SetContent(s.String())
}
// displayError updates the content viewport with the error message provided.
func (m *Model) displayError(error string) {
m.LineNumbers.SetContent(" ~ ")
m.Code.SetContent(fmt.Sprintf("%s",
m.ContentStyle.EmptyHint.Render(error),
))
}
// writeLineNumbers writes the number of line numbers to the line number
// viewport.
func (m *Model) writeLineNumbers(n int) {
var lineNumbers strings.Builder
for i := 1; i < n; i++ {
lineNumbers.WriteString(fmt.Sprintf("%3d \n", i))
}
m.LineNumbers.SetContent(lineNumbers.String() + " ~ \n")
}
const tabSpaces = 4
// updateActivePane updates the currently active pane.
func (m *Model) updateActivePane(msg tea.Msg) tea.Cmd {
var cmds []tea.Cmd
var cmd tea.Cmd
switch m.pane {
case folderPane:
m.ListStyle = DefaultStyles(m.config).Snippets.Blurred
m.ContentStyle = DefaultStyles(m.config).Content.Blurred
m.FoldersStyle = DefaultStyles(m.config).Folders.Focused
m.Folders, cmd = m.Folders.Update(msg)
m.updateKeyMap()
cmds = append(cmds, cmd, m.updateContent())
case snippetPane:
m.ListStyle = DefaultStyles(m.config).Snippets.Focused
m.ContentStyle = DefaultStyles(m.config).Content.Blurred
m.FoldersStyle = DefaultStyles(m.config).Folders.Blurred
*m.List(), cmd = (*m.List()).Update(msg)
cmds = append(cmds, cmd)
case contentPane:
m.ListStyle = DefaultStyles(m.config).Snippets.Blurred
m.ContentStyle = DefaultStyles(m.config).Content.Focused
m.FoldersStyle = DefaultStyles(m.config).Folders.Blurred
m.Code, cmd = m.Code.Update(msg)
cmds = append(cmds, cmd)
m.LineNumbers, cmd = m.LineNumbers.Update(msg)
cmds = append(cmds, cmd)
}
m.List().SetDelegate(snippetDelegate{m.ListStyle, m.state})
m.Folders.SetDelegate(folderDelegate{m.FoldersStyle})
m.Folders.Styles.TitleBar = m.FoldersStyle.TitleBar
m.Folders.Styles.Title = m.FoldersStyle.Title
return tea.Batch(cmds...)
}
// updateKeyMap disables or enables the keys based on the current state of the
// snippet list.
func (m *Model) updateKeyMap() {
hasItems := len(m.List().VisibleItems()) > 0
isFiltering := m.List().FilterState() == list.Filtering
isEditing := m.state == editingState
m.keys.DeleteSnippet.SetEnabled(hasItems && !isFiltering && !isEditing)
m.keys.CopySnippet.SetEnabled(hasItems && !isFiltering && !isEditing)
m.keys.PasteSnippet.SetEnabled(hasItems && !isFiltering && !isEditing)
m.keys.EditSnippet.SetEnabled(hasItems && !isFiltering && !isEditing)
m.keys.NewSnippet.SetEnabled(!isFiltering && !isEditing)
m.keys.ChangeFolder.SetEnabled(m.pane == folderPane)
}
// selectedSnippet returns the currently selected snippet.
func (m *Model) selectedSnippet() Snippet {
item := m.List().SelectedItem()
if item == nil {
return defaultSnippet
}
return item.(Snippet)
}
// selected folder returns the currently selected folder.
func (m *Model) selectedFolder() Folder {
item := m.Folders.SelectedItem()
if item == nil {
return "misc"
}
return item.(Folder)
}
// List returns the active list.
func (m *Model) List() *list.Model {
return m.Lists[m.selectedFolder()]
}
func (m *Model) moveSnippetDown() {
currentPosition := m.List().Index()
currentItem := m.List().SelectedItem()
m.List().InsertItem(currentPosition+2, currentItem)
m.List().RemoveItem(currentPosition)
m.List().CursorDown()
}
func (m *Model) moveSnippetUp() {
currentPosition := m.List().Index()
currentItem := m.List().SelectedItem()
m.List().RemoveItem(currentPosition)
m.List().InsertItem(currentPosition-1, currentItem)
m.List().CursorUp()
}
// createNewSnippet creates a new snippet file and adds it to the the list.
func (m *Model) createNewSnippetFile() tea.Cmd {
return func() tea.Msg {
folder := defaultSnippetFolder
folderItem := m.Folders.SelectedItem()
if folderItem != nil && folderItem.FilterValue() != "" {
folder = folderItem.FilterValue()
}
file := fmt.Sprintf("snippet-%d.%s", rand.Intn(1000000), m.config.DefaultLanguage)
newSnippet := Snippet{
Name: defaultSnippetName,
Date: time.Now(),
File: file,
Language: m.config.DefaultLanguage,
Tags: []string{},
Folder: folder,
}
_, _ = os.Create(filepath.Join(m.config.Home, newSnippet.Path()))
m.List().InsertItem(m.List().Index(), newSnippet)
return changeStateMsg{navigatingState}
}
}
// View returns the view string for the application model.
func (m *Model) View() string {
if m.state == quittingState {
return ""
}
var (
folder = m.ContentStyle.Title.Render(m.selectedSnippet().Folder)
name = m.ContentStyle.Title.Render(m.selectedSnippet().Name)
language = m.ContentStyle.Title.Render(m.selectedSnippet().Language)
titleBar = m.ListStyle.TitleBar.Render("Snippets")
)
if m.state == editingState {
folder = m.inputs[folderInput].View()
name = m.inputs[nameInput].View()
language = m.inputs[languageInput].View()
} else if m.state == copyingState {
titleBar = m.ListStyle.CopiedTitleBar.Render("Copied Snippet!")
} else if m.state == deletingState {
titleBar = m.ListStyle.DeletedTitleBar.Render("Delete Snippet? (y/N)")
} else if m.List().SettingFilter() {
titleBar = m.ListStyle.TitleBar.Render(m.List().FilterInput.View())
}
return lipgloss.JoinVertical(
lipgloss.Top,
lipgloss.JoinHorizontal(
lipgloss.Left,
m.FoldersStyle.Base.Render(m.Folders.View()),
m.ListStyle.Base.Render(titleBar+m.List().View()),
lipgloss.JoinVertical(lipgloss.Top,
lipgloss.JoinHorizontal(lipgloss.Left,
folder,
m.ContentStyle.Separator.Render("/"),
name,
m.ContentStyle.Separator.Render("."),
language,
),
lipgloss.JoinHorizontal(lipgloss.Left,
m.ContentStyle.LineNumber.Render(m.LineNumbers.View()),
m.ContentStyle.Base.Render(strings.ReplaceAll(m.Code.View(), "\t", strings.Repeat(" ", tabSpaces))),
),
),
),
marginStyle.Render(m.help.View(m.keys)),
)
}
func (m *Model) saveState() {
s := State{
CurrentFolder: string(m.selectedFolder()),
CurrentSnippet: m.selectedSnippet().File,
}
err := s.Save()
if err != nil {
panic(err.Error())
}
}