Skip to content
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
6 changes: 3 additions & 3 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type GitClient interface {
var _ GitClient = (*git.Client)(nil)

type UIClient interface {
TextArea(value string) (string, bool, error)
TextArea(value string) (string, ui.Action, error)
StartSpinner(message string)
UpdateSpinner(message string)
StopSpinner()
Expand Down Expand Up @@ -97,12 +97,12 @@ func (app *App) Run(ctx context.Context, userMessage string) error {
}
wrapped = strings.ReplaceAll(wrapped, "\n\n\n", "\n\n")
}
edited, accepted, err := app.ui.TextArea(wrapped)
edited, action, err := app.ui.TextArea(wrapped)
if err != nil {
return err
}

if accepted {
if action == ui.Commit {
return app.git.Commit(edited)
} else {
return ErrAborted
Expand Down
13 changes: 7 additions & 6 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/rm-hull/git-commit-summary/internal/ui"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -44,13 +45,13 @@ func (m *mockGitClient) Commit(message string) error {
}

type mockUIClient struct {
TextAreaFunc func(value string) (string, bool, error)
TextAreaFunc func(value string) (string, ui.Action, error)
StartSpinnerFunc func(message string)
UpdateSpinnerFunc func(message string)
StopSpinnerFunc func()
}

func (m *mockUIClient) TextArea(value string) (string, bool, error) {
func (m *mockUIClient) TextArea(value string) (string, ui.Action, error) {
return m.TextAreaFunc(value)
}

Expand Down Expand Up @@ -189,7 +190,7 @@ func TestAppRun(t *testing.T) {
DiffFunc: func() (string, error) { return "diff output", nil },
}
uiClient := &mockUIClient{
TextAreaFunc: func(value string) (string, bool, error) { return "", false, assert.AnError },
TextAreaFunc: func(value string) (string, ui.Action, error) { return "", ui.Abort, assert.AnError },
StartSpinnerFunc: func(message string) {},
UpdateSpinnerFunc: func(message string) {},
StopSpinnerFunc: func() {},
Expand All @@ -211,7 +212,7 @@ func TestAppRun(t *testing.T) {
DiffFunc: func() (string, error) { return "diff output", nil },
}
uiClient := &mockUIClient{
TextAreaFunc: func(value string) (string, bool, error) { return "", false, nil },
TextAreaFunc: func(value string) (string, ui.Action, error) { return "", ui.Abort, nil },
StartSpinnerFunc: func(message string) {},
UpdateSpinnerFunc: func(message string) {},
StopSpinnerFunc: func() {},
Expand All @@ -233,7 +234,7 @@ func TestAppRun(t *testing.T) {
CommitFunc: func(message string) error { return assert.AnError },
}
uiClient := &mockUIClient{
TextAreaFunc: func(value string) (string, bool, error) { return "edited message", true, nil },
TextAreaFunc: func(value string) (string, ui.Action, error) { return "edited message", ui.Commit, nil },
StartSpinnerFunc: func(message string) {},
UpdateSpinnerFunc: func(message string) {},
StopSpinnerFunc: func() {},
Expand All @@ -256,7 +257,7 @@ func TestAppRun(t *testing.T) {
CommitFunc: func(message string) error { return nil },
}
uiClient := &mockUIClient{
TextAreaFunc: func(value string) (string, bool, error) { return "edited message", true, nil },
TextAreaFunc: func(value string) (string, ui.Action, error) { return "edited message", ui.Commit, nil },
StartSpinnerFunc: func(message string) {},
UpdateSpinnerFunc: func(message string) {},
StopSpinnerFunc: func() {},
Expand Down
9 changes: 8 additions & 1 deletion internal/ui/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import (
"github.com/gookit/color"
)

type Action int

const (
Abort Action = iota
Commit
)

type Client struct {
spinner *spinner.Spinner
}
Expand All @@ -17,7 +24,7 @@ func NewClient() *Client {
}
}

func (c *Client) TextArea(value string) (string, bool, error) {
func (c *Client) TextArea(value string) (string, Action, error) {
return textArea(value)
}

Expand Down
18 changes: 9 additions & 9 deletions internal/ui/text_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (
"github.com/charmbracelet/lipgloss"
)

func textArea(value string) (string, bool, error) {
func textArea(value string) (string, Action, error) {
p := tea.NewProgram(initialModel(value))

finalModel, err := p.Run()
if err != nil {
return "", false, err
return "", Abort, err
}
m := finalModel.(model)

return m.Value(), m.Accepted(), nil
return m.Value(), m.Action(), nil
}

type model struct {
textarea textarea.Model
history *History
accepted bool
action Action
err error
}

Expand Down Expand Up @@ -57,7 +57,7 @@ func initialModel(value string) model {
return model{
textarea: ti,
history: NewHistory(value),
accepted: false,
action: Abort,
err: nil,
}
Comment on lines 57 to 62
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Following the suggestion to make Abort the zero value for the Action enum, the explicit initialization of action to Abort becomes redundant. The action field will be implicitly initialized to its zero value.

	return model{
		textarea: ti,
		history:  NewHistory(value),
		err:      nil,
	}

}
Expand All @@ -66,8 +66,8 @@ func (m model) Value() string {
return m.textarea.Value()
}

func (m model) Accepted() bool {
return m.accepted
func (m model) Action() Action {
return m.action
}

func (m model) Init() tea.Cmd {
Expand Down Expand Up @@ -105,12 +105,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil

case tea.KeyEsc:
m.accepted = false
m.action = Abort
m.textarea.Blur()
return m, tea.Quit

case tea.KeyCtrlX:
m.accepted = true
m.action = Commit
m.textarea.Blur()
return m, tea.Quit

Expand Down
Loading