-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrline.go
74 lines (65 loc) · 1.79 KB
/
rline.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
package rline
// Prompter is the shared interfaces for prompts.
type Prompter interface {
Prompt(string) (string, error)
Password(string) (string, error)
}
// Rline is a line reader.
type Rline struct {
prompt Prompt
app string
prompter Prompter
}
// New creates a new line reader for the specified prompt and app name.
func New(prompt Prompt, app string, opts ...Option) (*Rline, error) {
r := &Rline{
app: app,
}
if err := r.SetPrompt(prompt, opts...); err != nil {
return nil, err
}
return r, nil
}
// FromString creates a new line reader for the specified prompt and app name.
func FromString(typ, app string, opts ...Option) (*Rline, error) {
prompt, err := PromptFromString(typ)
if err != nil {
return nil, err
}
return New(prompt, app, opts...)
}
// SetPrompt changes the prompt.
//
// Can be used to dynamically change the line reader prompt.
//
// Options will need to be passed again if a prompt is changed, as individual
// options apply only to the actual line reader implementations.
func (r *Rline) SetPrompt(prompt Prompt, opts ...Option) error {
var err error
r.prompter, err = prompt.Init(r.app, opts...)
if err != nil {
return err
}
r.prompt = prompt
return nil
}
// SetPromptFromString changes the prompt to the named prompt type.
func (r *Rline) SetPromptFromString(typ string, opts ...Option) error {
prompt, err := PromptFromString(typ)
if err != nil {
return err
}
return r.SetPrompt(prompt, opts...)
}
// Type returns the prompt's type.
func (r *Rline) Type() string {
return r.prompt.String()
}
// Prompt prompts the user for a line of text.
func (r *Rline) Prompt(s string) (string, error) {
return r.prompter.Prompt(s)
}
// Password prompts the user for a password.
func (r *Rline) Password(s string) (string, error) {
return r.prompter.Password(s)
}