-
Notifications
You must be signed in to change notification settings - Fork 2
/
finder.go
102 lines (86 loc) · 2.13 KB
/
finder.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
package ui
import (
"fmt"
"github.com/dustin/go-humanize"
fzf "github.com/ktr0731/go-fuzzyfinder"
"github.com/sheepla/ghin/gh"
)
const noResultsText = "NO RESULTS"
//nolint:wrapcheck
func SelectRepo(repos gh.Repos) (*gh.Repo, error) {
idx, err := fzf.Find(
repos,
func(i int) string {
return fmt.Sprintf("%s/%s", repos[i].Owner, repos[i].Name)
},
//nolint:varnamelen
fzf.WithPreviewWindow(func(i, width, height int) string {
if i == -1 {
return noResultsText
}
return fmt.Sprintf(
"%s/%s (%d stars, %d forks)\n\n%s\n\nlicense: %s\nlanguage: %s\ncreated at: %s\nupdated at: %s",
repos[i].Owner,
repos[i].Name,
repos[i].Stars,
repos[i].Forks,
repos[i].Description,
repos[i].License,
repos[i].Language,
humanize.Time(repos[i].CreatedAt),
humanize.Time(repos[i].UpdatedAt),
)
}),
)
return &repos[idx], err
}
//nolint:wrapcheck
func SelectTag(rels gh.Releases) (*gh.Release, error) {
idx, err := fzf.Find(
rels,
func(i int) string {
return rels[i].Tag
},
//nolint:varnamelen
fzf.WithPreviewWindow(func(i, width, height int) string {
if i == -1 {
return noResultsText
}
return fmt.Sprintf(
"%s\n\nby %s\n\ncreated at: %s, published at: %s\n\nSee release page: %s\n\n─────────\n\n%s\n\n─────────\n\n",
rels[i].Tag,
rels[i].Author,
humanize.Time(rels[i].CreatedAt),
humanize.Time(rels[i].PublishedAt),
rels[i].PageURL,
rels[i].ReleaseNote,
)
}),
)
return &rels[idx], err
}
//nolint:wrapcheck
func SelectAsset(assets gh.Assets) (*gh.Asset, error) {
idx, err := fzf.Find(
assets,
func(i int) string {
size := humanize.Bytes(uint64(assets[i].Size))
return fmt.Sprintf("%s (%s)", assets[i].Name, size)
},
//nolint:varnamelen
fzf.WithPreviewWindow(func(i, width, height int) string {
if i == -1 {
return noResultsText
}
size := humanize.Bytes(uint64(assets[i].Size))
return fmt.Sprintf(
"%s (%s)\n\ncontent type: %s\n\nuploaded by %s\n",
assets[i].Name,
size,
assets[i].ContentType,
assets[i].Uploader,
)
}),
)
return &assets[idx], err
}