-
Notifications
You must be signed in to change notification settings - Fork 18
/
ui_new_branch.go
78 lines (65 loc) · 1.98 KB
/
ui_new_branch.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
package main
import (
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
newBranchLayout = lipgloss.NewStyle().
Padding(1, 1, 1, 2)
newBranchSuccessTextStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.AdaptiveColor{Light: "#25A065", Dark: "#2AD67F"})
newBranchSuccessNameStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.AdaptiveColor{Light: "#1A1A1A", Dark: "#FFFDF5"}).
Background(lipgloss.AdaptiveColor{Light: "#5B44FF", Dark: "#7653FF"})
newBranchFailedTextStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.AdaptiveColor{Light: "#D63B3A", Dark: "#D63B3A"})
newBranchFailedErrStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.AdaptiveColor{Light: "#1A1A1A", Dark: "#FFFDF5"}).
Background(lipgloss.AdaptiveColor{Light: "#D63B3A", Dark: "#D63B3A"})
)
type branchModel struct {
branch string
done bool
err error
}
func newBranchModel(branch string) branchModel {
return branchModel{branch: branch}
}
func (m branchModel) Init() tea.Cmd {
return func() tea.Msg { return m.branch }
}
func (m branchModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
return m, tea.Quit
}
case string:
time.Sleep(500 * time.Millisecond)
_, m.err = createBranch(msg)
m.done = true
return m, tea.Quit
}
return m, nil
}
func (m branchModel) View() string {
msg := newBranchLayout.Render(newBranchSuccessTextStyle.Render(" (⊙ˍ⊙) Creating new branch: " + m.branch + " "))
if m.done {
if m.err == nil {
msg = newBranchLayout.Render(
newBranchSuccessTextStyle.Render(" (^_~) Switched to a new branch: ") +
newBranchSuccessNameStyle.Render(m.branch) + " ")
} else {
msg = newBranchLayout.Render(
newBranchFailedTextStyle.Render(" (。•́︿•̀。) Create failed: ") +
newBranchFailedErrStyle.Render(m.err.Error()) + " ")
}
}
return msg
}