-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
143 lines (125 loc) · 3.2 KB
/
list.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
package main
import (
"github.com/erfanio/1pass/ui"
"strings"
)
var (
unfiltered []partialItem
filtered []partialItem
detailsItem completeItem
)
func setupList() {
ui.App.Search.SetTextChanged(filter)
ui.App.Search.SetListDataProviders(listData, listCount, fetchAndCopy, fetchAndOpen)
ui.App.Details.SetDataProvider(detailsData)
}
func populateList(items []partialItem) {
unfiltered = items
ui.App.Search.EnableAndFocus()
}
// filter will search the list of items for matches that show up in the list
func filter(text string) {
ui.App.Search.ListDataWillChange()
filtered = make([]partialItem, 0, len(unfiltered))
// don't bother filtering if text is empty, just hide the list
if text != "" {
// filter items
target := strings.ToLower(text)
for _, item := range unfiltered {
if strings.Contains(strings.ToLower(item.Overview.Title), target) ||
strings.Contains(strings.ToLower(item.Overview.URL), target) ||
strings.Contains(strings.ToLower(item.Overview.AdditionalInfo), target) {
filtered = append(filtered, item)
}
}
}
ui.App.Search.ListDataDidChange()
ui.App.Search.UpdateSize()
}
// listData will return the data for a row and role
func listData(row, role int) string {
if row >= len(filtered) {
return ""
}
if role == ui.TitleRole {
return filtered[row].Overview.Title
}
if role == ui.SubtitleRole {
return filtered[row].Overview.AdditionalInfo
}
return ""
}
// listCount will return the number of rows needed to display items
func listCount() int {
return len(filtered)
}
// fetchAndCopy
func fetchAndCopy(row int) {
fetchDetails(filtered[row].UUID, func(item completeItem) {
for _, f := range item.Details.Fields {
if f.Name == "password" {
ui.App.Search.Copy(f.Value)
return
}
}
})
}
// detailsData will supply data to details page
func detailsData() ui.DetailsItem {
// maybe set later
username := ""
password := ""
sections := make([]ui.DetailsSection, 0)
detailsFields := make([]ui.DetailsField, 0)
// details fields
for _, f := range detailsItem.Details.Fields {
if f.Name == "username" {
username = f.Value
} else if f.Name == "password" {
password = f.Value
} else {
detailsFields = append(detailsFields, ui.DetailsField{
Title: f.Name,
Value: f.Value,
Hidden: false,
})
}
}
// all fields in all sections
for _, s := range detailsItem.Details.Sections {
fields := make([]ui.DetailsField, 0)
for _, f := range s.Fields {
hidden := false
if f.Type == "concealed" {
hidden = true
}
fields = append(fields, ui.DetailsField{
Title: f.Title,
Value: f.Value,
Hidden: hidden,
})
}
sections = append(sections, ui.DetailsSection{
Title: s.Title,
Fields: fields,
})
}
return ui.DetailsItem{
Title: detailsItem.Overview.Title,
URL: detailsItem.Overview.URL,
Notes: detailsItem.Details.Notes,
Username: username,
Password: password,
Fields: detailsFields,
Sections: sections,
RawJSON: detailsItem.RawJSON,
}
}
// fetchAndOpen
func fetchAndOpen(row int) {
fetchDetails(filtered[row].UUID, func(item completeItem) {
// after start, Details will get the data from detailsData
detailsItem = item
ui.App.Details.Start(item.Overview.Title)
})
}