Skip to content

Commit

Permalink
Merge pull request #42 from evilmarty/prompt
Browse files Browse the repository at this point in the history
Include command and input descriptions in prompt
  • Loading branch information
evilmarty authored Aug 3, 2024
2 parents e0c87a3 + fca6661 commit e09c4a6
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 60 deletions.
108 changes: 108 additions & 0 deletions prompt.go
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
}
60 changes: 0 additions & 60 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@ import (
"flag"
"fmt"
"os"

"github.com/erikgeiser/promptkit/selection"
"github.com/erikgeiser/promptkit/textinput"
// "os/exec"
)

const (
minChoiceFiltering = 5
)

type Runner struct {
Config Config
Args []string
Expand Down Expand Up @@ -93,56 +86,3 @@ func NewRunner(config Config, args []string) *Runner {
}
return &r
}

func selectCommand(command ConfigCommand) (ConfigCommand, error) {
commandsLength := len(command.Commands)

sp := selection.New("Choose command", command.Commands)

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 := fmt.Sprintf("Choose a %s", input.Name)
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 := fmt.Sprintf("Please specify a %s", input.Name)
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()
}

0 comments on commit e09c4a6

Please sign in to comment.