Skip to content

Commit 3be9681

Browse files
authored
Merge pull request #36 from cocoide/develop
Merge Develop
2 parents 00e4bdd + dc0a509 commit 3be9681

File tree

20 files changed

+896
-95
lines changed

20 files changed

+896
-95
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.PHONY: test
2+
3+
test:
4+
go generate ./...
5+
go test -v ./internal/service

cmd/config.go

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/charmbracelet/bubbles/textinput"
8+
tea "github.com/charmbracelet/bubbletea"
9+
"github.com/cocoide/commitify/util"
10+
"github.com/fatih/color"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
var (
15+
configKey = [...]string{"api-key", "language", "format"}
16+
configOption = [][]string{
17+
{},
18+
{"Japanese", "English"},
19+
{"Format 1", "Format 2"},
20+
}
21+
)
22+
23+
type configModel struct {
24+
configKeyIndex int
25+
configOptionIndex int
26+
configKeySelected bool
27+
err error
28+
textInput textinput.Model
29+
}
30+
31+
func initConfigModel() configModel {
32+
ti := textinput.New()
33+
ti.Focus()
34+
35+
return configModel{
36+
textInput: ti,
37+
err: nil,
38+
}
39+
}
40+
41+
func (cm configModel) Init() tea.Cmd {
42+
return textinput.Blink
43+
}
44+
45+
func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
46+
switch cm.configKeySelected {
47+
// 設定項目を選択する
48+
case false:
49+
switch msg := msg.(type) {
50+
case tea.KeyMsg:
51+
switch msg.Type {
52+
case tea.KeyUp:
53+
if cm.configKeyIndex > 0 {
54+
cm.configKeyIndex--
55+
}
56+
case tea.KeyDown:
57+
if cm.configKeyIndex < len(configKey)-1 {
58+
cm.configKeyIndex++
59+
}
60+
case tea.KeyEnter:
61+
cm.configKeySelected = true
62+
return cm, nil
63+
case tea.KeyCtrlC, tea.KeyEsc:
64+
return cm, tea.Quit
65+
}
66+
}
67+
68+
// 設定項目に値をセットする
69+
case true:
70+
switch len(configOption[cm.configKeyIndex]) {
71+
// 選択肢のない項目は入力を受け付ける
72+
case 0:
73+
var cmd tea.Cmd
74+
switch msg := msg.(type) {
75+
case tea.KeyMsg:
76+
switch msg.Type {
77+
case tea.KeyEnter:
78+
saveConfig(cm)
79+
return cm, tea.Quit
80+
case tea.KeyCtrlC, tea.KeyEsc:
81+
return cm, tea.Quit
82+
}
83+
case error:
84+
cm.err = msg
85+
return cm, nil
86+
}
87+
88+
cm.textInput, cmd = cm.textInput.Update(msg)
89+
return cm, cmd
90+
91+
// 選択肢がある場合はセレクターで表示する
92+
default:
93+
switch msg := msg.(type) {
94+
case tea.KeyMsg:
95+
switch msg.Type {
96+
case tea.KeyUp:
97+
if cm.configOptionIndex > 0 {
98+
cm.configOptionIndex--
99+
}
100+
case tea.KeyDown:
101+
if cm.configOptionIndex < len(configOption[cm.configKeyIndex])-1 {
102+
cm.configOptionIndex++
103+
}
104+
case tea.KeyEnter:
105+
saveConfig(cm)
106+
return cm, tea.Quit
107+
case tea.KeyCtrlC, tea.KeyEsc:
108+
return cm, tea.Quit
109+
}
110+
}
111+
}
112+
}
113+
114+
return cm, nil
115+
}
116+
117+
func (cm configModel) View() string {
118+
var b strings.Builder
119+
120+
switch cm.configKeySelected {
121+
// 設定項目を選んでいない時
122+
case false:
123+
white := color.New(color.FgWhite).SprintFunc()
124+
b.WriteString(white("設定項目を選んでください:\n"))
125+
b.WriteString(white(" ↑↓の矢印キーで項目を移動、Enterで選択\n"))
126+
127+
for i, choice := range configKey {
128+
cyan := color.New(color.FgCyan).SprintFunc()
129+
hiCyan := color.New(color.FgHiCyan).SprintFunc()
130+
if i == cm.configKeyIndex {
131+
b.WriteString(fmt.Sprintf(hiCyan("➡️ %s\n"), choice))
132+
} else {
133+
b.WriteString(fmt.Sprintf(cyan(" %s\n"), choice))
134+
}
135+
}
136+
137+
// 設定項目に値をセットする
138+
case true:
139+
// 選択肢のない項目はテキストエリアを表示
140+
switch len(configOption[cm.configKeyIndex]) {
141+
case 0:
142+
white := color.New(color.FgWhite).SprintFunc()
143+
b.WriteString(white(fmt.Sprintf(
144+
"ここに%sを入力: %s\n",
145+
configKey[cm.configKeyIndex],
146+
cm.textInput.View(),
147+
)))
148+
b.WriteString(white(" Enterキーで確定"))
149+
150+
default:
151+
white := color.New(color.FgWhite).SprintFunc()
152+
b.WriteString(white("設定内容を選んでください:\n"))
153+
b.WriteString(white(" ↑↓の矢印キーで項目を移動、Enterで選択\n"))
154+
155+
for i, option := range configOption[cm.configKeyIndex] {
156+
cyan := color.New(color.FgCyan).SprintFunc()
157+
hiCyan := color.New(color.FgHiCyan).SprintFunc()
158+
if i == cm.configOptionIndex {
159+
b.WriteString(fmt.Sprintf(hiCyan("➡️ %s\n"), option))
160+
} else {
161+
b.WriteString(fmt.Sprintf(cyan(" %s\n"), option))
162+
}
163+
}
164+
}
165+
}
166+
167+
return b.String()
168+
}
169+
170+
var configCmd = &cobra.Command{
171+
Use: "config",
172+
Short: "設定を変更します",
173+
Long: `設定を変更します。設定項目はコマンドを実行すると表示されます。`,
174+
Run: func(cmd *cobra.Command, args []string) {
175+
p := tea.NewProgram(initConfigModel())
176+
p.Run()
177+
},
178+
}
179+
180+
func init() {
181+
rootCmd.AddCommand(configCmd)
182+
}
183+
184+
func saveConfig(cm configModel) {
185+
currentConfig, err := util.ReadConfig()
186+
if err != nil {
187+
fmt.Println(err)
188+
}
189+
190+
switch cm.configKeyIndex {
191+
case 0:
192+
currentConfig.ChatGptApiKey = cm.textInput.Value()
193+
case 1:
194+
currentConfig.UseLanguage = configOption[cm.configKeyIndex][cm.configOptionIndex]
195+
case 2:
196+
currentConfig.CommitFormat = configOption[cm.configKeyIndex][cm.configOptionIndex]
197+
}
198+
199+
err = util.WriteConfig(currentConfig)
200+
if err != nil {
201+
fmt.Println(err)
202+
}
203+
}

