@@ -2,33 +2,28 @@ package selfcmd
2
2
3
3
import (
4
4
"fmt"
5
+ "runtime"
6
+ "strings"
5
7
6
- "github.com/charmbracelet/bubbles/spinner"
7
- "github.com/charmbracelet/bubbles/textinput"
8
8
tea "github.com/charmbracelet/bubbletea"
9
- "github.com/charmbracelet/lipgloss"
10
- semver "github.com/hashicorp/go-version"
9
+ "github.com/google/go-github/v71/github"
11
10
"github.com/pubgo/funk/log"
12
- )
13
-
14
- const (
15
- envAlpha = "alpha"
16
- envBeta = "beta"
17
- envRelease = "release"
11
+ "github.com/samber/lo"
18
12
)
19
13
20
14
type model struct {
21
15
cursor int
22
- choices [] string
23
- selected string
16
+ assets [] * github. ReleaseAsset
17
+ selected * github. ReleaseAsset
24
18
length int
25
19
}
26
20
27
- func initialModel () model {
28
- choices := []string {envAlpha , envBeta , envRelease }
21
+ func initialModel (rsp * github.RepositoryRelease ) model {
29
22
return model {
30
- choices : choices ,
31
- length : len (choices ),
23
+ assets : lo .Filter (rsp .Assets , func (item * github.ReleaseAsset , index int ) bool {
24
+ return ! strings .Contains (item .GetName (), "checksums" ) && strings .Contains (strings .ToLower (item .GetName ()), strings .ToLower (runtime .GOOS ))
25
+ }),
26
+ length : len (rsp .Assets ) - 1 ,
32
27
}
33
28
}
34
29
@@ -43,7 +38,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
43
38
case tea .KeyUp , tea .KeyLeft , tea .KeyDown , tea .KeyRight :
44
39
m .cursor ++
45
40
case tea .KeyEnter :
46
- m .selected = m .choices [m .cursor % m .length ]
41
+ m .selected = m .assets [m .cursor % m .length ]
47
42
return m , tea .Quit
48
43
default :
49
44
log .Error ().Str ("key" , msg .String ()).Msg ("unknown key" )
@@ -55,126 +50,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
55
50
}
56
51
57
52
func (m model ) View () string {
58
- s := "Please Select Pre Tag :\n "
53
+ s := "Please Select:\n "
59
54
60
- for i , choice := range m .choices {
55
+ for i , choice := range m .assets {
61
56
cursor := " "
62
57
if m .cursor % m .length == i {
63
58
cursor = ">"
64
59
}
65
60
66
- s += fmt .Sprintf ("%s %s\n " , cursor , choice )
61
+ s += fmt .Sprintf ("%s %s\n " , cursor , lo . FromPtr ( choice . Name ) )
67
62
}
68
63
69
64
return s
70
65
}
71
-
72
- type model1 struct {
73
- spinner spinner.Model
74
- quitting bool
75
- err error
76
- }
77
-
78
- func InitialModelNew () model1 {
79
- s := spinner .New ()
80
- s .Spinner = spinner .Line
81
- s .Style = lipgloss .NewStyle ().Foreground (lipgloss .Color ("205" ))
82
- return model1 {spinner : s }
83
- }
84
-
85
- func (m model1 ) Init () tea.Cmd {
86
- return m .spinner .Tick
87
- }
88
-
89
- func (m model1 ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
90
- switch msg := msg .(type ) {
91
- case tea.KeyMsg :
92
- switch msg .String () {
93
- case "q" , "esc" , "ctrl+c" :
94
- m .quitting = true
95
- return m , tea .Quit
96
- default :
97
- return m , nil
98
- }
99
-
100
- default :
101
- var cmd tea.Cmd
102
- m .spinner , cmd = m .spinner .Update (msg )
103
- return m , cmd
104
- }
105
- }
106
-
107
- func (m model1 ) View () string {
108
-
109
- if m .err != nil {
110
- return m .err .Error ()
111
- }
112
- str := fmt .Sprintf ("%s Preparing..." , m .spinner .View ())
113
- if m .quitting {
114
- return str + "\n "
115
- }
116
- return str
117
- }
118
-
119
- type model2 struct {
120
- textInput textinput.Model
121
- exit bool
122
- }
123
-
124
- // sanitizeInput verifies that an input text string gets validated
125
- func sanitizeInput (input string ) error {
126
- _ , err := semver .NewSemver (input )
127
- return err
128
- }
129
-
130
- func InitialTextInputModel (data string ) model2 {
131
- ti := textinput .New ()
132
- ti .Focus ()
133
- ti .Prompt = ""
134
- ti .CharLimit = 156
135
- ti .Width = 20
136
- ti .Validate = sanitizeInput
137
- ti .SetValue (data )
138
-
139
- return model2 {
140
- textInput : ti ,
141
- }
142
- }
143
-
144
- // Init is called at the beginning of a textinput step
145
- // and sets the cursor to blink
146
- func (m model2 ) Init () tea.Cmd {
147
- return textinput .Blink
148
- }
149
-
150
- // Update is called when "things happen", it checks for the users text input,
151
- // and for Ctrl+C or Esc to close the program.
152
- func (m model2 ) Update (msg tea.Msg ) (tea.Model , tea.Cmd ) {
153
- var cmd tea.Cmd
154
-
155
- switch msg := msg .(type ) {
156
- case tea.KeyMsg :
157
- switch msg .Type {
158
- case tea .KeyEnter :
159
- return m , tea .Quit
160
- case tea .KeyCtrlC , tea .KeyEsc :
161
- m .exit = true
162
- return m , tea .Quit
163
- }
164
- }
165
-
166
- m .textInput , cmd = m .textInput .Update (msg )
167
- return m , cmd
168
- }
169
-
170
- // View is called to draw the textinput step
171
- func (m model2 ) View () string {
172
- return fmt .Sprintf (
173
- "new tag: %s\n " ,
174
- m .textInput .View (),
175
- )
176
- }
177
-
178
- func (m model2 ) Value () string {
179
- return m .textInput .Value ()
180
- }
0 commit comments