-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprompt_liner.go
48 lines (40 loc) · 797 Bytes
/
prompt_liner.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
package main
import (
"io"
"github.com/itchyny/liner"
"github.com/mattn/go-tty"
)
type realPrompt struct {
state *liner.State
}
func newPrompt() *realPrompt {
return &realPrompt{}
}
func (p *realPrompt) start() error {
tty, err := tty.Open()
if err != nil {
return err
}
p.state = liner.NewLinerTTY(tty)
p.state.SetCtrlCAborts(true)
return nil
}
func (p *realPrompt) prompt(message string) (string, error) {
input, err := p.state.Prompt(message)
if err != nil {
if err == liner.ErrPromptAborted {
return "", io.EOF
}
return "", err
}
return input, nil
}
func (p *realPrompt) setHistory(history []string) {
p.state.ClearHistory()
for i := len(history) - 1; i >= 0; i-- {
p.state.AppendHistory(history[i])
}
}
func (p *realPrompt) close() {
p.state.Close()
}