Skip to content

Commit

Permalink
feat: set terminal background color
Browse files Browse the repository at this point in the history
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 microsoft/vscode#111762
and microsoft/terminal#7014
  • Loading branch information
JanDeDobbeleer committed Mar 14, 2021
1 parent a0e18e9 commit 4d628a3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 19 additions & 9 deletions src/ansi_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
}

Expand Down
4 changes: 3 additions & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
1 change: 1 addition & 0 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4d628a3

Please sign in to comment.