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
This commit is contained in:
Jan De Dobbeleer 2021-03-14 14:14:08 +01:00 committed by Jan De Dobbeleer
parent a0e18e998c
commit e6247da593
5 changed files with 26 additions and 10 deletions

View file

@ -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: `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_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"` - 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 > "I Like The Way You Speak Words" - Gary Goodspeed

View file

@ -45,8 +45,9 @@ func getColorFromName(colorName string, isBackground bool) (string, error) {
// AnsiColor writes colorized strings // AnsiColor writes colorized strings
type AnsiColor struct { type AnsiColor struct {
builder strings.Builder builder strings.Builder
formats *ansiFormats formats *ansiFormats
terminalBackground string
} }
const ( const (
@ -71,18 +72,27 @@ func (a *AnsiColor) writeColoredText(background, foreground, text string) {
if text == "" { if text == "" {
return 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 != "" { if foreground == Transparent && background != "" {
ansiColor := a.getAnsiFromColorString(background, false) 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 { } else if background == "" || background == Transparent {
ansiColor := a.getAnsiFromColorString(foreground, false) ansiColor := a.getAnsiFromColorString(foreground, false)
coloredText = fmt.Sprintf(a.formats.colorSingle, ansiColor, text) coloredText := fmt.Sprintf(a.formats.colorSingle, ansiColor, text)
} else if foreground != "" && background != "" { a.builder.WriteString(coloredText)
bgAnsiColor := a.getAnsiFromColorString(background, true) return
fgAnsiColor := a.getAnsiFromColorString(foreground, false)
coloredText = fmt.Sprintf(a.formats.colorFull, bgAnsiColor, fgAnsiColor, text)
} }
bgAnsiColor := a.getAnsiFromColorString(background, true)
fgAnsiColor := a.getAnsiFromColorString(foreground, false)
coloredText := fmt.Sprintf(a.formats.colorFull, bgAnsiColor, fgAnsiColor, text)
a.builder.WriteString(coloredText) a.builder.WriteString(coloredText)
} }

View file

@ -147,8 +147,10 @@ func main() {
renderer := &AnsiRenderer{ renderer := &AnsiRenderer{
formats: formats, formats: formats,
} }
fmt.Println("echo \"", settings.TerminalBackground, "\"")
colorer := &AnsiColor{ colorer := &AnsiColor{
formats: formats, formats: formats,
terminalBackground: settings.TerminalBackground,
} }
title := &consoleTitle{ title := &consoleTitle{
env: env, env: env,

View file

@ -15,6 +15,7 @@ type Settings struct {
ConsoleTitle bool `json:"console_title"` ConsoleTitle bool `json:"console_title"`
ConsoleTitleStyle ConsoleTitleStyle `json:"console_title_style"` ConsoleTitleStyle ConsoleTitleStyle `json:"console_title_style"`
ConsoleTitleTemplate string `json:"console_title_template"` ConsoleTitleTemplate string `json:"console_title_template"`
TerminalBackground string `json:"terminal_background"`
Blocks []*Block `json:"blocks"` Blocks []*Block `json:"blocks"`
} }

View file

@ -1414,6 +1414,7 @@
"description": "https://ohmyposh.dev/docs/configure#console-title-template", "description": "https://ohmyposh.dev/docs/configure#console-title-template",
"default": "{{ .Shell }} in {{ .Folder }}" "default": "{{ .Shell }} in {{ .Folder }}"
}, },
"terminal_background": { "$ref": "#/definitions/color" },
"blocks": { "blocks": {
"type": "array", "type": "array",
"title": "Block array", "title": "Block array",