Skip to content

Commit

Permalink
refactor: Refactor prompt and item packages
Browse files Browse the repository at this point in the history
  • Loading branch information
HRemonen committed Dec 6, 2023
1 parent 29e1644 commit f7649a2
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 63 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
dist/

.commitsense.*
commitsense
commitsense

asdfasdf
12 changes: 6 additions & 6 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ var commitCmd = &cobra.Command{
os.Exit(1)
}

commitType, err := commit.PromptCommitType(csprompt.CSPrompt{
commitType, err := commit.PromptCommitType(csprompt.Prompt{
Label: "Select a commit type",
})
if err != nil {
colorprinter.ColorPrint("error", "Error prompting for the commit type: %v", err)
os.Exit(1)
}

commitScope, err := commit.PromptForString(csprompt.CSPrompt{
commitScope, err := commit.PromptForString(csprompt.Prompt{
Label: "Enter a commit scope (optional)",
})
if err != nil {
colorprinter.ColorPrint("error", "Error prompting for the commit scope: %v", err)
os.Exit(1)
}

commitDescription, err := commit.PromptForString(csprompt.CSPrompt{
commitDescription, err := commit.PromptForString(csprompt.Prompt{
Label: "Enter a brief commit description",
Validate: validators.ValidateStringNotEmpty,
})
Expand All @@ -65,7 +65,7 @@ var commitCmd = &cobra.Command{
os.Exit(1)
}

commitBody, err := commit.PromptForMultilineString(csprompt.CSPrompt{
commitBody, err := commit.PromptForMultilineString(csprompt.Prompt{
Label: "Enter a detailed commit body (press Enter twice to finish)",
})
if err != nil {
Expand All @@ -75,7 +75,7 @@ var commitCmd = &cobra.Command{

var coAuthors []string
if isCoAuthored {
coAuthors, err = commit.PromptForCoAuthors(csprompt.CSPrompt{
coAuthors, err = commit.PromptForCoAuthors(csprompt.Prompt{
Label: "Enter Co-Author information ",
})
if err != nil {
Expand All @@ -86,7 +86,7 @@ var commitCmd = &cobra.Command{

var breakingChangeDescription string
if isBreakingChange {
breakingChangeDescription, err = commit.PromptForString(csprompt.CSPrompt{
breakingChangeDescription, err = commit.PromptForString(csprompt.Prompt{
Label: "Enter a description of the breaking change",
})
if err != nil {
Expand Down
21 changes: 10 additions & 11 deletions pkg/commit/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package commit
import (
"commitsense/pkg/author"
"commitsense/pkg/config"
"commitsense/pkg/item"
"fmt"
"os"
"strings"
Expand All @@ -22,7 +21,7 @@ import (
)

// PromptCommitType prompts the user to select a commit type.
func PromptCommitType(prompt csprompt.CSPrompt) (string, error) {
func PromptCommitType(prompt csprompt.Prompt) (string, error) {
config, _ := config.ReadConfigFile()

promptType := promptui.Select{
Expand All @@ -36,7 +35,7 @@ func PromptCommitType(prompt csprompt.CSPrompt) (string, error) {
}

// PromptForBool prompts the user to enter a boolean value.
func PromptForBool(prompt csprompt.CSPrompt) (bool, error) {
func PromptForBool(prompt csprompt.Prompt) (bool, error) {
promptBool := promptui.Prompt{
Label: prompt.Label,
Validate: prompt.Validate,
Expand All @@ -52,7 +51,7 @@ func PromptForBool(prompt csprompt.CSPrompt) (bool, error) {
}

// PromptForString prompts the user to enter a string.
func PromptForString(prompt csprompt.CSPrompt) (string, error) {
func PromptForString(prompt csprompt.Prompt) (string, error) {
promptString := promptui.Prompt{
Label: prompt.Label,
Validate: prompt.Validate,
Expand All @@ -63,7 +62,7 @@ func PromptForString(prompt csprompt.CSPrompt) (string, error) {

// PromptForMultilineString prompts the user for a multiline string input based on the provided prompt configuration.
// Users can enter multiple lines of text until they press Enter twice to finish.
func PromptForMultilineString(prompt csprompt.CSPrompt) (string, error) {
func PromptForMultilineString(prompt csprompt.Prompt) (string, error) {
var lines []string
for {
line, err := PromptForString(prompt)
Expand All @@ -78,10 +77,10 @@ func PromptForMultilineString(prompt csprompt.CSPrompt) (string, error) {
}

// Prepend the prompt.Items with the continue item
func prependItemsWithSpecialOptions(items []*item.Item) []*item.Item {
continueItem := &item.Item{ID: "Continue"}
func prependItemsWithSpecialOptions(items []*csprompt.Item) []*csprompt.Item {
continueItem := &csprompt.Item{ID: "Continue"}

items = append([]*item.Item{continueItem}, items...)
items = append([]*csprompt.Item{continueItem}, items...)

return items
}
Expand All @@ -94,7 +93,7 @@ func createSelectTemplates() *promptui.SelectTemplates {
}
}

func promptForMultipleItems(prompt csprompt.CSPrompt) ([]*item.Item, error) {
func promptForMultipleItems(prompt csprompt.Prompt) ([]*csprompt.Item, error) {
promptItems := prependItemsWithSpecialOptions(prompt.Items)

promptMultiple := promptui.Select{
Expand All @@ -106,7 +105,7 @@ func promptForMultipleItems(prompt csprompt.CSPrompt) ([]*item.Item, error) {
HideSelected: true,
}

var selectedItems []*item.Item
var selectedItems []*csprompt.Item

for {
selectionIdx, _, err := promptMultiple.Run()
Expand Down Expand Up @@ -154,7 +153,7 @@ func coAuthorCompleter(suggestedCoAuthors []string) goprompt.Completer { // Use
// This function provides real-time auto-completion suggestions based on the suggestedCoAuthors
// list. Users can choose from the suggestions or enter custom co-authors. It returns a slice
// of selected co-author names.
func PromptForCoAuthors(prompt csprompt.CSPrompt) ([]string, error) {
func PromptForCoAuthors(prompt csprompt.Prompt) ([]string, error) {
suggestedCoAuthors, err := author.GetSuggestedCoAuthors()
if err != nil {
fmt.Println("Error getting the suggested co-authors:", err)
Expand Down
20 changes: 0 additions & 20 deletions pkg/item/item.go

This file was deleted.

25 changes: 0 additions & 25 deletions pkg/prompt/csprompt.go

This file was deleted.

14 changes: 14 additions & 0 deletions pkg/prompt/item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*

Check warning on line 1 in pkg/prompt/item.go

View workflow job for this annotation

GitHub Actions / lint

package-comments: package comment should be of the form "Package prompt ..." (revive)
Package item provides a struct for representing prompt items.
For more information on how to use the Item struct and its associated functionality, refer to the package-specific functions and commands.
Copyright © 2023 HENRI REMONEN <henri@remonen.fi>
*/
package prompt

// Item represents an item with an ID referring to a certain item in a multiselect prompt
type Item struct {
ID string
IsSelected bool
}
23 changes: 23 additions & 0 deletions pkg/prompt/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Package prompt provides a struct for defining promptui prompts in CommitSense.
The prompt struct in this package represents a prompt object that can be used with the promptui library. It includes fields for the prompt's label, validation function, and default value.
Usage:
- Create a prompt object to define custom prompts for user input.
- Use the prompt object in your CommitSense commands for interactive prompts.
For more information on how to use prompts with CommitSense, refer to the package-specific functions and commands.
Copyright © 2023 HENRI REMONEN <henri@remonen.fi>
*/
package prompt

// prompt represents a promptui prompt object used for user input.

Check warning on line 16 in pkg/prompt/prompt.go

View workflow job for this annotation

GitHub Actions / lint

exported: comment on exported type Prompt should be of the form "Prompt ..." (with optional leading article) (revive)
type Prompt struct {
Label string
Items []*Item
CursorPos int
Validate func(string) error
Default string
}

0 comments on commit f7649a2

Please sign in to comment.