From e6247da593d3704d1b0cc98ab20bdba01d4e8f34 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 14 Mar 2021 14:14:08 +0100 Subject: [PATCH] feat: set terminal background color relates to #529 relates to #322 relates to #497 Windors terminal and Visual Studio Code do not work well with inversted ANSI sequences. This weak allows users to override the Tranparency to the terminal background color removing black elements in their prompt. Ideally we remove this once they are on par with other terminals, but that could take a while. See https://github.com/microsoft/vscode/issues/111762 and https://github.com/microsoft/terminal/issues/7014 --- docs/docs/configuration.md | 2 ++ src/ansi_color.go | 28 +++++++++++++++++++--------- src/main.go | 4 +++- src/settings.go | 1 + themes/schema.json | 1 + 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/docs/docs/configuration.md b/docs/docs/configuration.md index 6044f1ec..e4ea79d4 100644 --- a/docs/docs/configuration.md +++ b/docs/docs/configuration.md @@ -57,6 +57,8 @@ boxes with question marks, [set up your terminal][setupterm] to use a supported - console_title: `boolean` - when true sets the current location as the console title - console_title_style: `string` - the title to set in the console - defaults to `folder` - console_title_template: `string` - the template to use when `"console_title_style" = "template"` +- terminal_background: `string` [color][colors] - terminal background color, set to your terminal's background color when +you notice black elements in Windows Terminal or the Visual Studio Code integrated terminal > "I Like The Way You Speak Words" - Gary Goodspeed diff --git a/src/ansi_color.go b/src/ansi_color.go index f2418982..a35bc1ae 100644 --- a/src/ansi_color.go +++ b/src/ansi_color.go @@ -45,8 +45,9 @@ func getColorFromName(colorName string, isBackground bool) (string, error) { // AnsiColor writes colorized strings type AnsiColor struct { - builder strings.Builder - formats *ansiFormats + builder strings.Builder + formats *ansiFormats + terminalBackground string } const ( @@ -71,18 +72,27 @@ func (a *AnsiColor) writeColoredText(background, foreground, text string) { if text == "" { return } - var coloredText string + if foreground == Transparent && background != "" && a.terminalBackground != "" { + bgAnsiColor := a.getAnsiFromColorString(background, true) + fgAnsiColor := a.getAnsiFromColorString(a.terminalBackground, false) + coloredText := fmt.Sprintf(a.formats.colorFull, bgAnsiColor, fgAnsiColor, text) + a.builder.WriteString(coloredText) + return + } if foreground == Transparent && background != "" { ansiColor := a.getAnsiFromColorString(background, false) - coloredText = fmt.Sprintf(a.formats.colorTransparent, ansiColor, text) + coloredText := fmt.Sprintf(a.formats.colorTransparent, ansiColor, text) + a.builder.WriteString(coloredText) + return } else if background == "" || background == Transparent { ansiColor := a.getAnsiFromColorString(foreground, false) - coloredText = fmt.Sprintf(a.formats.colorSingle, ansiColor, text) - } else if foreground != "" && background != "" { - bgAnsiColor := a.getAnsiFromColorString(background, true) - fgAnsiColor := a.getAnsiFromColorString(foreground, false) - coloredText = fmt.Sprintf(a.formats.colorFull, bgAnsiColor, fgAnsiColor, text) + coloredText := fmt.Sprintf(a.formats.colorSingle, ansiColor, text) + a.builder.WriteString(coloredText) + return } + bgAnsiColor := a.getAnsiFromColorString(background, true) + fgAnsiColor := a.getAnsiFromColorString(foreground, false) + coloredText := fmt.Sprintf(a.formats.colorFull, bgAnsiColor, fgAnsiColor, text) a.builder.WriteString(coloredText) } diff --git a/src/main.go b/src/main.go index 8bc3f27c..c09ade2e 100644 --- a/src/main.go +++ b/src/main.go @@ -147,8 +147,10 @@ func main() { renderer := &AnsiRenderer{ formats: formats, } + fmt.Println("echo \"", settings.TerminalBackground, "\"") colorer := &AnsiColor{ - formats: formats, + formats: formats, + terminalBackground: settings.TerminalBackground, } title := &consoleTitle{ env: env, diff --git a/src/settings.go b/src/settings.go index d3c741c7..da57d757 100644 --- a/src/settings.go +++ b/src/settings.go @@ -15,6 +15,7 @@ type Settings struct { ConsoleTitle bool `json:"console_title"` ConsoleTitleStyle ConsoleTitleStyle `json:"console_title_style"` ConsoleTitleTemplate string `json:"console_title_template"` + TerminalBackground string `json:"terminal_background"` Blocks []*Block `json:"blocks"` } diff --git a/themes/schema.json b/themes/schema.json index f6eedec6..bde3f51c 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -1414,6 +1414,7 @@ "description": "https://ohmyposh.dev/docs/configure#console-title-template", "default": "{{ .Shell }} in {{ .Folder }}" }, + "terminal_background": { "$ref": "#/definitions/color" }, "blocks": { "type": "array", "title": "Block array",