cmd/docs.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"fmt"
8+
9+
"github.com/cocoide/commitify/static"
10+
"github.com/fatih/color"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
// docsCmd represents the docs command
15+
var docsCmd = &cobra.Command{
16+
Use: "docs",
17+
Short: "Document of commitify",
18+
Run: func(cmd *cobra.Command, args []string) {
19+
b, _ := static.Logo.ReadFile("logo.txt")
20+
cyan := color.New(color.FgCyan).SprintFunc()
21+
logo := cyan(string(b))
22+
fmt.Println(logo)
23+
},
24+
}
25+
26+
func init() {
27+
rootCmd.AddCommand(docsCmd)
28+
}

cmd/settings.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

cmd/suggest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ type generateMessages struct {
2828

2929
func (m model) Init() tea.Cmd {
3030
return func() tea.Msg {
31-
util.LoadEnv()
3231
ctx := context.Background()
3332
og := gateway.NewOpenAIGateway(ctx)
3433
ms := service.NewMessageService(og)
35-
messages, err := ms.GenerateCommitMessage()
34+
stagingCode := util.ExecGetStagingCode()
35+
messages, err := ms.GenerateCommitMessage(stagingCode)
3636
if err != nil {
3737
return generateMessages{errorMsg: "メッセージの生成に失敗: " + err.Error()}
3838
}

doc/command_list.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## コマンドリスト
2+
3+
### コマンド
4+
- `suggest`:コミットメッセージの生成
5+
- `config`:各種の設定
6+
- 選択肢を出して設定項目を選んでもらう
7+
- 設定項目はAPIキー、日本語/英語、など
8+
9+
### フラッグ
10+
- `--help` `-h`:CLIのコマンドとオプション一覧を表示
11+
- `--docs` `-d`:コミットメッセージに関するドキュメント
12+
- `--version` `-v`:アプリのバージョン表示

go.mod

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ module github.com/cocoide/commitify
33
go 1.19
44

55
require (
6+
github.com/charmbracelet/bubbles v0.16.1
67
github.com/charmbracelet/bubbletea v0.24.2
78
github.com/fatih/color v1.15.0
8-
github.com/joho/godotenv v1.5.1
9+
github.com/golang/mock v1.4.4
910
github.com/sashabaranov/go-openai v1.15.1
1011
github.com/spf13/cobra v1.7.0
1112
github.com/spf13/viper v1.16.0
13+
google.golang.org/grpc v1.55.0
14+
google.golang.org/protobuf v1.30.0
1215
)
1316

1417
require (
@@ -29,9 +32,11 @@ require (
2932
)
3033

3134
require (
35+
github.com/atotto/clipboard v0.1.4 // indirect
3236
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
37+
github.com/charmbracelet/lipgloss v0.7.1 // indirect
3338
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
34-
github.com/inconshreveable/mousetrap v1.1.0 // indirect
39+
github.com/golang/protobuf v1.5.3 // indirect
3540
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
3641
github.com/mattn/go-isatty v0.0.18 // indirect
3742
github.com/mattn/go-localereader v0.0.1 // indirect
@@ -41,9 +46,10 @@ require (
4146
github.com/muesli/reflow v0.3.0 // indirect
4247
github.com/muesli/termenv v0.15.1 // indirect
4348
github.com/rivo/uniseg v0.2.0 // indirect
44-
github.com/spf13/pflag v1.0.5 // indirect
49+
golang.org/x/net v0.10.0 // indirect
4550
golang.org/x/sync v0.1.0 // indirect
4651
golang.org/x/sys v0.8.0 // indirect
47-
golang.org/x/term v0.6.0 // indirect
52+
golang.org/x/term v0.8.0 // indirect
4853
golang.org/x/text v0.9.0 // indirect
54+
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
4955
)

0 commit comments

Comments
 (0)