Skip to content

Commit bea1a41

Browse files
author
Rodrigo Navarro
committed
no-story (feature): Add prompt package with input and select utility
1 parent 329c759 commit bea1a41

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

pkg/prompt/prompt.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package prompt
2+
3+
import (
4+
"errors"
5+
"strings"
6+
7+
"github.com/mritd/promptx"
8+
)
9+
10+
// SelectItem a default item struct
11+
type SelectItem struct {
12+
Title string
13+
Description string
14+
Value string
15+
}
16+
17+
// SelectConfiguration a complete configuration select options
18+
type SelectConfiguration struct {
19+
ActiveTpl string
20+
InactiveTpl string
21+
SelectPrompt string
22+
SelectedTpl string
23+
DetailsTpl string
24+
}
25+
26+
// Select Returns a selected item
27+
func Select(items []SelectItem, config SelectConfiguration) SelectItem {
28+
configuration := &promptx.SelectConfig{
29+
ActiveTpl: config.ActiveTpl,
30+
InactiveTpl: config.InactiveTpl,
31+
SelectPrompt: config.SelectPrompt,
32+
SelectedTpl: config.SelectedTpl,
33+
DisPlaySize: 9,
34+
DetailsTpl: config.DetailsTpl,
35+
}
36+
37+
selector := &promptx.Select{
38+
Items: items,
39+
Config: configuration,
40+
}
41+
42+
return items[selector.Run()]
43+
}
44+
45+
// Input return a simple user input
46+
func Input(title string, errorMessage string, defaultValue string) string {
47+
input := promptx.NewDefaultPrompt(func(line []rune) error {
48+
if strings.TrimSpace(string(line)) == "" && defaultValue == "" {
49+
return errors.New(errorMessage)
50+
}
51+
52+
return nil
53+
}, title)
54+
55+
value := input.Run()
56+
57+
if value == "" && defaultValue != "" {
58+
return defaultValue
59+
}
60+
61+
return value
62+
}

0 commit comments

Comments
 (0)