Skip to content

Commit f403415

Browse files
author
mritd
committed
feat(prompt): 支持自定义 err message
支持自定义 err message Signed-off-by: mritd <[email protected]>
1 parent 59b30f9 commit f403415

File tree

3 files changed

+36
-50
lines changed

3 files changed

+36
-50
lines changed

pkg/commit/ci.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
gitprompt "github.com/mritd/gitflow-toolkit/pkg/prompt"
1212
"github.com/mritd/gitflow-toolkit/pkg/util"
1313
"github.com/mritd/promptui"
14+
"github.com/pkg/errors"
1415
)
1516

1617
type TypeMessage struct {
@@ -83,11 +84,11 @@ func SelectCommitType() consts.CommitType {
8384
// 输入影响范围
8485
func InputScope() string {
8586

86-
p := gitprompt.NewDefaultPrompt(func(line []rune) bool {
87+
p := gitprompt.NewDefaultPrompt(func(line []rune) error {
8788
if strings.TrimSpace(string(line)) == "" {
88-
return false
89+
return errors.New("Input is empty!")
8990
} else {
90-
return true
91+
return nil
9192
}
9293
}, "Scope:")
9394

@@ -98,11 +99,11 @@ func InputScope() string {
9899
// 输入提交主题
99100
func InputSubject() string {
100101

101-
p := gitprompt.NewDefaultPrompt(func(line []rune) bool {
102+
p := gitprompt.NewDefaultPrompt(func(line []rune) error {
102103
if strings.TrimSpace(string(line)) == "" {
103-
return false
104+
return errors.New("Input is empty!")
104105
} else {
105-
return true
106+
return nil
106107
}
107108
}, "Subject:")
108109

@@ -112,8 +113,8 @@ func InputSubject() string {
112113
// 输入完整提交信息
113114
func InputBody() string {
114115

115-
p := gitprompt.NewDefaultPrompt(func(line []rune) bool {
116-
return true
116+
p := gitprompt.NewDefaultPrompt(func(line []rune) error {
117+
return nil
117118
}, "Body:")
118119

119120
body := p.Run()
@@ -127,8 +128,8 @@ func InputBody() string {
127128
// 输入提交关联信息
128129
func InputFooter() string {
129130

130-
p := gitprompt.NewDefaultPrompt(func(line []rune) bool {
131-
return true
131+
p := gitprompt.NewDefaultPrompt(func(line []rune) error {
132+
return nil
132133
}, "Footer:")
133134

134135
return p.Run()

pkg/prompt/codes.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package prompt
22

33
import (
4+
"fmt"
45
"strconv"
56
"strings"
6-
"fmt"
77
"text/template"
88
)
99

@@ -49,9 +49,12 @@ const (
4949
var ResetCode = fmt.Sprintf("%s%dm", esc, reset)
5050

5151
const (
52-
hideCursor = esc + "?25l"
53-
showCursor = esc + "?25h"
54-
clearLine = esc + "2K"
52+
hideCursor = esc + "?25l"
53+
showCursor = esc + "?25h"
54+
clearLine = esc + "2K"
55+
moveUp = esc + "1A"
56+
moveDown = esc + "1B"
57+
cleanTerminal = "\033c"
5558
)
5659

5760
// FuncMap defines template helpers for the output. It can be extended as a

pkg/prompt/prompt.go

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@ import (
1010
)
1111

1212
const (
13-
DefaultPrompt = "»"
14-
DefaultErrorMsg = "✘Invalid input!"
15-
DefaultQuestionTpl = "{{ . | cyan }} "
16-
DefaultPromptTpl = "{{ . | green }} "
17-
DefaultInvalidTpl = "{{ . | red }} "
18-
DefaultValidTpl = "{{ . | green }} "
19-
DefaultErrorMsgTpl = "{{ . | red }} "
13+
DefaultPrompt = "»"
14+
DefaultErrorMsgPrefix = "✘ "
15+
DefaultQuestionTpl = "{{ . | cyan }} "
16+
DefaultPromptTpl = "{{ . | green }} "
17+
DefaultInvalidTpl = "{{ . | red }} "
18+
DefaultValidTpl = "{{ . | green }} "
19+
DefaultErrorMsgTpl = "{{ . | red }} "
2020
)
2121

2222
type Prompt struct {
2323
Question string
2424
Prompt string
25-
ErrorMsg string
2625
PromptTpl *Tpl
2726
FuncMap template.FuncMap
2827

@@ -39,10 +38,10 @@ type Tpl struct {
3938
ValidTpl string
4039
InvalidTpl string
4140
ErrorMsgTpl string
42-
CheckListener func(line []rune) bool
41+
CheckListener func(line []rune) error
4342
}
4443

45-
func NewDefaultTpl(check func(line []rune) bool) *Tpl {
44+
func NewDefaultTpl(check func(line []rune) error) *Tpl {
4645
return &Tpl{
4746
QuestionTpl: DefaultQuestionTpl,
4847
PromptTpl: DefaultPromptTpl,
@@ -53,10 +52,9 @@ func NewDefaultTpl(check func(line []rune) bool) *Tpl {
5352
}
5453
}
5554

56-
func NewDefaultPrompt(check func(line []rune) bool, question string) *Prompt {
55+
func NewDefaultPrompt(check func(line []rune) error, question string) *Prompt {
5756
return &Prompt{
5857
Question: question,
59-
ErrorMsg: DefaultErrorMsg,
6058
Prompt: DefaultPrompt,
6159
PromptTpl: NewDefaultTpl(check),
6260
FuncMap: FuncMap,
@@ -104,52 +102,36 @@ func (p *Prompt) Run() string {
104102
displayPrompt := append(render(p.prompt, p.Prompt), render(p.question, p.Question)...)
105103
validPrompt := append(render(p.valid, p.Prompt), render(p.question, p.Question)...)
106104
invalidPrompt := append(render(p.invalid, p.Prompt), render(p.question, p.Question)...)
107-
errorMsgPrompt := render(p.errorMsg, p.ErrorMsg)
108105

109106
l, err := readline.NewEx(&readline.Config{
110107
Prompt: string(displayPrompt),
111108
DisableAutoSaveHistory: true,
112109
InterruptPrompt: "^C",
113110
FuncFilterInputRune: filterInput,
114111
})
112+
util.CheckAndExit(err)
115113

116114
l.Config.SetListener(func(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
117-
if p.PromptTpl.CheckListener(line) {
115+
if err = p.PromptTpl.CheckListener(line); err != nil {
116+
l.SetPrompt(string(invalidPrompt))
117+
l.Refresh()
118118

119+
} else {
119120
l.SetPrompt(string(validPrompt))
120121
l.Refresh()
121-
} else {
122122

123-
l.SetPrompt(string(invalidPrompt))
124-
l.Refresh()
125123
}
126124
return nil, 0, false
127125

128126
})
129-
if err != nil {
130-
panic(err)
131-
}
132127
defer l.Close()
133128
for {
134129
s, err := l.Readline()
135130
util.CheckAndExit(err)
136-
if p.PromptTpl.CheckListener([]rune(s)) {
137-
return s
131+
if err = p.PromptTpl.CheckListener([]rune(s)); err != nil {
132+
fmt.Println(string(render(p.errorMsg, DefaultErrorMsgPrefix+err.Error())))
138133
} else {
139-
fmt.Println(string(errorMsgPrompt))
134+
return s
140135
}
141136
}
142137
}
143-
144-
//func Test() {
145-
//
146-
// check := func(line []rune) bool {
147-
// if len(line) > 5 {
148-
// return false
149-
// }
150-
// return true
151-
// }
152-
//
153-
// p := NewDefaultPrompt(check, "请输入5个字符:")
154-
// fmt.Println(p.Run())
155-
//}

0 commit comments

Comments
 (0)