-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
186 lines (161 loc) · 4.01 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"os/exec"
"strings"
"time"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type state int
const (
choosePrefix state = iota
enterMessage
commitDone
)
var (
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
emojiMap = map[string][]string{}
)
type model struct {
choices []string // CLIに表示するアイテム
cursor int // カーソルの位置
selected string // 選択されたアイテム
message textinput.Model // テキスト入力用のモデル
quitting bool // 終了フラグ
currentState state // 状態
}
func initialModel() model {
ti := textinput.New()
ti.Placeholder = "Enter your commit message"
ti.Focus()
ti.CharLimit = 156
ti.Width = 40
return model{
choices: []string{
"feat: A new feature",
"fix: A bug fix",
"docs: Documentation only changes",
"style: Changes that do not affect the code meaning (white-space, formatting, etc.)",
"refactor: A code change that neither fixes a bug nor adds a feature",
"perf: A code change that improves performance",
"test: Adding missing tests or correcting existing tests",
"chore: Other changes that don't modify src or test files",
},
cursor: 0,
message: ti,
currentState: choosePrefix,
}
}
func (m model) Init() tea.Cmd {
return textinput.Blink
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch m.currentState {
case choosePrefix:
switch msg.String() {
case "ctrl+c", "q":
m.quitting = true
return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
case "enter":
prefix := strings.SplitN(m.choices[m.cursor], ":", 2)[0]
m.selected = prefix + ": " + randomEmoji(prefix) + " "
m.currentState = enterMessage
}
case enterMessage:
switch msg.String() {
case "ctrl+c":
m.quitting = true
return m, tea.Quit
case "enter":
m.commit(m.selected + m.message.Value())
m.currentState = commitDone
return m, tea.Quit
}
m.message, cmd = m.message.Update(msg)
}
}
return m, cmd
}
func (m model) View() string {
if m.quitting {
return "Exiting...\n"
}
switch m.currentState {
case choosePrefix:
s := "Choose a commit message prefix:\n\n"
for i, choice := range m.choices {
cursor := " "
line := itemStyle.Render(choice)
if m.cursor == i {
cursor = ">"
line = selectedItemStyle.Render(choice)
}
s += fmt.Sprintf("%s %s\n", cursor, line)
}
return s
case enterMessage:
return fmt.Sprintf("Enter your commit message (starting with %s):\n\n%s%s", m.selected, m.selected, m.message.View())
case commitDone:
return "Commit complete!\n"
}
return ""
}
func (m *model) commit(commitMessage string) {
cmd := exec.Command("git", "commit", "-m", commitMessage)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println("Failed to commit:", err)
os.Exit(1)
}
}
func randomEmoji(prefix string) string {
if emojis, ok := emojiMap[prefix]; ok {
rand.Seed(time.Now().UnixNano())
return emojis[rand.Intn(len(emojis))]
}
return ""
}
func loadEmojis(filename string) {
data, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatalf("Failed to read emoji file: %v", err)
}
err = json.Unmarshal(data, &emojiMap)
if err != nil {
log.Fatalf("Failed to parse emoji file: %v", err)
}
}
func main() {
emojiFile := os.Getenv("EMOJI_FILE")
if emojiFile == "" {
log.Fatalf("EMOJI_FILE is not set")
}
loadEmojis(emojiFile)
m := initialModel()
p := tea.NewProgram(m)
if err := p.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Alas, there's been an error: %v", err)
os.Exit(1)
}
}