pkg/terminal: fix handling list colors via config (#3240)

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.
This commit is contained in:
Derek Parker 2023-01-04 01:22:19 -08:00 committed by GitHub
parent 709da9a217
commit 9230a97210
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 22 deletions

@ -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)
}

@ -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
}

@ -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 {
@ -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
}