-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetline.go
40 lines (35 loc) · 1.02 KB
/
getline.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
package main
import (
"context"
"errors"
"fmt"
"io"
"github.com/nyaosorg/go-readline-ny"
"github.com/nyaosorg/go-readline-ny/keys"
)
type Tty = readline.XTty
func getline(out io.Writer, prompt string, defaultStr string, history readline.IHistory) (string, error) {
editor := readline.Editor{
Writer: out,
Default: defaultStr,
Cursor: 65535,
PromptWriter: func(w io.Writer) (int, error) {
fmt.Fprintf(w, "\r\x1B[0;33;40;1m%s%s", prompt, _ANSI_ERASE_LINE)
return 2, nil
},
LineFeedWriter: func(readline.Result, io.Writer) (int, error) { return 0, nil },
History: history,
}
defer io.WriteString(out, _ANSI_CURSOR_OFF)
editor.BindKey(keys.Escape, readline.CmdInterrupt)
text, err := editor.ReadLine(context.Background())
if err == readline.CtrlC {
return "", errors.New("Canceled")
}
return text, err
}
func yesNo(tty1 Tty, out io.Writer, message string) bool {
fmt.Fprintf(out, "%s\r%s%s", _ANSI_YELLOW, message, _ANSI_ERASE_LINE)
ch, err := readline.GetKey(tty1)
return err == nil && ch == "y"
}