delve/terminal/terminal.go

196 lines
4.2 KiB
Go
Raw Normal View History

package terminal
import (
"fmt"
"io"
"os"
"os/signal"
"strings"
2016-01-15 05:26:54 +00:00
"syscall"
2016-02-27 23:02:55 +00:00
"github.com/peterh/liner"
"github.com/derekparker/delve/config"
"github.com/derekparker/delve/service"
)
const (
historyFile string = ".dbg_history"
2016-01-10 08:57:52 +00:00
terminalBlueEscapeCode string = "\033[34m"
terminalResetEscapeCode string = "\033[0m"
)
2016-01-10 08:57:52 +00:00
// Term represents the terminal running dlv.
type Term struct {
2015-10-04 17:58:32 +00:00
client service.Client
prompt string
line *liner.State
2016-02-27 23:02:55 +00:00
cmds *Commands
2015-10-04 17:58:32 +00:00
dumb bool
InitFile string
}
2016-01-10 08:57:52 +00:00
// New returns a new Term.
func New(client service.Client, conf *config.Config) *Term {
2016-02-27 23:02:55 +00:00
cmds := DebugCommands(client)
if conf != nil && conf.Aliases != nil {
cmds.Merge(conf.Aliases)
}
return &Term{
prompt: "(dlv) ",
line: liner.NewLiner(),
client: client,
2016-02-27 23:02:55 +00:00
cmds: cmds,
dumb: !supportsEscapeCodes(),
}
}
2016-02-27 23:02:55 +00:00
// Close returns the terminal to its previous mode.
func (t *Term) Close() {
t.line.Close()
}
2016-01-10 08:57:52 +00:00
// Run begins running dlv in the terminal.
func (t *Term) Run() (int, error) {
defer t.Close()
// Send the debugger a halt command on SIGINT
ch := make(chan os.Signal)
2016-01-15 05:26:54 +00:00
signal.Notify(ch, syscall.SIGINT)
go func() {
for range ch {
_, err := t.client.Halt()
if err != nil {
2016-01-10 08:57:52 +00:00
fmt.Fprintf(os.Stderr, "%v", err)
}
}
}()
2015-09-10 14:09:32 +00:00
t.line.SetCompleter(func(line string) (c []string) {
2016-02-27 23:02:55 +00:00
for _, cmd := range t.cmds.cmds {
2015-09-10 14:09:32 +00:00
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 != "" {
2016-02-27 23:02:55 +00:00
err := t.cmds.executeFile(t, t.InitFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error executing init file: %s\n", err)
}
}
for {
cmdstr, err := t.promptForInput()
if err != nil {
if err == io.EOF {
2015-07-11 14:28:29 +00:00
fmt.Println("exit")
2015-07-29 23:19:06 +00:00
return t.handleExit()
}
2016-03-09 15:11:58 +00:00
return 1, fmt.Errorf("Prompt for input failed.\n")
}
cmdstr, args := parseCommand(cmdstr)
if err := t.cmds.Call(cmdstr, args, t); err != nil {
2015-07-29 23:19:06 +00:00
if _, ok := err.(ExitRequestError); ok {
return t.handleExit()
}
2015-06-21 18:08:14 +00:00
// 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)
}
}
}
}
2016-01-10 08:57:52 +00:00
// Println prints a line to the terminal.
func (t *Term) Println(prefix, str string) {
if !t.dumb {
2016-01-10 08:57:52 +00:00
prefix = fmt.Sprintf("%s%s%s", terminalBlueEscapeCode, prefix, terminalResetEscapeCode)
}
fmt.Printf("%s%s\n", prefix, str)
}
2015-04-30 13:38:00 +00:00
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
}
2016-01-10 08:57:52 +00:00
func (t *Term) handleExit() (int, error) {
fullHistoryFile, err := config.GetConfigFilePath(historyFile)
if err != nil {
2016-03-09 15:11:58 +00:00
fmt.Println("Error saving history file: ", err)
} else {
if f, err := os.OpenFile(fullHistoryFile, os.O_RDWR, 0666); err == nil {
2016-03-09 15:11:58 +00:00
_, err = t.line.WriteHistory(f)
if err != nil {
fmt.Println("readline history error: ", err)
}
f.Close()
}
}
s, err := t.client.GetState()
if err != nil {
2016-01-10 08:57:52 +00:00
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 {
2016-01-10 08:57:52 +00:00
return 2, io.EOF
}
answer = strings.ToLower(strings.TrimSpace(answer))
kill = (answer != "n" && answer != "no")
}
2016-03-09 15:11:58 +00:00
if err := t.client.Detach(kill); err != nil {
2016-01-10 08:57:52 +00:00
return 1, err
}
}
2016-01-10 08:57:52 +00:00
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])
}