Skip to content

Commit

Permalink
Merge 16db90e into 9f9605c
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW authored Jun 14, 2024
2 parents 9f9605c + 16db90e commit 0c6e0eb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,7 @@ See [Language Definition](https://github.com/expr-lang/expr/blob/master/docs/lan
- `input` ... [prompter.Prompt](https://pkg.go.dev/github.com/Songmu/prompter#Prompt)
- `intersect` ... Find the intersection of two iterable values ( `func(x, y any) any` ).
- `secret` ... [prompter.Password](https://pkg.go.dev/github.com/Songmu/prompter#Password)
- `select` ... [prompter.Choose](https://pkg.go.dev/github.com/Songmu/prompter#Choose)
- `select` ... Select from candidates. `func(message string, candidates []string, default string) string`
- `basename` ... [filepath.Base](https://pkg.go.dev/path/filepath#Base)
- `time` ... Converts the given string or number to `time.Time{}`.
- `faker.*` ... Generate fake data using [Faker](https://pkg.go.dev/github.com/k1LoW/runn/builtin#Faker) ).
Expand Down
39 changes: 35 additions & 4 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import (
"net/url"
"path/filepath"
"regexp"
"slices"
"strings"
"testing"
"time"

"github.com/Songmu/prompter"
"github.com/chromedp/chromedp"
"github.com/elk-language/go-prompt"
pstrings "github.com/elk-language/go-prompt/strings"
"github.com/k1LoW/duration"
"github.com/k1LoW/runn/builtin"
"github.com/k1LoW/sshc/v4"
"github.com/samber/lo"
"github.com/spf13/cast"
"golang.org/x/crypto/ssh"
"google.golang.org/grpc"
Expand Down Expand Up @@ -1220,11 +1224,38 @@ func setupBuiltinFunctions(opts ...Option) []Option {
return prompter.Password(cast.ToString(msg))
}),
Func("select", func(msg any, list []any, defaultSelect any) string {
var choices []string
for _, v := range list {
choices = append(choices, cast.ToString(v))
choices := lo.Map(list, func(v any, _ int) string { return cast.ToString(v) })

var completer prompt.Completer = func(d prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber, pstrings.RuneNumber) {
endIndex := d.CurrentRuneIndex()
w := d.GetWordBeforeCursor()
startIndex := endIndex - pstrings.RuneCount([]byte(w))

var s = []prompt.Suggest{}
for _, v := range choices {
s = append(s, prompt.Suggest{Text: v})
}
return prompt.FilterHasPrefix(s, w, true), startIndex, endIndex
}
for {
opts := []prompt.Option{
prompt.WithCompleter(completer),
}
if cast.ToString(defaultSelect) == "" {
opts = append(opts, prompt.WithPrefix(fmt.Sprintf("%s: ", cast.ToString(msg))))
} else {
opts = append(opts, prompt.WithPrefix(fmt.Sprintf("%s [%s]: ", cast.ToString(msg), cast.ToString(defaultSelect))))
}
selected := prompt.Input(opts...)
if selected == "" {
return cast.ToString(defaultSelect)
}
if !slices.Contains(choices, selected) {
fmt.Println("Invalid selection. Please try again.")
continue
}
return selected
}
return prompter.Choose(cast.ToString(msg), choices, cast.ToString(defaultSelect))
}),
Func("basename", filepath.Base),
Func("faker", builtin.NewFaker()),
Expand Down

0 comments on commit 0c6e0eb

Please sign in to comment.