-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(CoAuthor): Refactor multiple selection to take prompt object…
… as dependency Co-authored-by: Henri Remonen <[email protected]> Co-authored-by: Henri Remonen <[email protected]>
- Loading branch information
Showing
5 changed files
with
26 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -76,17 +77,23 @@ var commitCmd = &cobra.Command{ | |
|
||
var coAuthors []string | ||
if isCoAuthored { | ||
suggestedCoAuthors, err := author.GetSuggestedCoAuthors() | ||
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, | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ Copyright © 2023 HENRI REMONEN <[email protected]> | |
package commit | ||
|
||
import ( | ||
"commitsense/pkg/author" | ||
"commitsense/pkg/item" | ||
"commitsense/pkg/prompt" | ||
"fmt" | ||
|
@@ -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{ | ||
|
@@ -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) | ||
} | ||
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} |