-
Notifications
You must be signed in to change notification settings - Fork 18
/
ui_commit.go
112 lines (92 loc) · 2.24 KB
/
ui_commit.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
103
104
105
106
107
108
109
110
111
112
package main
import (
"strings"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
)
const (
SELECTOR = iota
INPUTS
COMMIT
ERROR
)
type done struct {
nextView int
err error
}
type commitMsg struct {
Type string
Scope string
Subject string
Body string
Footer string
SOB string
}
type commitModel struct {
err error
views []tea.Model
viewIndex int
}
func (m commitModel) Init() tea.Cmd {
return func() tea.Msg {
err := repoCheck()
if err != nil {
return done{nextView: ERROR, err: err}
}
err = hasStagedFiles()
if err != nil {
return done{nextView: ERROR, err: err}
}
return nil
}
}
func (m *commitModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.(type) {
case done: // If the view returns a done message, it means that the stage has been processed
m.err = msg.(done).err
m.viewIndex = msg.(done).nextView
// some special views need to determine the state of the data to update
switch m.viewIndex {
case INPUTS:
return m, tea.Batch(textinput.Blink, spinner.Tick, m.inputs)
case COMMIT:
return m, tea.Batch(spinner.Tick, m.commit)
case ERROR:
return m, m.showErr
default:
return m, tea.Quit
}
default: // By default, the cmd returned by the view needs to be processed by itself
var cmd tea.Cmd
m.views[m.viewIndex], cmd = m.views[m.viewIndex].Update(msg)
return m, cmd
}
}
func (m commitModel) View() string {
return m.views[m.viewIndex].View()
}
func (m commitModel) inputs() tea.Msg {
return strings.ToUpper(m.views[SELECTOR].(selectorModel).choice)
}
func (m commitModel) commit() tea.Msg {
sob, err := createSOB()
if err != nil {
return done{err: err}
}
msg := commitMsg{
Type: m.views[SELECTOR].(selectorModel).choice,
Scope: m.views[INPUTS].(inputsModel).inputs[0].input.Value(),
Subject: m.views[INPUTS].(inputsModel).inputs[1].input.Value(),
Body: strings.Replace(m.views[INPUTS].(inputsModel).inputs[2].input.Value(), newLineKey, "\n", -1),
Footer: m.views[INPUTS].(inputsModel).inputs[3].input.Value(),
SOB: sob,
}
if msg.Body == "" {
msg.Body = msg.Subject
}
return msg
}
func (m commitModel) showErr() tea.Msg {
return m.err
}