-
Notifications
You must be signed in to change notification settings - Fork 5
/
prompt_example_customize_output_test.go
70 lines (54 loc) · 1.25 KB
/
prompt_example_customize_output_test.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
package strumt_test
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/antham/strumt/v2"
)
func Example_customizePromptOutput() {
var stdout bytes.Buffer
buf := "whatever\nyes\n"
p := strumt.NewPromptsFromReaderAndWriter(bytes.NewBufferString(buf), &stdout)
p.AddLinePrompter(&AreYouOkPrompt{})
p.SetFirst("okprompt")
p.Run()
for {
line, err := stdout.ReadString('\n')
if err == io.EOF {
break
}
fmt.Println(strings.TrimSpace(line))
}
// Output:
// ==> Are you Ok ?
// An error occurred : You must answer yes or no
// ==> Are you Ok ?
//
}
type AreYouOkPrompt struct {
}
func (a *AreYouOkPrompt) ID() string {
return "okprompt"
}
func (a *AreYouOkPrompt) PromptString() string {
return "Are you Ok ?"
}
func (a *AreYouOkPrompt) Parse(value string) error {
if value == "yes" || value == "no" {
return nil
}
return fmt.Errorf("You must answer yes or no")
}
func (a *AreYouOkPrompt) NextOnSuccess(value string) string {
return ""
}
func (a *AreYouOkPrompt) NextOnError(err error) string {
return "okprompt"
}
func (a *AreYouOkPrompt) PrintPrompt(w io.Writer, prompt string) {
fmt.Fprintf(w, "==> %s\n", prompt)
}
func (a *AreYouOkPrompt) PrintError(w io.Writer, err error) {
fmt.Fprintf(w, "An error occurred : %s", err)
}