This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from rainycape/governator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readline.go
87 lines (75 loc) · 1.42 KB
/
readline.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
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
)
var (
readline func(string) *string
add_history func(string)
read_history func(string)
write_history func(string)
)
type lineReader interface {
ReadLine() (string, error)
AddHistory(s string)
}
type bufLineReader struct {
r *bufio.Reader
}
func (r *bufLineReader) ReadLine() (string, error) {
fmt.Printf("%s> ", AppName)
return r.r.ReadString('\n')
}
func (r *bufLineReader) AddHistory(_ string) {
}
func newLineReader() lineReader {
if readline != nil {
r := &readlineLineReader{}
r.readHistory()
return r
}
return &bufLineReader{
r: bufio.NewReader(os.Stdin),
}
}
type readlineLineReader struct {
}
func (r *readlineLineReader) historyFile() (string, error) {
dir, err := governatorUserDir()
if err != nil {
return "", err
}
return filepath.Join(dir, "history"), nil
}
func (r *readlineLineReader) readHistory() error {
if read_history != nil {
file, err := r.historyFile()
if err != nil {
return err
}
read_history(file)
}
return nil
}
func (r *readlineLineReader) ReadLine() (string, error) {
s := readline(fmt.Sprintf("%s> ", AppName))
if s == nil {
return "", io.EOF
}
return *s, nil
}
func (r *readlineLineReader) AddHistory(s string) {
if add_history != nil {
add_history(s)
if write_history != nil {
file, err := r.historyFile()
if err != nil {
return
}
write_history(file)
}
}
}