Skip to content

Commit

Permalink
refactor: Update variable names and rename imported go-prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
HRemonen committed Sep 18, 2023
1 parent 46b177e commit 45f7e9d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 48 deletions.
14 changes: 7 additions & 7 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package cmd

import (
"commitsense/pkg/commit"
"commitsense/pkg/prompt"
csprompt "commitsense/pkg/prompt"
"commitsense/pkg/validators"
"fmt"
"os"
Expand All @@ -39,23 +39,23 @@ var commitCmd = &cobra.Command{
os.Exit(1)
}

commitType, err := commit.PromptCommitType(prompt.Prompt{
commitType, err := commit.PromptCommitType(csprompt.CSPrompt{
Label: "Select a commit type",
})
if err != nil {
fmt.Println("Error prompting for the commit type: ", err)
os.Exit(1)
}

commitScope, err := commit.PromptForString(prompt.Prompt{
commitScope, err := commit.PromptForString(csprompt.CSPrompt{
Label: "Enter a commit scope (optional)",
})
if err != nil {
fmt.Println("Error prompting for the commit scope:", err)
os.Exit(1)
}

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

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

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

var breakingChangeDescription string
if isBreakingChange {
breakingChangeDescription, err = commit.PromptForString(prompt.Prompt{
breakingChangeDescription, err = commit.PromptForString(csprompt.CSPrompt{
Label: "Enter a description of the breaking change",
})
if err != nil {
Expand Down
Binary file modified commitsense
Binary file not shown.
32 changes: 16 additions & 16 deletions pkg/commit/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
"os"
"strings"

p "github.com/c-bata/go-prompt"
goprompt "github.com/c-bata/go-prompt"
"github.com/manifoldco/promptui"
)

// PromptCommitType prompts the user to select a commit type.
func PromptCommitType(prompt prompt.Prompt) (string, error) {
func PromptCommitType(prompt csprompt.CSPrompt) (string, error) {
promptItems := []string{"feat", "fix", "chore", "docs", "style", "refactor", "perf", "test", "build", "ci"}

promptType := promptui.Select{
Expand All @@ -34,7 +34,7 @@ func PromptCommitType(prompt prompt.Prompt) (string, error) {
}

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

// PromptForString prompts the user to enter a string.
func PromptForString(prompt prompt.Prompt) (string, error) {
func PromptForString(prompt csprompt.CSPrompt) (string, error) {
promptString := promptui.Prompt{
Label: prompt.Label,
Validate: prompt.Validate,
Expand All @@ -61,7 +61,7 @@ func PromptForString(prompt prompt.Prompt) (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 prompt.Prompt) (string, error) {
func PromptForMultilineString(prompt csprompt.CSPrompt) (string, error) {
var lines []string
for {
line, err := PromptForString(prompt)
Expand Down Expand Up @@ -92,7 +92,7 @@ func createSelectTemplates() *promptui.SelectTemplates {
}
}

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

promptMultiple := promptui.Select{
Expand Down Expand Up @@ -134,16 +134,16 @@ func promptForMultipleItems(prompt prompt.Prompt) ([]*item.Item, error) {
return selectedItems, nil
}

func coAuthorCompleter(suggestedCoAuthors []string) p.Completer { // Use "p" as the alias
return func(d p.Document) []p.Suggest {
s := []p.Suggest{}
t := d.TextBeforeCursor()
func coAuthorCompleter(suggestedCoAuthors []string) goprompt.Completer { // Use "p" as the alias
return func(d goprompt.Document) []goprompt.Suggest {
suggestions := []goprompt.Suggest{}
text := d.TextBeforeCursor()
for _, coAuthor := range suggestedCoAuthors {
if strings.HasPrefix(coAuthor, t) {
s = append(s, p.Suggest{Text: coAuthor})
if strings.HasPrefix(coAuthor, text) {
suggestions = append(suggestions, goprompt.Suggest{Text: coAuthor})
}
}
return p.FilterHasPrefix(s, t, true)
return goprompt.FilterHasPrefix(suggestions, text, true)
}
}

Expand All @@ -152,7 +152,7 @@ func coAuthorCompleter(suggestedCoAuthors []string) p.Completer { // Use "p" as
// 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 prompt.Prompt) ([]string, error) {
func PromptForCoAuthors(prompt csprompt.CSPrompt) ([]string, error) {
suggestedCoAuthors, err := author.GetSuggestedCoAuthors()
if err != nil {
fmt.Println("Error getting the suggested co-authors:", err)
Expand All @@ -162,10 +162,10 @@ func PromptForCoAuthors(prompt prompt.Prompt) ([]string, error) {
fmt.Println("Enter Co-authors:")
fmt.Println("Press 'Tab' to auto-complete.")

pr := p.New(
pr := goprompt.New(
func(_ string) { /* No-op executor */ },
coAuthorCompleter(suggestedCoAuthors),
p.OptionPrefix(prompt.Label),
goprompt.OptionPrefix(prompt.Label),
)

coAuthors := []string{}
Expand Down
26 changes: 26 additions & 0 deletions pkg/prompt/csprompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Package csprompt provides a struct for defining promptui prompts in CommitSense.
The CSPrompt 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 CSPrompt object to define custom prompts for user input.
- Use the CSPrompt 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 <[email protected]>
*/
package csprompt

import "commitsense/pkg/item"

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

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

This file was deleted.

0 comments on commit 45f7e9d

Please sign in to comment.