-
Notifications
You must be signed in to change notification settings - Fork 4
/
browse.go
207 lines (170 loc) · 4.38 KB
/
browse.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
package main
import (
"fmt"
"github.com/tobischo/gokeepasslib/v3"
"github.com/atotto/clipboard"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
)
type viewMode string
var (
viewModeList viewMode = "list"
viewModeEntry viewMode = "entry"
)
func browseCmd(_ *cobra.Command, _ []string) error {
p := tea.NewProgram(initiateModel(&db.Content.Root.Groups[0]))
if _, err := p.Run(); err != nil {
return fmt.Errorf("Encountered error: %w", err)
}
return nil
}
type groupCursor struct {
group *gokeepasslib.Group
previous *groupCursor
}
type model struct {
mode viewMode
groupCursor *groupCursor
choices []interface{} // items on the to-do list
cursor int // which to-do list item our cursor is pointing at
}
func initiateModel(group *gokeepasslib.Group) model {
return model{
mode: viewModeList,
groupCursor: &groupCursor{
group: group,
},
// Our shopping list is a grocery list
choices: prepareChoices(group),
}
}
func prepareChoices(group *gokeepasslib.Group) []interface{} {
choices := []interface{}{}
for _, group := range group.Groups {
choices = append(choices, group)
}
for _, entry := range group.Entries {
choices = append(choices, entry)
}
return choices
}
func (m model) Init() tea.Cmd {
// Just return `nil`, which means "no I/O right now, please."
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
// Is it a key press?
case tea.KeyMsg:
// Cool, what was the actual key pressed?
switch msg.String() {
// These keys should exit the program.
case "ctrl+c", "q":
return m, tea.Quit
// The "up" and "k" keys move the cursor up
case "up", "k":
if m.mode == viewModeList {
if m.cursor > 0 {
m.cursor--
}
}
// The "down" and "j" keys move the cursor down
case "down", "j":
if m.mode == viewModeList {
if m.cursor < len(m.choices)-1 {
m.cursor++
}
}
case "left", "h":
if m.mode == viewModeList {
if m.groupCursor.previous != nil {
m.groupCursor = m.groupCursor.previous
m.choices = prepareChoices(m.groupCursor.group)
m.cursor = 0
}
} else {
m.mode = viewModeList
}
case "right", "l":
choice := m.choices[m.cursor]
switch t := choice.(type) {
case gokeepasslib.Group:
// go further to the right
m.groupCursor = &groupCursor{
group: &t,
previous: m.groupCursor,
}
m.choices = prepareChoices(&t)
m.cursor = 0
case gokeepasslib.Entry:
m.mode = viewModeEntry
}
case "enter", " ":
if m.mode == viewModeEntry {
entry, ok := m.choices[m.cursor].(gokeepasslib.Entry)
if !ok {
// Do nothing for now
break
}
// Ignore error here for now
_ = clipboard.WriteAll(entry.GetPassword())
}
}
}
// Return the updated model to the Bubble Tea runtime for processing.
// Note that we're not returning a command.
return m, nil
}
func (m model) View() string {
s := ""
switch m.mode {
case viewModeList:
s += m.viewList()
case viewModeEntry:
s += m.viewEntry()
}
// The footer
s += "\nPress q to quit.\n"
return s
}
func (m model) viewList() string {
// The header
s := "Pick an entry\n\n"
// Iterate over our choices
for i, choice := range m.choices {
// Is the cursor pointing at this choice?
cursor := " " // no cursor
if m.cursor == i {
cursor = ">" // cursor!
}
// Render the row
switch t := choice.(type) {
case gokeepasslib.Group:
s += fmt.Sprintf("%s %s [>]\n", cursor, t.Name)
case gokeepasslib.Entry:
s += fmt.Sprintf("%s %s\n", cursor, t.GetTitle())
default:
s += fmt.Sprintf("unsupported type %T\n", t)
}
}
// Send the UI for rendering
return s
}
func (m model) viewEntry() string {
entry, ok := m.choices[m.cursor].(gokeepasslib.Entry)
if !ok {
return "fatal: choice is not entry"
}
s := "Entry\n\n"
s += fmt.Sprintf("Title: %s\n", entry.GetTitle())
s += fmt.Sprintf("Creation: %s\n", entry.Times.CreationTime.Time.Format(timeFormat))
s += fmt.Sprintf(
"Last Modification: %s\n",
entry.Times.LastModificationTime.Time.Format(timeFormat),
)
s += fmt.Sprintf("Last Access: %s\n", entry.Times.LastAccessTime.Time.Format(timeFormat))
s += fmt.Sprintf("UserName: %s\n", entry.GetContent("UserName"))
s += fmt.Sprintf("URL: %s\n", entry.GetContent("URL"))
s += fmt.Sprintf("Notes:\n%s\n", entry.GetContent("Notes"))
return s
}