Skip to content

Commit 279ba24

Browse files
committed
refactor: rename files
1 parent cf2f31a commit 279ba24

3 files changed

Lines changed: 413 additions & 413 deletions

File tree

internal/app.go

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
package internal
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"strings"
7+
8+
"github.com/charmbracelet/bubbles/spinner"
9+
tea "github.com/charmbracelet/bubbletea"
10+
11+
"github.com/a3chron/gith/internal/config"
12+
"github.com/a3chron/gith/internal/ui"
13+
)
14+
15+
func UpdateOnInit() tea.Cmd {
16+
return func() tea.Msg {
17+
if err := UpdateRepo(); err != nil {
18+
return RepoUpdateErrorMsg{}
19+
}
20+
return RepoUpdatedMsg{}
21+
}
22+
}
23+
24+
func (m Model) Init() tea.Cmd {
25+
return tea.Batch(
26+
m.Spinner.Tick,
27+
UpdateOnInit(),
28+
)
29+
}
30+
31+
func (m Model) getCurrentOptions() []string {
32+
switch m.CurrentStep {
33+
case StepAction:
34+
return m.ActionModel.Actions
35+
36+
case StepBranchAction:
37+
return m.BranchModel.Actions
38+
case StepBranchSelect:
39+
return m.BranchModel.Branches
40+
case StepBranchCreate:
41+
return m.BranchModel.Options
42+
43+
case StepCommitAction:
44+
return m.CommitModel.Actions
45+
case StepCommitSelectPrefix:
46+
return m.CommitModel.CommitPrefixes
47+
48+
case StepTag:
49+
return m.TagModel.Actions
50+
case StepTagSelect:
51+
return m.TagModel.Options
52+
case StepTagAdd:
53+
return m.TagModel.AddOptions
54+
55+
case StepRemote:
56+
return m.RemoteModel.Actions
57+
case StepRemoteSelect:
58+
return m.RemoteModel.Options
59+
60+
case StepOptions:
61+
return m.ConfigModel.Actions
62+
case StepOptionsFlavorSelect:
63+
return m.ConfigModel.Flavors
64+
case StepOptionsAccentSelect:
65+
return m.ConfigModel.Accents
66+
default:
67+
return []string{}
68+
}
69+
}
70+
71+
func (m *Model) resetState() {
72+
m.CurrentStep = StepAction
73+
m.ActionModel.SelectedAction = ""
74+
75+
m.BranchModel.SelectedBranch = ""
76+
m.BranchModel.SelectedAction = ""
77+
m.BranchModel.SelectedOption = ""
78+
m.BranchModel.Input = ""
79+
80+
m.CommitModel.SelectedAction = ""
81+
m.CommitModel.SelectedPrefix = ""
82+
m.CommitModel.CommitMessage = ""
83+
84+
m.TagModel.SelectedAction = ""
85+
m.TagModel.SelectedOption = ""
86+
m.TagModel.SelectedAddTag = ""
87+
m.TagModel.CurrentTag = ""
88+
m.TagModel.ManualInput = ""
89+
90+
m.RemoteModel.SelectedAction = ""
91+
m.RemoteModel.SelectedOption = ""
92+
m.RemoteModel.NameInput = ""
93+
m.RemoteModel.UrlInput = ""
94+
95+
m.ConfigModel.SelectedAccent = ""
96+
m.ConfigModel.SelectedFlavor = ""
97+
m.ConfigModel.SelectedAction = ""
98+
m.Selected = 0
99+
m.Level = 1
100+
m.Output = []string{} // TODO add m.Output[0] if exisiting
101+
m.Err = ""
102+
m.Success = ""
103+
m.StartAt = ""
104+
}
105+
106+
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
107+
switch msg := msg.(type) {
108+
case RepoUpdatedMsg, RepoUpdateErrorMsg:
109+
m.Loading = false
110+
111+
if _, isErr := msg.(RepoUpdateErrorMsg); isErr {
112+
m.OutputByLevel("Failed to fetch from remote")
113+
}
114+
115+
// quick-start flows triggered via StartAt
116+
switch m.StartAt {
117+
case "add-tag":
118+
m.PrepareTagAddition()
119+
m.ActionModel.SelectedAction = "Tag"
120+
m.TagModel.SelectedAction = "Add Tag"
121+
122+
case "push-tag":
123+
m.PrepareTagSelection()
124+
m.ActionModel.SelectedAction = "Tag"
125+
m.TagModel.SelectedAction = "Push Tag"
126+
127+
case "commit-staged":
128+
m.Level = 3
129+
m.Selected = 0
130+
m.CurrentStep = StepCommitSelectPrefix
131+
m.ActionModel.SelectedAction = "Commit"
132+
m.CommitModel.SelectedAction = "Commit Staged"
133+
134+
case "commit-all":
135+
m.Level = 3
136+
m.Selected = 0
137+
m.CurrentStep = StepCommitSelectPrefix
138+
m.ActionModel.SelectedAction = "Commit"
139+
m.CommitModel.SelectedAction = "Commit All"
140+
141+
default:
142+
m.CurrentStep = StepAction
143+
m.Level = 1
144+
}
145+
146+
return m, nil
147+
148+
case spinner.TickMsg:
149+
m.Spinner, _ = m.Spinner.Update(msg)
150+
return m, m.Spinner.Tick
151+
152+
case tea.KeyMsg:
153+
if isInputStep(m.CurrentStep) {
154+
switch msg.String() {
155+
case "ctrl+c", "esc":
156+
m.Err = "User cancelled"
157+
return m, tea.Quit
158+
case "ctrl+h":
159+
m.resetState()
160+
case "enter":
161+
switch m.CurrentStep {
162+
case StepTagInput:
163+
return m.HandleTagInputSubmit()
164+
case StepBranchInput:
165+
return m.HandleBranchInputSubmit()
166+
case StepRemoteNameInput:
167+
// proceed to URL input if name provided
168+
if strings.TrimSpace(m.RemoteModel.NameInput) == "" {
169+
m.Err = "Remote name cannot be empty"
170+
return m, tea.Quit
171+
}
172+
m.CurrentStep = StepRemoteUrlInput
173+
return m, nil
174+
case StepRemoteUrlInput:
175+
if strings.TrimSpace(m.RemoteModel.UrlInput) == "" {
176+
m.Err = "Remote URL cannot be empty"
177+
return m, tea.Quit
178+
}
179+
out, err := exec.Command("git", "remote", "add", m.RemoteModel.NameInput, m.RemoteModel.UrlInput).CombinedOutput()
180+
if err != nil {
181+
m.OutputByLevel(string(out))
182+
m.Err = "Failed to add remote"
183+
} else {
184+
m.Success = fmt.Sprintf("Added Remote '%s' -> %s", m.RemoteModel.NameInput, m.RemoteModel.UrlInput)
185+
}
186+
return m, tea.Quit
187+
case StepCommitInput:
188+
return m.HandleCommitMessageSubmit()
189+
}
190+
case "backspace":
191+
switch m.CurrentStep {
192+
case StepTagInput:
193+
if len(m.TagModel.ManualInput) > 0 {
194+
m.TagModel.ManualInput = m.TagModel.ManualInput[:len(m.TagModel.ManualInput)-1]
195+
}
196+
case StepBranchInput:
197+
if len(m.BranchModel.Input) > 0 {
198+
m.BranchModel.Input = m.BranchModel.Input[:len(m.BranchModel.Input)-1]
199+
}
200+
case StepRemoteNameInput:
201+
if len(m.RemoteModel.NameInput) > 0 {
202+
m.RemoteModel.NameInput = m.RemoteModel.NameInput[:len(m.RemoteModel.NameInput)-1]
203+
}
204+
case StepRemoteUrlInput:
205+
if len(m.RemoteModel.UrlInput) > 0 {
206+
m.RemoteModel.UrlInput = m.RemoteModel.UrlInput[:len(m.RemoteModel.UrlInput)-1]
207+
}
208+
case StepCommitInput:
209+
if len(m.CommitModel.CommitMessage) > 0 {
210+
m.CommitModel.CommitMessage = m.CommitModel.CommitMessage[:len(m.CommitModel.CommitMessage)-1]
211+
}
212+
}
213+
default:
214+
// Add character to input
215+
if len(msg.String()) == 1 {
216+
switch m.CurrentStep {
217+
case StepTagInput:
218+
m.TagModel.ManualInput += msg.String()
219+
case StepBranchInput:
220+
m.BranchModel.Input += msg.String()
221+
case StepRemoteNameInput:
222+
m.RemoteModel.NameInput += msg.String()
223+
case StepRemoteUrlInput:
224+
m.RemoteModel.UrlInput += msg.String()
225+
case StepCommitInput:
226+
m.CommitModel.CommitMessage += msg.String()
227+
}
228+
}
229+
}
230+
return m, nil
231+
}
232+
233+
switch msg.String() {
234+
case "q", "ctrl+c", "esc":
235+
m.Err = "User quit"
236+
return m, tea.Quit
237+
238+
case "ctrl+h":
239+
if m.CurrentStep == StepAction || m.CurrentStep == StepLoad {
240+
return m, tea.Quit
241+
}
242+
m.resetState()
243+
244+
case "up", "k", "down", "j":
245+
m.handleNavigation(msg.String())
246+
247+
case "enter":
248+
return m.handleEnterKey()
249+
}
250+
}
251+
return m, nil
252+
}
253+
254+
func (m Model) handleEnterKey() (tea.Model, tea.Cmd) {
255+
switch m.CurrentStep {
256+
case StepAction:
257+
return m.HandleActionSelection()
258+
259+
case StepBranchAction:
260+
return m.HandleBranchActionSelection()
261+
case StepBranchSelect:
262+
return m.HandleBranchSelection()
263+
case StepBranchCreate:
264+
return m.HandleBranchCreateSelection()
265+
266+
case StepCommitAction:
267+
return m.HandleCommitSelection()
268+
case StepCommitSelectPrefix:
269+
return m.HandleCommitPrefixSelection()
270+
271+
case StepTag:
272+
return m.HandleTagActionSelection()
273+
case StepTagSelect:
274+
return m.HandleTagSelection()
275+
case StepTagAdd:
276+
return m.HandleTagAddSelection()
277+
278+
case StepRemote:
279+
return m.HandleRemoteActionSelection()
280+
case StepRemoteSelect:
281+
return m.HandleRemoteSelection()
282+
283+
case StepOptions:
284+
return m.HandleOptionsActionSelection()
285+
case StepOptionsFlavorSelect:
286+
return m.HandleOptionsFlavorSelection()
287+
case StepOptionsAccentSelect:
288+
return m.HandleOptionsAccentSelection()
289+
}
290+
return m, nil
291+
}
292+
293+
func (m *Model) handleNavigation(key string) {
294+
options := m.getCurrentOptions()
295+
if len(options) == 0 {
296+
return
297+
}
298+
299+
switch key {
300+
case "up", "k":
301+
if m.Selected > 0 {
302+
m.Selected--
303+
} else {
304+
m.Selected = len(options) - 1
305+
}
306+
case "down", "j":
307+
if m.Selected < len(options)-1 {
308+
m.Selected++
309+
} else {
310+
m.Selected = 0
311+
}
312+
}
313+
314+
// handle preview when selecting a theme
315+
if m.CurrentStep == StepOptionsAccentSelect {
316+
flavor := config.GetCatppuccinFlavor(m.CurrentConfig.Flavor)
317+
ui.UpdateStyles(flavor, config.GetAccentColor(flavor, m.ConfigModel.Accents[m.Selected]))
318+
}
319+
320+
if m.CurrentStep == StepOptionsFlavorSelect {
321+
flavor := config.GetCatppuccinFlavor(m.ConfigModel.Flavors[m.Selected])
322+
ui.UpdateStyles(flavor, config.GetAccentColor(flavor, m.CurrentConfig.Accent))
323+
}
324+
}

0 commit comments

Comments
 (0)