8000 Include command and input descriptions in prompt by evilmarty · Pull Request #42 · evilmarty/ilc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Include command and input descriptions in prompt #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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