Skip to content

Commit

Permalink
pkg/terminal: fix handling list colors via config (go-delve#3240)
Browse files Browse the repository at this point in the history
Parsing the source list color when set during a Delve debug session
resulted in unexpected errors. Additionally the changes were not reflected
in the current session, forcing the user to save the config and start a
new session. This patch fixes those issues.
  • Loading branch information
derekparker authored Jan 4, 2023
1 parent 709da9a commit 9230a97
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
21 changes: 21 additions & 0 deletions pkg/config/split.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,28 @@ func ConfigureSetSimple(rest string, cfgname string, field reflect.Value) error
v := rest == "true"
return reflect.ValueOf(&v), nil
case reflect.String:
unquoted, err := strconv.Unquote(rest)
if err == nil {
rest = unquoted
}
return reflect.ValueOf(&rest), nil
case reflect.Interface:
// We special case this particular configuration key because historically we accept both a numerical value and a string value for it.
if cfgname == "source-list-line-color" {
n, err := strconv.Atoi(rest)
if err == nil {
if n < 0 {
return reflect.ValueOf(nil), fmt.Errorf("argument to %q must be a number greater than zero", cfgname)
}
return reflect.ValueOf(&n), nil
}
unquoted, err := strconv.Unquote(rest)
if err == nil {
rest = unquoted
}
return reflect.ValueOf(&rest), nil
}
fallthrough
default:
return reflect.ValueOf(nil), fmt.Errorf("unsupported type for configuration key %q", cfgname)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/terminal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func configureCmd(t *Term, ctx callContext, args string) error {
if t.client != nil { // only happens in tests
lcfg := t.loadConfig()
t.client.SetReturnValuesLoadConfig(&lcfg)
t.updateColorScheme()
}
return nil
}
Expand Down
53 changes: 31 additions & 22 deletions pkg/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,7 @@ func New(client service.Client, conf *config.Config) *Term {
t.stdout.pw = &pagingWriter{w: getColorableWriter()}
t.stdout.colorEscapes = make(map[colorize.Style]string)
t.stdout.colorEscapes[colorize.NormalStyle] = terminalResetEscapeCode
wd := func(s string, defaultCode int) string {
if s == "" {
return fmt.Sprintf(terminalHighlightEscapeCode, defaultCode)
}
return s
}
t.stdout.colorEscapes[colorize.KeywordStyle] = conf.SourceListKeywordColor
t.stdout.colorEscapes[colorize.StringStyle] = wd(conf.SourceListStringColor, ansiGreen)
t.stdout.colorEscapes[colorize.NumberStyle] = conf.SourceListNumberColor
t.stdout.colorEscapes[colorize.CommentStyle] = wd(conf.SourceListCommentColor, ansiBrMagenta)
t.stdout.colorEscapes[colorize.ArrowStyle] = wd(conf.SourceListArrowColor, ansiYellow)
switch x := conf.SourceListLineColor.(type) {
case string:
t.stdout.colorEscapes[colorize.LineNoStyle] = x
case int:
if (x > ansiWhite && x < ansiBrBlack) || x < ansiBlack || x > ansiBrWhite {
x = ansiBlue
}
t.stdout.colorEscapes[colorize.LineNoStyle] = fmt.Sprintf(terminalHighlightEscapeCode, x)
case nil:
t.stdout.colorEscapes[colorize.LineNoStyle] = fmt.Sprintf(terminalHighlightEscapeCode, ansiBlue)
}
t.updateColorScheme()
}

if client != nil {
Expand All @@ -141,6 +120,36 @@ func New(client service.Client, conf *config.Config) *Term {
return t
}

func (t *Term) updateColorScheme() {
if t.stdout.colorEscapes == nil {
return
}

conf := t.conf
wd := func(s string, defaultCode int) string {
if s == "" {
return fmt.Sprintf(terminalHighlightEscapeCode, defaultCode)
}
return s
}
t.stdout.colorEscapes[colorize.KeywordStyle] = conf.SourceListKeywordColor
t.stdout.colorEscapes[colorize.StringStyle] = wd(conf.SourceListStringColor, ansiGreen)
t.stdout.colorEscapes[colorize.NumberStyle] = conf.SourceListNumberColor
t.stdout.colorEscapes[colorize.CommentStyle] = wd(conf.SourceListCommentColor, ansiBrMagenta)
t.stdout.colorEscapes[colorize.ArrowStyle] = wd(conf.SourceListArrowColor, ansiYellow)
switch x := conf.SourceListLineColor.(type) {
case string:
t.stdout.colorEscapes[colorize.LineNoStyle] = x
case int:
if (x > ansiWhite && x < ansiBrBlack) || x < ansiBlack || x > ansiBrWhite {
x = ansiBlue
}
t.stdout.colorEscapes[colorize.LineNoStyle] = fmt.Sprintf(terminalHighlightEscapeCode, x)
case nil:
t.stdout.colorEscapes[colorize.LineNoStyle] = fmt.Sprintf(terminalHighlightEscapeCode, ansiBlue)
}
}

func (t *Term) SetTraceNonInteractive() {
t.traceNonInteractive = true
}
Expand Down

0 comments on commit 9230a97

Please sign in to comment.