-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.go
334 lines (291 loc) · 9 KB
/
format.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
package main
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/search/query"
aw "github.com/deanishe/awgo"
"github.com/olekukonko/tablewriter"
"golang.org/x/crypto/ssh/terminal"
)
func caseInsensitiveReplace(subject string, search string, replace string) string {
searchRegex := regexp.MustCompile("(?i)" + regexp.QuoteMeta(search))
pos := searchRegex.FindStringIndex(subject)
if pos != nil {
start, stop := pos[0], pos[1]
return subject[:start] + highlightText.Sprintf(subject[start:stop]) + subject[stop:len(subject)]
}
return subject
}
func highlightTerms(s string, terms []string) string {
for _, term := range terms {
s = caseInsensitiveReplace(s, term, highlightText.Sprint(term))
}
return s
}
func removeFields(header []string, tableData [][]string, omitFields []string) ([]string, [][]string) {
// Removes fields from the header and table
// Remove fields from header
n := 0
for _, field := range header {
if contains(omitFields, field) == false {
header[n] = field
n++
}
}
header = header[:n]
// Remove fields from table
for ridx := range tableData {
n = 0
for idx, field := range header {
if contains(omitFields, field) == false {
tableData[ridx][n] = tableData[ridx][idx]
n++
}
}
tableData[ridx] = tableData[ridx][:n]
}
return header, tableData
}
// Generate a result table
func resultTable(results *bleve.SearchResult, isQuery bool, highlightTermSet []string) {
/*
Format
*/
// Terminal Window size
var xsize, _, _ = terminal.GetSize(0)
var colWidth int
tableData := make([][]string, len(results.Hits))
for idx, gist := range results.Hits {
updatedAt := strings.Split(gist.Fields["UpdatedAt"].(string), "T")[0]
tableData[idx] = []string{
fmt.Sprintf("%v", gist.Fields["IDX"]),
ifelse(gist.Fields["Starred"].(string) == "T", "⭐", ""),
ifelse(gist.Fields["Public"].(string) == "F", "🔒", ""),
highlightTerms(fmt.Sprintf("%.60v", gist.Fields["Description"].(string)), highlightTermSet),
highlightTerms(fmt.Sprintf("%v", gist.Fields["Filename"]), highlightTermSet),
highlightTerms(fmt.Sprintf("%v", gist.Fields["Language"]), highlightTermSet),
highlightTerms(gist.Fields["Owner"].(string), highlightTermSet),
string(fmt.Sprintf("%v", gist.Fields["NLines"].(float64))),
updatedAt,
}
if isQuery {
tableData[idx] = append(tableData[idx], fmt.Sprintf("%1.3f", gist.Score))
}
}
// Render results
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
/*
Header
*/
var header = []string{"ID", "⭐", "🔒", "Description", "Filename", "Language", "Owner", "n", "Updated"}
if isQuery {
header = append(header, "Score")
}
colWidth = (xsize / len(header))
var omitFields []string
switch {
case between(xsize, 100, 125):
omitFields = []string{"Updated", "Owner"}
case between(xsize, 80, 100):
omitFields = []string{"Updated", "Owner", "Language"}
}
if xsize < 125 {
header, tableData = removeFields(header, tableData, omitFields)
}
table.SetAutoFormatHeaders(false)
table.SetHeader(header)
var headerColors []tablewriter.Colors
headerColors = make([]tablewriter.Colors, len(header))
for i := 0; i < len(header); i++ {
headerColors[i] = tablewriter.Colors{tablewriter.Bold}
}
table.SetHeaderColor(headerColors...)
table.SetHeaderLine(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetColWidth(colWidth)
table.SetColMinWidth(3, int(float32(colWidth)*2.5))
table.SetColumnSeparator("\t")
table.SetCenterSeparator("\t")
// Give Description 2x width
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.AppendBulk(tableData)
//table.SetTablePadding("")
table.SetAutoWrapText(false)
table.Render()
blueText.Printf("Showing %v Hit%s of %v Results\n", len(results.Hits), ifelse(results.Total != 1, "s", ""), results.Total)
}
/*
Summarize a field
*/
func fieldSummaryTable(field string, data [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetAutoFormatHeaders(false)
table.SetHeader([]string{field, "Count"})
table.SetHeaderColor(
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.Bold},
)
table.SetHeaderLine(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.AppendBulk(data)
table.SetColumnSeparator("\t")
table.SetCenterSeparator("\t")
table.Render()
}
func queryGistsAlfred(alfredQuery string) {
squery.limit = 100
squery.status = "all"
switch {
// Starred gists
case strings.HasPrefix(alfredQuery, "⭐"):
squery.starred = true
var subQuery = strings.SplitAfter(alfredQuery, " ")
if len(subQuery) > 0 {
squery.term = subQuery[0]
}
default:
squery.term = alfredQuery
}
ls(&squery)
}
func fieldSummaryAlfred(field string, data [][]string) {
var qPrefix string
var tagFmt string
var icon *aw.Icon
for _, row := range data {
switch {
case field == "Tags":
qPrefix = "#"
icon = tagIcon
case field == "Language":
qPrefix = "~"
icon = resolveIcon(row[0])
case field == "Owner":
qPrefix = ":"
icon = randomOwnerIcon()
}
tagFmt = qPrefix + strings.ToLower(row[0])
var subQuery = strings.SplitAfter(alfredQuery, " ")
// Need to split this logic out for other args...
var prefixMatch = strings.HasPrefix(tagFmt, strings.ToLower(alfredQuery)) || (tagFmt == strings.Trim(subQuery[0], " "))
var isMatched = (alfredQuery[len(alfredQuery)-1] == ' ') || (len(subQuery) > 1)
if (prefixMatch == true) && (isMatched == false) {
wf.NewItem(fmt.Sprintf("%s%v", qPrefix, row[0])).
Icon(icon).
Autocomplete(fmt.Sprintf("%s%v ", qPrefix, row[0])).
Subtitle(fmt.Sprintf("%v gists", row[1]))
} else if (prefixMatch == true) && (isMatched == true) {
// Query for results
squery.term = subQuery[1] // if empty (""); term does nothing
squery.status = "all"
squery.limit = 100
if qPrefix == "#" {
squery.tag = row[0]
} else if qPrefix == "~" {
squery.language = row[0]
} else if qPrefix == ":" {
squery.owner = row[0]
}
ls(&squery) // invokes resultListAlfred
}
}
}
func resultListAlfred(results *bleve.SearchResult) {
for _, gist := range results.Hits {
// Get Private and Starred States
isStarred := ifelse(gist.Fields["Starred"].(string) == "T", "⭐", "")
isPrivate := ifelse(gist.Fields["Public"].(string) == "F", "🔒", "")
statusText := ""
if len(isStarred) > 0 || len(isPrivate) > 0 {
statusText = fmt.Sprintf("%s%s| ", isStarred, isPrivate)
}
// Parse/format filenames
filenameText := ""
switch val := gist.Fields["Filename"].(type) {
case string:
filenameText = val
case []interface{}:
for _, s := range val {
filenameText += s.(string) + " "
}
}
filenameText = ifelse(filenameText != "", fmt.Sprintf("%s | ", filenameText), "")
// Get Gist Content
gistText := GistToText(gist)
subtitleText := fmt.Sprintf("%s%s%s", statusText, filenameText, truncateString(gistText, 100))
icon := resolveIcon(gist.Fields["Language"])
it := wf.NewItem(gist.Fields["Description"].(string)).
Icon(icon).
Quicklook(gist.Fields["URL"].(string)).
Copytext(gistText).
Subtitle(subtitleText).
Arg(gistText).
Var("title", fmt.Sprintf("'%s'", gist.Fields["Description"].(string))).
Valid(true)
it.Cmd().
Subtitle("Open Gist in browser").
Arg(gist.Fields["URL"].(string)).
Valid(true)
it.Opt().
Subtitle("Edit locally").
Arg(fmt.Sprintf("%v", int(gist.Fields["IDX"].(float64)))).
Valid(true)
}
}
func fieldSummary(field string) {
// Calculates frequencies for a given field
facet := bleve.NewFacetRequest(field, 100000)
query := query.NewMatchAllQuery()
searchRequest := bleve.NewSearchRequest(query)
searchRequest.AddFacet("count", facet)
searchResults, err := dbIdx.Search(searchRequest)
if err != nil {
panic(err)
}
// term with highest occurrences in field name
data := make([][]string, searchResults.Facets["count"].Terms.Len())
for idx, val := range searchResults.Facets["count"].Terms {
data[idx] = []string{val.Term, strconv.Itoa(val.Count)}
}
if outputFormat == "console" {
fieldSummaryTable(field, data)
} else if outputFormat == "alfred" {
fieldSummaryAlfred(field, data)
}
}
func outputGist(gistIdx int) {
gist := lookupGist(gistIdx)
fileset := parseGistFiles(gist)
for _, file := range fileset {
var xsize, _, _ = terminal.GetSize(0)
var line = strings.Repeat("-", xsize-len(file["filename"])-50)
var isPrivate string
if gist.Fields["Public"] == "false" {
isPrivate = "🔒"
} else {
isPrivate = "-"
}
if outputPipe() {
fmt.Print(file["content"])
} else {
errlog.Printf("%s%s%s%s", greenText.Sprint(file["filename"]), isPrivate, line, file["language"])
highlight(os.Stdout, file["filename"], file["content"], "terminal16m", "fruity")
fmt.Fprintf(os.Stderr, "\n\n")
}
}
}
func fetchGistContent(gistIdx int) string {
gist := lookupGist(gistIdx)
fileset := parseGistFiles(gist)
var result string
for _, file := range fileset {
result += file["content"]
}
return result
}