Skip to content

Merge main #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 6, 2023
42 changes: 10 additions & 32 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/cocoide/commitify/internal/entity"
"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/cocoide/commitify/internal/entity"
)

var (
Expand All @@ -27,29 +28,29 @@ var (
}
)

type configModel struct {
type configCmdModel struct {
configKeyIndex int
configOptionIndex int
configKeySelected bool
err error
textInput textinput.Model
}

func initConfigModel() configModel {
func initConfigModel() configCmdModel {
ti := textinput.New()
ti.Focus()

return configModel{
return configCmdModel{
textInput: ti,
err: nil,
}
}

func (cm configModel) Init() tea.Cmd {
func (cm configCmdModel) Init() tea.Cmd {
return textinput.Blink
}

func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (cm configCmdModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch cm.configKeySelected {
// 設定項目を選択する
case false:
Expand Down Expand Up @@ -82,7 +83,7 @@ func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEnter:
saveConfig(cm)
entity.SaveConfig(cm.configKeyIndex, -1, cm.textInput.Value())
return cm, tea.Quit
case tea.KeyCtrlC, tea.KeyEsc:
return cm, tea.Quit
Expand All @@ -109,7 +110,7 @@ func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cm.configOptionIndex++
}
case tea.KeyEnter:
saveConfig(cm)
entity.SaveConfig(cm.configKeyIndex, configOption[cm.configKeyIndex][cm.configOptionIndex], "")
return cm, tea.Quit
case tea.KeyCtrlC, tea.KeyEsc:
return cm, tea.Quit
Expand All @@ -121,7 +122,7 @@ func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return cm, nil
}

func (cm configModel) View() string {
func (cm configCmdModel) View() string {
var b strings.Builder

switch cm.configKeySelected {
Expand Down Expand Up @@ -180,26 +181,3 @@ var configCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(configCmd)
}

func saveConfig(cm configModel) {
currentConfig, err := entity.ReadConfig()
if err != nil {
fmt.Println(err)
}

switch cm.configKeyIndex {
case 0:
currentConfig.ChatGptApiKey = cm.textInput.Value()
case 1:
currentConfig.UseLanguage = configOption[cm.configKeyIndex][cm.configOptionIndex]
case 2:
currentConfig.CommitFormat = configOption[cm.configKeyIndex][cm.configOptionIndex]
case 3:
currentConfig.AISource = configOption[cm.configKeyIndex][cm.configOptionIndex]
}

err = entity.WriteConfig(currentConfig)
if err != nil {
fmt.Println(err)
}
}
4 changes: 3 additions & 1 deletion cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ var docsCmd = &cobra.Command{
Short: "Document of commitify",
Run: func(cmd *cobra.Command, args []string) {
b, _ := static.Logo.ReadFile("logo.txt")
fmt.Print(color.CyanString(string(b)) + "\n\n ・Languageは日本語と英語が選択できます\n\n ・CodeFormatはPrefix (例: feat: A)とEmoji (例: 🐛 Bugix), Normal (例: Feat A)が選べます")
fmt.Println(color.CyanString(string(b)))
fmt.Println("\n ・Languageは日本語と英語が選択できます")
fmt.Println(" ・CodeFormatはPrefix (例: feat: A)とEmoji (例: 🐛 Bugix), Normal (例: Feat A)が選べます")
},
}

Expand Down
124 changes: 60 additions & 64 deletions cmd/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,122 +10,102 @@ import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/cocoide/commitify/internal/entity"
"github.com/cocoide/commitify/internal/gateway"
"github.com/cocoide/commitify/util"
"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/cocoide/commitify/internal/service"
)

var (
textStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("252")).Render
spinnerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("69"))
helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render
)

type model struct {
type suggestModel struct {
choices []string
currentIdx int
errorMsg string
isLoading bool
isEditing bool
spinner spinner.Model
textInput textinput.Model
scs *service.SuggestCmdService
}

func (m *model) Init() tea.Cmd {
conf, err := entity.ReadConfig()
if err != nil {
log.Fatal("設定情報の取得に失敗: ", err)
}

var gi gateway.GatewayInterface
switch conf.AISource {
case int(entity.WrapServer):
gi = gateway.NewGrpcServeGateway()
default:
gi = gateway.NewGrpcServeGateway()
}

func (sm *suggestModel) Init() tea.Cmd {
go func() {
messages, err := gi.FetchCommitMessages()
messages, err := sm.scs.GenerateCommitMessages()
if err != nil {
log.Fatal("コミットメッセージの生成に失敗: ", err)
os.Exit(-1)
}
m.choices = messages
m.isLoading = false
sm.choices = messages
sm.isLoading = false
}()

return textinput.Blink
}

func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (sm *suggestModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.textInput, cmd = m.textInput.Update(msg)
sm.textInput, cmd = sm.textInput.Update(msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyTab:
m.isEditing = true
m.textInput.Focus()
m.textInput.SetValue(m.choices[m.currentIdx])
m.textInput.CharLimit = 100
m.textInput.Width = 100
return m, cmd
sm.isEditing = true
sm.textInput.Focus()
sm.textInput.SetValue(sm.choices[sm.currentIdx])
sm.textInput.CharLimit = 100
sm.textInput.Width = 100
return sm, cmd
case tea.KeyUp:
if m.currentIdx > 0 {
m.currentIdx--
if sm.currentIdx > 0 {
sm.currentIdx--
}
case tea.KeyDown:
if m.currentIdx < len(m.choices)-1 {
m.currentIdx++
if sm.currentIdx < len(sm.choices)-1 {
sm.currentIdx++
}
case tea.KeyEnter:
if err := util.ExecCommitMessage(m.choices[m.currentIdx]); err != nil {
m.errorMsg = "コミットに失敗: " + err.Error()
return m, tea.Quit
if err := sm.scs.SubmitCommit(sm.choices[sm.currentIdx]); err != nil {
sm.errorMsg = "コミットに失敗: " + err.Error()
return sm, tea.Quit
}
return m, tea.Quit
return sm, tea.Quit
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
return sm, tea.Quit
}
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
sm.spinner, cmd = sm.spinner.Update(msg)
return sm, cmd
}
return m, m.spinner.Tick
return sm, sm.spinner.Tick
}

func (m *model) resetSpinner() {
m.spinner = spinner.New()
m.spinner.Style = spinnerStyle
m.spinner.Spinner = spinner.Globe
}

func (m *model) View() string {
if m.errorMsg != "" {
return color.RedString(m.errorMsg)
func (sm *suggestModel) View() string {
if sm.errorMsg != "" {
return color.RedString(sm.errorMsg)
}
if m.isLoading {
s := fmt.Sprintf("\n %s %s\n\n", m.spinner.View(), textStyle("コミットメッセージ生成中"))
if sm.isLoading {
s := fmt.Sprintf("\n %s %s\n\n", sm.spinner.View(), textStyle("コミットメッセージ生成中"))
return s
}
var b strings.Builder
if m.errorMsg != "" {
b.WriteString(color.RedString(m.errorMsg) + "\n\n")
if sm.errorMsg != "" {
b.WriteString(color.RedString(sm.errorMsg) + "\n\n")
}
if m.isEditing {
return m.textInput.View()
if sm.isEditing {
return sm.textInput.View()
}

b.WriteString(color.WhiteString("🍕 Please select and enter to commit"))
b.WriteString(color.WhiteString("\n Use arrow ↑↓ to navigate and press Enter to select."))
b.WriteString(color.WhiteString("\n ( enter Tab key to edit message )\n\n"))

for i, choice := range m.choices {
if i == m.currentIdx {
for i, choice := range sm.choices {
if i == sm.currentIdx {
b.WriteString(fmt.Sprintf(color.HiCyanString("➡️ %s\n"), choice))
} else {
b.WriteString(fmt.Sprintf(color.CyanString(" %s\n"), choice))
Expand All @@ -134,28 +114,44 @@ func (m *model) View() string {
return b.String()
}

func initialModel() model {
// モデルの初期化処理
func NewSuggestModel() *suggestModel {
ti := textinput.New()
ti.Focus()

return model{
// suggestコマンドのサービスの取得
scs, err := service.NewSuggestCmdService()
if err != nil {
log.Fatal(err)
os.Exit(-1)
}

return &suggestModel{
choices: []string{""},
currentIdx: 0,
errorMsg: "",
isLoading: true,
isEditing: false,
textInput: ti,
scs: scs,
}
}

// スピナーの初期化
func (sm *suggestModel) initSpinner() {
sm.spinner = spinner.New()
sm.spinner.Style = spinnerStyle
sm.spinner.Spinner = spinner.Globe
}

var suggestCmd = &cobra.Command{
Use: "suggest",
Short: "Suggestion of commit message for staging repository",
Aliases: []string{"s", "suggest"},
Run: func(cmd *cobra.Command, args []string) {
m := initialModel()
m.resetSpinner()
p := tea.NewProgram(&m)
sm := NewSuggestModel()
sm.initSpinner()
p := tea.NewProgram(sm)
p.Run()
},
}
Expand Down
Loading