-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathcli.go
More file actions
167 lines (150 loc) · 3.65 KB
/
cli.go
File metadata and controls
167 lines (150 loc) · 3.65 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Read Eval Print Loop for CLI
package cli
import (
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"github.com/go-python/gpython/py"
"github.com/go-python/gpython/repl"
"github.com/peterh/liner"
)
const HistoryFileName = ".gpyhistory"
// homeDirectory finds the home directory or returns ""
func homeDirectory() string {
usr, err := user.Current()
if err == nil {
return usr.HomeDir
}
// Fall back to reading $HOME - work around user.Current() not
// working for cross compiled binaries on OSX.
// https://github.com/golang/go/issues/6376
return os.Getenv("HOME")
}
// Holds state for readline services
type readline struct {
*liner.State
repl *repl.REPL
historyFile string
prompt string
}
// newReadline creates a new instance of readline
func newReadline(repl *repl.REPL) *readline {
rl := &readline{
State: liner.NewLiner(),
repl: repl,
}
home := homeDirectory()
if home != "" {
rl.historyFile = filepath.Join(home, HistoryFileName)
}
rl.SetTabCompletionStyle(liner.TabPrints)
rl.SetWordCompleter(rl.Completer)
rl.SetCtrlCAborts(true)
return rl
}
// readHistory reads the history into the term
func (rl *readline) ReadHistory() error {
f, err := os.Open(rl.historyFile)
if err != nil {
return err
}
defer f.Close()
_, err = rl.State.ReadHistory(f)
if err != nil {
return err
}
return nil
}
// writeHistory writes the history from the term
func (rl *readline) WriteHistory() error {
f, err := os.OpenFile(rl.historyFile, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return err
}
defer f.Close()
_, err = rl.State.WriteHistory(f)
if err != nil {
return err
}
return nil
}
// Close the readline and write history
func (rl *readline) Close() error {
err := rl.State.Close()
if err != nil {
return err
}
if rl.historyFile != "" {
err := rl.WriteHistory()
if err != nil {
return err
}
}
return nil
}
// Completer takes the currently edited line with the cursor
// position and returns the completion candidates for the partial word
// to be completed. If the line is "Hello, wo!!!" and the cursor is
// before the first '!', ("Hello, wo!!!", 9) is passed to the
// completer which may returns ("Hello, ", {"world", "Word"}, "!!!")
// to have "Hello, world!!!".
func (rl *readline) Completer(line string, pos int) (head string, completions []string, tail string) {
return rl.repl.Completer(line, pos)
}
// SetPrompt sets the current terminal prompt
func (rl *readline) SetPrompt(prompt string) {
rl.prompt = prompt
}
// Print prints the output
func (rl *readline) Print(out string) {
_, _ = os.Stdout.WriteString(out + "\n")
}
// RunREPL starts the REPL loop
func RunREPL(replCtx *repl.REPL) error {
if replCtx == nil {
replCtx = repl.New(nil)
}
rl := newReadline(replCtx)
replCtx.SetUI(rl)
defer rl.Close()
// Set up InputHook for the input() builtin function
py.InputHook = func(prompt string) (string, error) {
return rl.Prompt(prompt)
}
defer func() { py.InputHook = nil }()
err := rl.ReadHistory()
if err != nil {
if !os.IsNotExist(err) {
fmt.Printf("Failed to open history: %v\n", err)
}
}
for {
line, err := rl.Prompt(rl.prompt)
if err != nil {
if err == io.EOF {
fmt.Printf("\n")
break
}
if err == liner.ErrPromptAborted {
fmt.Println("KeyboardInterrupt")
rl.repl.ResetContinuation()
continue
}
fmt.Printf("Problem reading line: %v\n", err)
continue
}
if line != "" {
rl.AppendHistory(line)
}
err = rl.repl.Run(line)
if err != nil {
return err
}
}
return nil
}