-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.go
More file actions
63 lines (52 loc) · 1.21 KB
/
shell.go
File metadata and controls
63 lines (52 loc) · 1.21 KB
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
package main
import (
"fmt"
"os"
"strings"
"github.com/chzyer/readline"
)
type Shell struct {
internal *readline.Instance
}
func NewShell() *Shell {
shell := Shell{}
err := os.MkdirAll(_histDirname, 0o750)
if err != nil {
fmt.Printf("history disabled due to inability to create directory: %s", _histDirname)
shell.internal, err = readline.New("")
if err != nil {
panic(err)
}
} else {
shell.internal, err = readline.NewEx(&readline.Config{
HistoryFile: _histFilename,
})
if err != nil {
panic(err)
}
}
return &shell
}
func (s *Shell) SetPrompt(prompt string) {
s.internal.SetPrompt(prompt)
}
func (s *Shell) ReadLine() string {
line, err := s.internal.Readline()
if err != nil {
// probably normal exit due to ctrl-c, ctrl-d
return "exit"
}
// dirty hack to permit 1,000,000 to be interpreted as 1000000
// mostly for pasting financial values. i'm sure there is a more
// i18n friendly way of doing this so that 1.000.000 would also
// work for european locales, etc.
commasRemoved := strings.ReplaceAll(line, ",", "")
lineTrimmed := strings.TrimSpace(commasRemoved)
return lineTrimmed
}
func (s *Shell) Close() {
err := s.internal.Close()
if err != nil {
fmt.Println(err)
}
}