Skip to content

Commit

Permalink
chore 🧹(huh): cleaning prompts
Browse files Browse the repository at this point in the history
Signed-off-by: moualhi zine el abidine <zmoualhi@outlook.com>
  • Loading branch information
muandane committed Aug 27, 2024
1 parent dd36433 commit 5128cea
Showing 1 changed file with 61 additions and 91 deletions.
152 changes: 61 additions & 91 deletions pkg/utils/askQuestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,24 @@ import (
"fmt"
"strings"

"github.com/rs/zerolog/log"

"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"github.com/muandane/goji/pkg/config"
)

func AskQuestions(config *config.Config, presetType, presetMessage string) ([]string, error) {
var commitType string
var commitScope string
var commitSubject string
var commitDescription string
commitTypeOptions := make([]huh.Option[string], len(config.Types))
var form huh.Form
nameStyle := lipgloss.NewStyle().
Width(15).
Align(lipgloss.Left)

emojiStyle := lipgloss.NewStyle().
Width(5).
PaddingRight(5).
Align(lipgloss.Left)
var commitType, commitScope, commitSubject, commitDescription string

descStyle := lipgloss.NewStyle().
Width(45).
Align(lipgloss.Left)
nameStyle := lipgloss.NewStyle().Width(15).Align(lipgloss.Left)
emojiStyle := lipgloss.NewStyle().Width(5).PaddingRight(5).Align(lipgloss.Left)
descStyle := lipgloss.NewStyle().Width(45).Align(lipgloss.Left)

commitTypeOptions := make([]huh.Option[string], len(config.Types))
for i, ct := range config.Types {
name := nameStyle.Render(ct.Name)
emoji := emojiStyle.Render(ct.Emoji)
desc := descStyle.Render(ct.Description)
row := lipgloss.JoinHorizontal(lipgloss.Center, name, emoji, desc)
row := lipgloss.JoinHorizontal(lipgloss.Center,
nameStyle.Render(ct.Name),
emojiStyle.Render(ct.Emoji),
descStyle.Render(ct.Description))
commitTypeOptions[i] = huh.NewOption[string](row, fmt.Sprintf("%s %s", ct.Name, func() string {
if !config.NoEmoji {
return ct.Emoji
Expand All @@ -45,7 +31,6 @@ func AskQuestions(config *config.Config, presetType, presetMessage string) ([]st
}()))
}

// Pre-fill the type if it was provided via flag
if presetType != "" {
for _, option := range commitTypeOptions {
if strings.HasPrefix(option.Value, presetType) {
Expand All @@ -55,79 +40,64 @@ func AskQuestions(config *config.Config, presetType, presetMessage string) ([]st
}
}

// Pre-fill the message if it was provided via flag
form := huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Title("Select the type of change:").
Options(commitTypeOptions...).
Height(8).
Value(&commitType),
huh.NewInput().
Title("Scope of this change (optional):").
Placeholder("e.g., ci, api, parser").
CharLimit(50).
Suggestions(config.Scopes).
Value(&commitScope),
),
huh.NewGroup(
huh.NewInput().
Title("Short summary of changes:").
Placeholder("Short Commit description").
CharLimit(70).
Value(&commitSubject).
Validate(func(s string) error {
if s == "" {
return errors.New("subject cannot be empty")
}
return nil
}),
huh.NewText().
Title("Long description (optional):").
CharLimit(config.SubjectMaxLength).
Placeholder("Longer Commit description").
Value(&commitDescription).
WithHeight(4),
huh.NewConfirm().
Title("Commit changes?").
Affirmative("Yes").
Negative("No").
Validate(func(v bool) error {
if !v {
return errors.New("changes not committed")
}
return nil
}),
),
)

if presetMessage != "" {
commitSubject = presetMessage
}

group1 := huh.NewGroup(
huh.NewSelect[string]().
Title("Select the type of change you are committing:").
Options(commitTypeOptions...).
Height(8).
Value(&commitType),
huh.NewInput().
Title("What is the scope of this change? (class or file name): (press [enter] to skip)").
CharLimit(50).
Suggestions(config.Scopes).
Placeholder("Example: ci, api, parser").
Value(&commitScope),
)

group2 := huh.NewGroup(
huh.NewInput().
Title("Write a short and imperative summary of the code changes: (lower case and no period)").
CharLimit(70).
Placeholder("Short description of your commit").
Value(&commitSubject).
Validate(func(str string) error {
if len(str) == 0 {
return errors.New("sorry, subject can't be empty")
}
return nil
}),
huh.NewText().
Title("Write a Long description of the code changes: (press [enter] to skip)").
CharLimit(config.SubjectMaxLength).
Placeholder("Long description of your commit").
Value(&commitDescription).
WithHeight(4),
huh.NewConfirm().
Key("done").
Title("Commit Changes ?").
Validate(func(v bool) error {
if !v {
return fmt.Errorf("welp, finish up then")
}
return nil
}).
Affirmative("Yes").
Negative("Wait, no"),
)

form = *huh.NewForm(group1, group2)
err := form.Run()
if err != nil {
log.Fatal().Msg(err.Error())
if err := form.Run(); err != nil {
return nil, err
}

var commitMessage string
var commitBody string
switch {
case commitScope != "" && commitDescription != "":
commitMessage = fmt.Sprintf("%s(%s): %s", commitType, commitScope, commitSubject)
commitBody = commitDescription
case commitDescription != "":
commitMessage = fmt.Sprintf("%s: %s", commitType, commitSubject)
commitBody = commitDescription
case commitScope != "":
commitMessage = fmt.Sprintf("%s(%s): %s", commitType, commitScope, commitSubject)
default:
commitMessage = fmt.Sprintf("%s: %s", commitType, commitSubject)
commitMessage := commitType
if commitScope != "" {
commitMessage += fmt.Sprintf("(%s)", commitScope)
}
commitMessage += fmt.Sprintf(": %s", commitSubject)

var result []string
result = append(result, commitMessage, commitBody)

return result, nil
return []string{commitMessage, commitDescription}, nil
}

0 comments on commit 5128cea

Please sign in to comment.