Skip to content

Commit

Permalink
refactor(CoAuthor): Refactor multiple selection to take prompt object…
Browse files Browse the repository at this point in the history
… as dependency

Co-authored-by: Henri Remonen <[email protected]>
Co-authored-by: Henri Remonen <[email protected]>
  • Loading branch information
HRemonen and HRemonen committed Sep 9, 2023
1 parent c43c0ca commit ff26a73
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
13 changes: 10 additions & 3 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Copyright © 2023 HENRI REMONEN <[email protected]>
package cmd

import (
"commitsense/pkg/author"
"commitsense/pkg/commit"
"commitsense/pkg/prompt"
"commitsense/pkg/validators"
Expand Down Expand Up @@ -76,17 +77,23 @@ var commitCmd = &cobra.Command{

var coAuthors []string
if isCoAuthored {
suggestedCoAuthors, err := author.GetSuggestedCoAuthors()

Check failure on line 80 in cmd/commit.go

View workflow job for this annotation

GitHub Actions / lint

shadow: declaration of "err" shadows declaration at line 32 (govet)
if err != nil {
fmt.Println("Prompt failed:", err)
os.Exit(1)
}

coAuthors, err = commit.PromptForCoAuthors(prompt.Prompt{
Label: "Select authors that are involded",
Label: "Select authors that are involded",
Items: suggestedCoAuthors,
CursorPos: 0,
})
if err != nil {
fmt.Println("Prompt failed:", err)
os.Exit(1)
}
}

fmt.Println(coAuthors)

isBreakingChange, err := commit.PromptForBool(prompt.Prompt{
Label: "Is this a breaking change?",
Validate: validators.ValidateStringYesNo,
Expand Down
Binary file modified commitsense
Binary file not shown.
1 change: 0 additions & 1 deletion pkg/author/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@ func GetSuggestedCoAuthors() ([]*item.Item, error) {
suggestedCoAuthors = append(suggestedCoAuthors, items...)
}


return suggestedCoAuthors, nil
}
23 changes: 9 additions & 14 deletions pkg/commit/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Copyright © 2023 HENRI REMONEN <[email protected]>
package commit

import (
"commitsense/pkg/author"
"commitsense/pkg/item"
"commitsense/pkg/prompt"
"fmt"
Expand Down Expand Up @@ -69,8 +68,9 @@ func PromptForMultilineString(prompt prompt.Prompt) (string, error) {
return strings.Join(lines, "\n"), nil
}

func promptForMultiple(selectedPos int, allItems []*item.Item) ([]*item.Item, error) {
func promptForMultiple(prompt prompt.Prompt) ([]*item.Item, error) {
const continueItem = "Continue"
allItems := prompt.Items

if len(allItems) > 0 && allItems[0].ID != continueItem {
items := []*item.Item{
Expand All @@ -88,16 +88,16 @@ func promptForMultiple(selectedPos int, allItems []*item.Item) ([]*item.Item, er
Inactive: "{{if .IsSelected }}✔ {{ .ID | green }} {{else}}{{ .ID | faint }}{{end}} ",
}

prompt := promptui.Select{
Label: "Select multiple",
promptMultiple := promptui.Select{
Label: prompt.Label,
Items: allItems,
Templates: templates,
Size: 10,
CursorPos: selectedPos,
CursorPos: prompt.CursorPos,
HideSelected: true,
}

selectionIdx, _, err := prompt.Run()
selectionIdx, _, err := promptMultiple.Run()
if err != nil {
return nil, fmt.Errorf("prompt failed: %w", err)
}
Expand All @@ -108,8 +108,8 @@ func promptForMultiple(selectedPos int, allItems []*item.Item) ([]*item.Item, er
// If the user selected something other than "Continue",
// toggle selection on this item and run the function again.
chosenItem.IsSelected = !chosenItem.IsSelected

return promptForMultiple(selectionIdx, allItems)
prompt.CursorPos = selectionIdx
return promptForMultiple(prompt)
}

var selectedItems []*item.Item
Expand All @@ -127,12 +127,7 @@ func promptForMultiple(selectedPos int, allItems []*item.Item) ([]*item.Item, er
// from the author package. It then presents the user with a selectable list of suggested co-authors
// and allows them to choose from the suggestions or enter custom co-authors.
func PromptForCoAuthors(prompt prompt.Prompt) ([]string, error) {
suggestedCoAuthors, err := author.GetSuggestedCoAuthors()
if err != nil {
return nil, err
}

selectedAuthorPtrs, err := promptForMultiple(0, suggestedCoAuthors)
selectedAuthorPtrs, err := promptForMultiple(prompt)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ Copyright © 2023 HENRI REMONEN <[email protected]>
*/
package prompt

import "commitsense/pkg/item"

// Prompt represents a promptui prompt object used for user input.
type Prompt struct {
Label string
Validate func(string) error
Default string
Label string
Items []*item.Item
CursorPos int
Validate func(string) error
Default string
}

0 comments on commit ff26a73

Please sign in to comment.