-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.go
65 lines (56 loc) · 1.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Package main demonstrates how promptkit/textinput can be customized.
package main
import (
"fmt"
"net"
"os"
"github.com/erikgeiser/promptkit/textinput"
)
func main() {
const customTemplate = `
{{- "┏" }}━{{ Repeat "━" (Len .Prompt) }}━┯━{{ Repeat "━" 13 }}{{ "━━━━┓\n" }}
{{- "┃" }} {{ Bold .Prompt }} │ {{ .Input -}}
{{- Repeat " " (Max 0 (Sub 16 (Len .Input))) }}
{{- if .ValidationError -}}
{{- Foreground "1" (Bold "✘") -}}
{{- else -}}
{{- Foreground "2" (Bold "✔") -}}
{{- end -}}┃
{{- if .ValidationError -}}
{{- (print " Error: " (Foreground "1" .ValidationError.Error)) -}}
{{- end -}}
{{- "\n┗" }}━{{ Repeat "━" (Len .Prompt) }}━┷━{{ Repeat "━" 13 }}{{ "━━━━┛\n" -}}
{{- if .AutoCompleteIndecisive -}}
{{ print " Suggestions: " }}
{{- range $suggestion := AutoCompleteSuggestions -}}
{{- print $suggestion " " -}}
{{- end -}}
{{- end -}}
`
const customResultTemplate = `
{{- Bold (print "🖥️ Connecting to " (Foreground "32" .FinalValue) ) -}}`
input := textinput.New("Enter an IP address")
input.Placeholder = "127.0.0.1"
input.Validate = func(input string) error {
if net.ParseIP(input) == nil {
return fmt.Errorf("invalid IP address")
}
return nil
}
input.Template = customTemplate
input.ResultTemplate = customResultTemplate
input.CharLimit = 15
input.AutoComplete = textinput.AutoCompleteFromSliceWithDefault([]string{
"10.0.0.1",
"10.0.0.2",
"127.0.0.1",
"fe80::1",
}, input.Placeholder)
ip, err := input.RunPrompt()
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
// do something with the result
_ = ip
}