/
print.go
232 lines (200 loc) · 7.92 KB
/
print.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
package cmd
import (
"fmt"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/spf13/cobra"
"log"
"os"
"sort"
"strconv"
"strings"
)
var (
ids []int
notebookTitles []string
tags []string
printAll bool
printCmd = &cobra.Command{
Use: "print",
Short: "Print notes",
Long: "There are 4 ways to print a set of notes" +
" 1) Give a comma separated list of note ids" +
" 2) Give a comma separated list of notebook titles" +
" 3) Give a comma separated list of tags," +
" 4) If -a or --all flag is set all notes will be printed" +
"Press Esc to exit print mode",
Example: "print -i 1,2,... -n notebook1,notebook2,... -t tag1,tag2,... ",
Run: func(cmd *cobra.Command, args []string) {
ids, _ = cmd.Flags().GetIntSlice("ids")
notebookTitles, _ = cmd.Flags().GetStringSlice("notebook")
tags, _ = cmd.Flags().GetStringSlice("tags")
printAll, _ = cmd.Flags().GetBool("all")
jNotes, err := collectNotes(ids, notebookTitles, tags, printAll)
if err != nil {
log.Fatalln(err)
}
printNotes2Terminal(jNotes)
},
}
)
func init() {
rootCmd.AddCommand(printCmd)
printCmd.Flags().IntSliceP("ids", "i", []int{}, "Comma separated list of note ids.")
printCmd.Flags().StringSlice("tags", []string{}, "Comma-separated tags of note.")
printCmd.Flags().StringSliceP("notebook", "n", []string{}, "Comma separated list of notebook titles")
printCmd.Flags().BoolP("all", "a", false, "Print all notes")
}
func collectNotes(ids []int, notebookTitles []string, tags []string, printAll bool) ([]*jsonNote, error) {
notes, err := collectNotesFromDB(ids, notebookTitles, tags, printAll)
if err != nil {
return nil, err
}
jNotes, err := transformNotes2JSONNotes(noteMap2Slice(notes))
if err != nil {
return nil, err
}
return jNotes, nil
}
func printNotes2Terminal(jNotes []*jsonNote) {
uiApp := createUI(jNotes)
if uiApp == nil {
fmt.Println("No notes to display")
os.Exit(0)
}
if err := uiApp.Run(); err != nil {
log.Fatal(err)
}
}
func createUI(jNotes []*jsonNote) *tview.Application {
app := tview.NewApplication()
if len(jNotes) == 0 {
return nil
}
//Sort By date (descending)
sort.Slice(jNotes, func(i, j int) bool {
return jNotes[i].LastUpdated.After(jNotes[j].LastUpdated)
})
notesTable := constructNotesTable(jNotes)
notesFlex := tview.NewFlex()
notesFlex.SetDirection(tview.FlexRow)
numberOfVisibleRows := 5
notesFlex.AddItem(notesTable, numberOfVisibleRows, 1, true)
memo := constructMemo(jNotes)
dates := constructDatesRow(jNotes)
help := constructHelpLine()
flexLayout := tview.NewFlex()
flexLayout.SetDirection(tview.FlexRow)
flexLayout.AddItem(notesFlex, numberOfVisibleRows, 1, true)
flexLayout.AddItem(memo, 0, 3, false)
flexLayout.AddItem(dates, 1, 1, false)
flexLayout.AddItem(help, 1, 1, false)
pages := tview.NewPages()
pages.AddPage("notes", flexLayout, true, true)
notesTable.SetSelectionChangedFunc(func(row, column int) {
if row >= 0 {
selectedNote := jNotes[row-1]
memo.SetText(selectedNote.Memo)
dates.SetCell(0, 1, &tview.TableCell{Text: selectedNote.Created.Format("Jan 2 2006 15:04"), Align: tview.AlignLeft, Color: tcell.ColorDefault, Expansion: 2})
dates.SetCell(0, 3, &tview.TableCell{Text: selectedNote.LastUpdated.Format("Jan 2 2006 15:04"), Align: tview.AlignLeft, Color: tcell.ColorDefault, Expansion: 2})
}
})
app.SetRoot(pages, true)
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlD:
row, _ := notesTable.GetSelection()
if notesTable.GetRowCount() > 1 {
noteIndex := row - 1
toBeDelete := jNotes[noteIndex]
jNotes = append(jNotes[:noteIndex], jNotes[noteIndex+1:]...)
delete([]int64{toBeDelete.ID})
notesFlex.RemoveItem(notesTable)
notesTable = constructNotesTable(jNotes)
notesFlex.AddItem(notesTable, numberOfVisibleRows, 1, true)
}
case tcell.KeyCtrlU:
row, _ := notesTable.GetSelection()
if notesTable.GetRowCount() > 1 {
noteIndex := row - 1
toBeUpdated := jNotes[noteIndex]
updateForm := constructUpdateForm(toBeUpdated)
updateForm.AddButton("Continue", func() {
notebookTitleInputField := updateForm.GetFormItem(0).(*tview.InputField)
noteTitleInputField := updateForm.GetFormItem(1).(*tview.InputField)
tagsInputField := updateForm.GetFormItem(2).(*tview.InputField)
notebookTitle := notebookTitleInputField.GetText()
noteTitle := noteTitleInputField.GetText()
tagsStr := tagsInputField.GetText()
tags := strings.Split(tagsStr, ",")
app.Suspend(func() {
editor := &viEditor{}
update(toBeUpdated.ID, noteTitle, tags, notebookTitle, editor)
})
app.Stop()
updatedJNotes, _ := collectNotes(ids, notebookTitles, tags, printAll)
printNotes2Terminal(updatedJNotes)
})
updateForm.AddButton("Cancel", func() {
pages.SwitchToPage("notes")
})
pages.AddAndSwitchToPage("update", updateForm, true)
}
}
return event
})
return app
}
func constructNotesTable(jNotes []*jsonNote) *tview.Table {
notesTable := tview.NewTable()
notesTable.SetFixed(1, 0)
notesTable.SetSelectable(true, false)
//set header
idCell := &tview.TableCell{Text: "ID", Align: tview.AlignLeft, Color: tcell.ColorBlue, Expansion: 1, NotSelectable: true}
notesTable.SetCell(0, 0, idCell)
notebookTitleCell := &tview.TableCell{Text: "Notebook Title", Align: tview.AlignLeft, Color: tcell.ColorBlue, Expansion: 2, NotSelectable: true}
notesTable.SetCell(0, 1, notebookTitleCell)
noteTitleCell := &tview.TableCell{Text: "Note title", Align: tview.AlignLeft, Color: tcell.ColorBlue, Expansion: 2, NotSelectable: true}
notesTable.SetCell(0, 2, noteTitleCell)
tagsCell := &tview.TableCell{Text: "Tags", Align: tview.AlignLeft, Color: tcell.ColorBlue, Expansion: 2, NotSelectable: true}
notesTable.SetCell(0, 3, tagsCell)
for row := 0; row < len(jNotes); row++ {
notesTable.SetCell(row+1, 0, &tview.TableCell{Text: strconv.FormatInt(jNotes[row].ID, 10), Align: tview.AlignLeft, Color: tcell.ColorLimeGreen, Expansion: 1})
notesTable.SetCell(row+1, 1, &tview.TableCell{Text: jNotes[row].NotebookTitle, Align: tview.AlignLeft, Color: tcell.ColorLimeGreen, Expansion: 2})
notesTable.SetCell(row+1, 2, &tview.TableCell{Text: jNotes[row].Title, Align: tview.AlignLeft, Color: tcell.ColorLimeGreen, Expansion: 2})
notesTable.SetCell(row+1, 3, &tview.TableCell{Text: strings.Join(jNotes[row].Tags, ","), Align: tview.AlignLeft, Color: tcell.ColorLimeGreen, Expansion: 2})
}
return notesTable
}
func constructMemo(jNotes []*jsonNote) *tview.TextView {
memo := tview.NewTextView()
memo.SetBorder(true)
memo.SetWordWrap(true)
memo.SetBorderPadding(0, 0, 0, 0)
memo.SetTextAlign(tview.AlignLeft)
memo.SetText(jNotes[0].Memo)
return memo
}
func constructDatesRow(jNotes []*jsonNote) *tview.Table {
dates := tview.NewTable()
dates.SetSelectable(false, false)
dates.SetCell(0, 0, &tview.TableCell{Text: "Created: ", Align: tview.AlignLeft, Color: tcell.ColorDefault, Expansion: 1})
dates.SetCell(0, 1, &tview.TableCell{Text: jNotes[0].Created.Format("Jan 2 2006 15:04"), Align: tview.AlignLeft, Color: tcell.ColorDefault, Expansion: 1})
dates.SetCell(0, 2, &tview.TableCell{Text: "Updated: ", Align: tview.AlignLeft, Color: tcell.ColorDefault, Expansion: 1})
dates.SetCell(0, 3, &tview.TableCell{Text: jNotes[0].LastUpdated.Format("Jan 2 2006 15:04"), Align: tview.AlignLeft, Color: tcell.ColorDefault, Expansion: 1})
return dates
}
func constructHelpLine() *tview.TextView {
help := tview.NewTextView()
help.SetText("Press Ctrl+C to espace, Ctrl+D to delete and Ctrl+U to update a note")
help.SetTextAlign(tview.AlignCenter)
help.SetTextColor(tcell.ColorRed)
return help
}
func constructUpdateForm(jNote *jsonNote) *tview.Form {
form := tview.NewForm()
form.AddInputField("Notebook Title:", jNote.NotebookTitle, 30, nil, nil)
form.AddInputField("Note Title:", jNote.Title, 30, nil, nil)
form.AddInputField("Tags:", strings.Join(jNote.Tags, ","), 30, nil, nil)
return form
}