Skip to content

Feature/loading #49

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
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions cmd/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ import (
"os"
"strings"

"github.com/charmbracelet/bubbles/spinner"
"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"
)

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 {
choices []string
currentIdx int
errorMsg string
isLoading bool
isEditing bool
textInput textinput.Model
isEditing bool
spinner spinner.Model
textInput textinput.Model
}

func (m *model) Init() tea.Cmd {
Expand All @@ -38,13 +47,16 @@ func (m *model) Init() tea.Cmd {
gi = gateway.NewGrpcServeGateway()
}

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

return textinput.Blink
}

Expand Down Expand Up @@ -78,29 +90,39 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
}
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
return m, nil
return m, m.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)
}
if m.isLoading {
return "🌎 Generating commit messages ..."
s := fmt.Sprintf("\n %s %s\n\n", m.spinner.View(), textStyle("Generating commit messages..."))
return s
}
var b strings.Builder
if m.errorMsg != "" {
b.WriteString(color.RedString(m.errorMsg) + "\n\n")
}
if m.isEditing{
if m.isEditing {
return m.textInput.View()
}

b.WriteString(color.WhiteString("🍕Please select an option:"))
b.WriteString(color.WhiteString("\n Use arrow ↑↓ to navigate and press Enter to select.\n\n"))


for i, choice := range m.choices {
if i == m.currentIdx {
b.WriteString(fmt.Sprintf(color.HiCyanString("➡️ %s\n"), choice))
Expand All @@ -116,22 +138,22 @@ func initialModel() model {
ti.Focus()

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


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)
p.Run()
},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/lipgloss v0.7.1
github.com/fatih/color v1.15.0
github.com/golang/mock v1.4.4
github.com/sashabaranov/go-openai v1.15.1
Expand Down