@@ -2,33 +2,28 @@ package selfcmd
22
33import (
44 "fmt"
5+ "runtime"
6+ "strings"
57
6- "github.com/charmbracelet/bubbles/spinner"
7- "github.com/charmbracelet/bubbles/textinput"
88 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"
1110 "github.com/pubgo/funk/log"
12- )
13-
14- const (
15- envAlpha = "alpha"
16- envBeta = "beta"
17- envRelease = "release"
11+ "github.com/samber/lo"
1812)
1913
2014type model struct {
2115 cursor int
22- choices [] string
23- selected string
16+ assets [] * github. ReleaseAsset
17+ selected * github. ReleaseAsset
2418 length int
2519}
2620
27- func initialModel () model {
28- choices := []string {envAlpha , envBeta , envRelease }
21+ func initialModel (rsp * github.RepositoryRelease ) model {
2922 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 ,
3227 }
3328}
3429
@@ -43,7 +38,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
4338 case tea .KeyUp , tea .KeyLeft , tea .KeyDown , tea .KeyRight :
4439 m .cursor ++
4540 case tea .KeyEnter :
46- m .selected = m .choices [m .cursor % m .length ]
41+ m .selected = m .assets [m .cursor % m .length ]
4742 return m , tea .Quit
4843 default :
4944 log .Error ().Str ("key" , msg .String ()).Msg ("unknown key" )
@@ -55,126 +50,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5550}
5651
5752func (m model ) View () string {
58- s := "Please Select Pre Tag :\n "
53+ s := "Please Select:\n "
5954
60- for i , choice := range m .choices {
55+ for i , choice := range m .assets {
6156 cursor := " "
6257 if m .cursor % m .length == i {
6358 cursor = ">"
6459 }
6560
66- s += fmt .Sprintf ("%s %s\n " , cursor , choice )
61+ s += fmt .Sprintf ("%s %s\n " , cursor , lo . FromPtr ( choice . Name ) )
6762 }
6863
6964 return s
7065}
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