Skip to content

Commit

Permalink
fix: improve bumpver ux
Browse files Browse the repository at this point in the history
  • Loading branch information
majori committed Nov 13, 2024
1 parent f35312f commit 5d831ec
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 192 deletions.
21 changes: 11 additions & 10 deletions internal/cli/bumpver.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,31 @@ func runBumpVer(cmd *cobra.Command, opts bumpVerOpts) error {
return err
}

changelog, err := changelog.RunChangelog()
changelogModel, err := changelog.RunChangelog(cmd.InOrStdin(), cmd.OutOrStdout())
if err != nil {
return err
}

switch changelog.Increment {
case "patch":
switch changelogModel.Increment {
case changelog.Patch:
newVer = currentVer.IncPatch()
case "minor":
case changelog.Minor:
newVer = currentVer.IncMinor()
case "major":
case changelog.Major:
newVer = currentVer.IncMajor()
}

changelogMsg = changelog.Msg
changelogMsg = changelogModel.Msg

} else {
optVer, err := semver.NewVersion(opts.RecipeVersion)
if err != nil {
if errors.Is(err, semver.ErrInvalidSemVer) {
switch {
case errors.Is(err, semver.ErrInvalidSemVer):
return fmt.Errorf("provided version is not valid semver: %s", opts.RecipeVersion)
default:
return err
}
return err
}

newVer = *optVer
Expand All @@ -115,8 +117,7 @@ func runBumpVer(cmd *cobra.Command, opts bumpVerOpts) error {
return err
}

cmd.Printf("bumped version: %s => %s \n", prevVer, newVerWithPrefix)
cmd.Printf("with changelog message: %s \n", changelogMsg)
cmd.Printf("Recipe version bumped: %s => %s \n", prevVer, newVerWithPrefix)

return nil
}
42 changes: 30 additions & 12 deletions pkg/ui/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@ package changelog
import (
"errors"
"fmt"
"io"

tea "github.com/charmbracelet/bubbletea"
changelog "github.com/futurice/jalapeno/pkg/ui/changelog/prompt"
"github.com/futurice/jalapeno/pkg/ui/util"
)

const (
Patch = "patch"
Minor = "minor"
Major = "major"
)

type Changelog struct {
Increment string
Msg string
}

func RunChangelog() (Changelog, error) {
verInc, err := runSelectPrompt()
func RunChangelog(in io.Reader, out io.Writer) (Changelog, error) {
verInc, err := runVersionPrompt(in, out)

if err != nil {
return Changelog{}, fmt.Errorf("failed to get version type: %w", err)
}

logmsg, err := runTextAreaPrompt()
logmsg, err := runMessagePrompt(in, out)

if err != nil {
return Changelog{}, fmt.Errorf("failed to get log message: %w", err)
Expand All @@ -34,32 +41,43 @@ func RunChangelog() (Changelog, error) {
return changelog, nil
}

func runSelectPrompt() (string, error) {
options := []string{"patch", "minor", "major"}
func runVersionPrompt(in io.Reader, out io.Writer) (string, error) {
options := []string{Patch, Minor, Major}

p := tea.NewProgram(changelog.NewSelectModel(options))
p := tea.NewProgram(NewSelectModel(options), tea.WithInput(in), tea.WithOutput(out))

if m, err := p.Run(); err != nil {
return "", err
} else {
sel, ok := m.(changelog.SelectModel)
sel, ok := m.(VersionModel)
if !ok {
return "", errors.New("internal error: unexpected model type")
}
return sel.Value(), nil
value := sel.Value()
if value == "" {
return "", util.ErrUserAborted
}

return value, nil
}
}

func runTextAreaPrompt() (string, error) {
p := tea.NewProgram(changelog.NewStringModel())
func runMessagePrompt(in io.Reader, out io.Writer) (string, error) {
p := tea.NewProgram(NewStringModel(), tea.WithInput(in), tea.WithOutput(out))

if m, err := p.Run(); err != nil {
return "", err
} else {
txt, ok := m.(changelog.StringModel)
txt, ok := m.(MessageModel)
if !ok {
return "", errors.New("internal error: unexpected model type")
}

value := txt.Value()
if value == "" {
return "", util.ErrUserAborted
}

return txt.Value(), nil
}
}
93 changes: 93 additions & 0 deletions pkg/ui/changelog/message_prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package changelog

import (
"errors"
"fmt"
"strings"

"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/futurice/jalapeno/pkg/ui/colors"
"github.com/muesli/reflow/wordwrap"
)

type MessageModel struct {
textArea textarea.Model
width int
err error
}

var _ tea.Model = MessageModel{}

func NewStringModel() MessageModel {
ti := textarea.New()
ti.Focus()
ti.SetHeight(5)
ti.CharLimit = 156

return MessageModel{
textArea: ti,
err: nil,
}
}

func (m MessageModel) Init() tea.Cmd {
return textinput.Blink
}

func (m MessageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
case tea.KeyCtrlS:
m.err = m.Validate()
if m.err == nil {
return m, tea.Quit
}
return m, nil
}

case tea.WindowSizeMsg:
m.width = msg.Width
m.textArea.SetWidth(m.width)
}

m.textArea, cmd = m.textArea.Update(msg)
return m, cmd
}

func (m MessageModel) View() string {
var s strings.Builder

s.WriteString(wordwrap.String("Write the changelog message for the new version", m.width))
s.WriteString("\nPress Ctrl+S to save")
s.WriteRune('\n')

s.WriteString(m.textArea.View())

if m.err != nil {
s.WriteString("\n\n")
s.WriteString(colors.Red.Render(fmt.Sprintf("Error: %s", m.err.Error())))
}

s.WriteString("\n\n")

return s.String()
}

func (m MessageModel) Value() string {
return strings.TrimSpace(m.textArea.Value())
}

func (m MessageModel) Validate() error {
if m.textArea.Value() == "" {
return errors.New("changelog message cannot be empty")
}

return nil
}
124 changes: 0 additions & 124 deletions pkg/ui/changelog/prompt/textarea.go

This file was deleted.

Loading

0 comments on commit 5d831ec

Please sign in to comment.