This repository has been archived by the owner on Dec 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompt.go
137 lines (117 loc) · 3.23 KB
/
prompt.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package promptx
import (
"text/template"
"github.com/mritd/promptx/utils"
"github.com/mritd/readline"
)
const (
DefaultPrompt = "»"
DefaultErrorMsgPrefix = "✘ "
DefaultAskTpl = "{{ . | cyan }} "
DefaultPromptTpl = "{{ . | green }} "
DefaultInvalidTpl = "{{ . | red }} "
DefaultValidTpl = "{{ . | green }} "
DefaultErrorMsgTpl = "{{ . | red }} "
)
type Prompt struct {
Config
Ask string
Prompt string
FuncMap template.FuncMap
isFirstRun bool
ask *template.Template
prompt *template.Template
valid *template.Template
invalid *template.Template
errorMsg *template.Template
}
type Config struct {
AskTpl string
PromptTpl string
ValidTpl string
InvalidTpl string
ErrorMsgTpl string
CheckListener func(line []rune) error
}
func NewDefaultConfig(check func(line []rune) error) Config {
return Config{
AskTpl: DefaultAskTpl,
PromptTpl: DefaultPromptTpl,
InvalidTpl: DefaultInvalidTpl,
ValidTpl: DefaultValidTpl,
ErrorMsgTpl: DefaultErrorMsgTpl,
CheckListener: check,
}
}
func NewDefaultPrompt(check func(line []rune) error, ask string) Prompt {
return Prompt{
Ask: ask,
Prompt: DefaultPrompt,
FuncMap: FuncMap,
Config: NewDefaultConfig(check),
}
}
func (p *Prompt) prepareTemplates() {
var err error
p.ask, err = template.New("").Funcs(FuncMap).Parse(p.AskTpl)
utils.CheckAndExit(err)
p.prompt, err = template.New("").Funcs(FuncMap).Parse(p.PromptTpl)
utils.CheckAndExit(err)
p.valid, err = template.New("").Funcs(FuncMap).Parse(p.ValidTpl)
utils.CheckAndExit(err)
p.invalid, err = template.New("").Funcs(FuncMap).Parse(p.InvalidTpl)
utils.CheckAndExit(err)
p.errorMsg, err = template.New("").Funcs(FuncMap).Parse(p.ErrorMsgTpl)
utils.CheckAndExit(err)
}
func (p *Prompt) Run() string {
p.isFirstRun = true
p.prepareTemplates()
displayPrompt := append(utils.Render(p.prompt, p.Prompt), utils.Render(p.ask, p.Ask)...)
validPrompt := append(utils.Render(p.valid, p.Prompt), utils.Render(p.ask, p.Ask)...)
invalidPrompt := append(utils.Render(p.invalid, p.Prompt), utils.Render(p.ask, p.Ask)...)
l, err := readline.NewEx(&readline.Config{
Prompt: string(displayPrompt),
DisableAutoSaveHistory: true,
InterruptPrompt: "^C",
})
utils.CheckAndExit(err)
filterInput := func(r rune) (rune, bool) {
switch r {
// block CtrlZ feature
case readline.CharCtrlZ:
return r, false
default:
return r, true
}
}
l.Config.FuncFilterInputRune = filterInput
l.Config.SetListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
// Real-time verification
if err = p.CheckListener(line); err != nil {
l.SetPrompt(string(invalidPrompt))
l.Refresh()
} else {
l.SetPrompt(string(validPrompt))
l.Refresh()
}
return nil, 0, false
})
defer func() { _ = l.Close() }()
// read line
for {
if !p.isFirstRun {
_, err := l.Write([]byte(moveUp))
utils.CheckAndExit(err)
}
s, err := l.Readline()
utils.CheckAndExit(err)
if err = p.CheckListener([]rune(s)); err != nil {
_, _ = l.Write([]byte(clearLine))
_, _ = l.Write(utils.Render(p.errorMsg, DefaultErrorMsgPrefix+err.Error()))
p.isFirstRun = false
} else {
return s
}
}
}