Skip to content

プロジェクトのファイル構成を修正 #58

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 7 commits into from
Nov 6, 2023
Prev Previous commit
Next Next commit
fix: モデルの名前をsuggestModelに変更
  • Loading branch information
mochi-yu committed Oct 4, 2023
commit c27f677ed942f615a30a9af52d8c8ad25b5895f7
87 changes: 43 additions & 44 deletions cmd/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
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
Expand All @@ -33,7 +32,7 @@ type model struct {
textInput textinput.Model
}

func (m *model) Init() tea.Cmd {
func (sm *suggestModel) Init() tea.Cmd {
conf, err := entity.ReadConfig()
if err != nil {
log.Fatal("設定情報の取得に失敗: ", err)
Expand All @@ -53,79 +52,79 @@ func (m *model) Init() tea.Cmd {
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 := util.ExecCommitMessage(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 (sm *suggestModel) resetSpinner() {
sm.spinner = spinner.New()
sm.spinner.Style = spinnerStyle
sm.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,7 +133,7 @@ func (m *model) View() string {
return b.String()
}

func initialModel() model {
func initialModel() suggestModel {
ti := textinput.New()
ti.Focus()

Expand All @@ -153,9 +152,9 @@ var suggestCmd = &cobra.Command{
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 := initialModel()
sm.resetSpinner()
p := tea.NewProgram(&sm)
p.Run()
},
}
Expand Down