forked from go-delve/delve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.go
195 lines (172 loc) · 4.24 KB
/
terminal.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package terminal
import (
"fmt"
"io"
"os"
"os/signal"
"strings"
"github.com/peterh/liner"
"syscall"
"github.com/derekparker/delve/config"
"github.com/derekparker/delve/service"
)
const (
historyFile string = ".dbg_history"
terminalBlueEscapeCode string = "\033[34m"
terminalResetEscapeCode string = "\033[0m"
)
// Term represents the terminal running dlv.
type Term struct {
client service.Client
prompt string
line *liner.State
conf *config.Config
dumb bool
InitFile string
}
// New returns a new Term.
func New(client service.Client, conf *config.Config) *Term {
return &Term{
prompt: "(dlv) ",
line: liner.NewLiner(),
client: client,
conf: conf,
dumb: strings.ToLower(os.Getenv("TERM")) == "dumb",
}
}
// Run begins running dlv in the terminal.
func (t *Term) Run() (int, error) {
defer t.line.Close()
// Send the debugger a halt command on SIGINT
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT)
go func() {
for range ch {
_, err := t.client.Halt()
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
}
}
}()
cmds := DebugCommands(t.client)
if t.conf != nil && t.conf.Aliases != nil {
cmds.Merge(t.conf.Aliases)
}
t.line.SetCompleter(func(line string) (c []string) {
for _, cmd := range cmds.cmds {
for _, alias := range cmd.aliases {
if strings.HasPrefix(alias, strings.ToLower(line)) {
c = append(c, alias)
}
}
}
return
})
fullHistoryFile, err := config.GetConfigFilePath(historyFile)
if err != nil {
fmt.Printf("Unable to load history file: %v.", err)
}
f, err := os.Open(fullHistoryFile)
if err != nil {
f, err = os.Create(fullHistoryFile)
if err != nil {
fmt.Printf("Unable to open history file: %v. History will not be saved for this session.", err)
}
}
t.line.ReadHistory(f)
f.Close()
fmt.Println("Type 'help' for list of commands.")
if t.InitFile != "" {
err := cmds.executeFile(t, t.InitFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error executing init file: %s\n", err)
}
}
var status int
for {
cmdstr, err := t.promptForInput()
if err != nil {
if err == io.EOF {
fmt.Println("exit")
return t.handleExit()
}
err, status = fmt.Errorf("Prompt for input failed.\n"), 1
break
}
cmdstr, args := parseCommand(cmdstr)
cmd := cmds.Find(cmdstr)
if err := cmd(t, args); err != nil {
if _, ok := err.(ExitRequestError); ok {
return t.handleExit()
}
// The type information gets lost in serialization / de-serialization,
// so we do a string compare on the error message to see if the process
// has exited, or if the command actually failed.
if strings.Contains(err.Error(), "exited") {
fmt.Fprintln(os.Stderr, err.Error())
} else {
fmt.Fprintf(os.Stderr, "Command failed: %s\n", err)
}
}
}
return status, nil
}
// Println prints a line to the terminal.
func (t *Term) Println(prefix, str string) {
if !t.dumb {
prefix = fmt.Sprintf("%s%s%s", terminalBlueEscapeCode, prefix, terminalResetEscapeCode)
}
fmt.Printf("%s%s\n", prefix, str)
}
func (t *Term) promptForInput() (string, error) {
l, err := t.line.Prompt(t.prompt)
if err != nil {
return "", err
}
l = strings.TrimSuffix(l, "\n")
if l != "" {
t.line.AppendHistory(l)
}
return l, nil
}
func (t *Term) handleExit() (int, error) {
fullHistoryFile, err := config.GetConfigFilePath(historyFile)
if err != nil {
fmt.Println("Error saving history file:", err)
} else {
if f, err := os.OpenFile(fullHistoryFile, os.O_RDWR, 0666); err == nil {
_, err := t.line.WriteHistory(f)
if err != nil {
fmt.Println("readline history error: ", err)
}
f.Close()
}
}
s, err := t.client.GetState()
if err != nil {
return 1, err
}
if !s.Exited {
kill := true
if t.client.AttachedToExistingProcess() {
answer, err := t.line.Prompt("Would you like to kill the process? [Y/n] ")
if err != nil {
return 2, io.EOF
}
answer = strings.ToLower(strings.TrimSpace(answer))
kill = (answer != "n" && answer != "no")
}
err = t.client.Detach(kill)
if err != nil {
return 1, err
}
}
return 0, nil
}
func parseCommand(cmdstr string) (string, string) {
vals := strings.SplitN(cmdstr, " ", 2)
if len(vals) == 1 {
return vals[0], ""
}
return vals[0], strings.TrimSpace(vals[1])
}