-
-
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.
Merge pull request #42 from evilmarty/prompt
Include command and input descriptions in prompt
- Loading branch information
Showing
2 changed files
with
108 additions
and
60 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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"unicode/utf8" | ||
|
||
"github.com/erikgeiser/promptkit/selection" | ||
"github.com/erikgeiser/promptkit/textinput" | ||
"github.com/muesli/termenv" | ||
) | ||
|
||
const ( | ||
maxPadSpacing = 5 | ||
minChoiceFiltering = 5 | ||
accentColor = termenv.ANSI256Color(32) | ||
) | ||
|
||
func selectCommand(command ConfigCommand) (ConfigCommand, error) { | ||
commandsLength := len(command.Commands) | ||
maxNameLength := maxStringLength(command.Commands, func(c ConfigCommand) string { | ||
return c.Name | ||
}) | ||
|
||
sp := selection.New("Choose command", command.Commands) | ||
sp.SelectedChoiceStyle = func(c *selection.Choice[ConfigCommand]) string { | ||
return renderChoiceStyle(c.Value.Name, c.Value.Description, maxNameLength, true) | ||
} | ||
sp.UnselectedChoiceStyle = func(c *selection.Choice[ConfigCommand]) string { | ||
return renderChoiceStyle(c.Value.Name, c.Value.Description, maxNameLength, false) | ||
} | ||
|
||
if commandsLength <= minChoiceFiltering { | ||
sp.Filter = nil | ||
} | ||
|
||
if choice, err := sp.RunPrompt(); err != nil { | ||
return command, err | ||
} else { | ||
return choice, nil | ||
} | ||
} | ||
|
||
func askInput(input ConfigInput) (string, error) { | ||
if input.Selectable() { | ||
return selectInput(input) | ||
} else { | ||
return getInput(input) | ||
} | ||
} | ||
|
||
func selectInput(input ConfigInput) (string, error) { | ||
prompt := input.Description | ||
if prompt == "" { | ||
prompt = fmt.Sprintf("Choose a %s", termenv.String(input.Name).Underline().String()) | ||
} | ||
sp := selection.New(prompt, input.Options) | ||
|
||
if len(input.Options) <= minChoiceFiltering { | ||
sp.Filter = nil | ||
} | ||
|
||
if option, err := sp.RunPrompt(); err == nil { | ||
return option.Value, err | ||
} else { | ||
return "", err | ||
} | ||
} | ||
|
||
func getInput(input ConfigInput) (string, error) { | ||
prompt := input.Description | ||
if prompt == "" { | ||
prompt = fmt.Sprintf("Please specify a %s", termenv.String(input.Name).Underline().String()) | ||
} | ||
ti := textinput.New(prompt) | ||
ti.InitialValue = input.DefaultValue | ||
ti.Validate = func(value string) error { | ||
if input.Valid(value) { | ||
return nil | ||
} else { | ||
return fmt.Errorf("invalid value") | ||
} | ||
} | ||
return ti.RunPrompt() | ||
} | ||
|
||
func renderChoiceStyle(name, desc string, maxNameLength int, selected bool) string { | ||
padLen := maxNameLength - utf8.RuneCountInString(name) | ||
if padLen < 0 { | ||
padLen = 0 | ||
} | ||
if selected { | ||
name = termenv.String(name).Foreground(accentColor).Bold().String() | ||
} | ||
if desc != "" { | ||
desc = strings.Repeat(" ", padLen+maxPadSpacing) + termenv.String(desc).Faint().String() | ||
} | ||
return name + desc | ||
} | ||
|
||
func maxStringLength[T any](items []T, fun func(T) string) int { | ||
maxLen := 0 | ||
for _, item := range items { | ||
s := fun(item) | ||
maxLen = max(maxLen, utf8.RuneCountInString(s)) | ||
} | ||
return maxLen | ||
} |
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