terminal: Refactor handling of dumb terminals

This commit is contained in:
Derek Parker
2015-09-29 22:42:06 -07:00
parent 76b9aa03bb
commit 80107dba8d
3 changed files with 119 additions and 135 deletions

View File

@ -14,13 +14,18 @@ import (
"github.com/derekparker/delve/service"
)
const historyFile string = ".dbg_history"
const (
historyFile string = ".dbg_history"
TerminalBlueEscapeCode string = "\033[34m"
TerminalWhiteEscapeCode string = "\033[37m"
)
type Term struct {
client service.Client
prompt string
line *liner.State
conf *config.Config
dumb bool
}
func New(client service.Client, conf *config.Config) *Term {
@ -29,6 +34,7 @@ func New(client service.Client, conf *config.Config) *Term {
line: liner.NewLiner(),
client: client,
conf: conf,
dumb: strings.ToLower(os.Getenv("TERM")) == "dumb",
}
}
@ -93,7 +99,7 @@ func (t *Term) Run() (error, int) {
cmdstr, args := parseCommand(cmdstr)
cmd := cmds.Find(cmdstr)
if err := cmd(t.client, args...); err != nil {
if err := cmd(t, args...); err != nil {
if _, ok := err.(ExitRequestError); ok {
return t.handleExit()
}
@ -111,6 +117,13 @@ func (t *Term) Run() (error, int) {
return nil, status
}
func (t *Term) Println(prefix, str string) {
if !t.dumb {
prefix = fmt.Sprintf("%s%s%s", TerminalBlueEscapeCode, prefix, TerminalWhiteEscapeCode)
}
fmt.Printf("%s%s\n", prefix, str)
}
func (t *Term) promptForInput() (string, error) {
l, err := t.line.Prompt(t.prompt)
if err != nil {