jsonpowershellwindows-terminaloh-my-posh

Oh My Posh custom theme backgound_template color being ignored


I made an Oh My Posh custom theme for my PowerShell prompt. All the code works as expected, except background_template condition is not able to set the background color when the condition is true. It correctly detects the condition as true, however, completely ignores the color value #ff0000 (RED). What is the correct way to do this?

Expected Behavior:

enter image description here

Actual Behavior:

enter image description here

Code:

{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [
    {
      "alignment": "left",
      "segments": [
        {
          "background": "#42E66C",
          "background_templates": [
            "{{ if eq .Env.ElevatedPINNotAuthorized \"True\" }} #ff0000 {{ end }}"
          ],
          "foreground": "#ffffff",
          "leading_diamond": "\ue0b6",
          "style": "diamond",
          "template": " {{ if .WSL }}WSL at {{ end }}{{.Icon}} ",
          "type": "os"
        },
        {
          "background": "#42E66C",
          "background_templates": [
            "{{ if eq .Env.ElevatedPINNotAuthorized \"True\" }} #ff0000 {{ end }}"
          ],
          "foreground": "#ffffff",
          "style": "powerline",
          "template": " {{ .Env.CyberCredential }} ",
          "type": "session"
        },
        {
          "background": "lightBlue",
          "foreground": "black",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "style": "full"
          },
          "style": "powerline",
          "template": " \ue5ff {{ .Path }} ",
          "type": "path"
        },
        {
          "background": "#ec9706",
          "foreground": "#100e23",
          "leading_diamond": "<transparent,background>\ue0b0</>",
          "properties": {
            "always_enabled": true
          },
          "style": "diamond",
          "template": " \ufbab {{ .FormattedMs }}\u2800",
          "trailing_diamond": "\ue0b0",
          "type": "executiontime"
        }
      ],
      "type": "prompt"
    }
  ],
  "final_space": true,
  "version": 2
}

I even tried a simpler script (below) using else. However, it doesn't work correctly at all:

{
  "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
  "blocks": [
    {
      "alignment": "left",
      "segments": [
        {
          "background_templates": [
            " {{if eq .Env.ElevatedPINNotAuthorized \"True\" }}#ff0000{{ else }}#42E66C{{ end }}"
          ],
          "foreground": "#ffffff",
          "leading_diamond": "\ue0b6",
          "style": "diamond",
          "template": " {{ if .WSL }}WSL at {{ end }}{{.Icon}} ",
          "type": "os"
        },
        {
          "background_templates": [
            " {{if eq .Env.ElevatedPINNotAuthorized \"True\" }}#ff0000{{ else }}#42E66C{{ end }}"
          ],
          "foreground": "#ffffff",
          "style": "powerline",
          "template": " {{ .Env.CyberCredential }} ",
          "type": "session"
        },
        {
          "background": "lightBlue",
          "foreground": "black",
          "powerline_symbol": "\ue0b0",
          "properties": {
            "style": "full"
          },
          "style": "powerline",
          "template": " \ue5ff {{ .Path }} ",
          "type": "path"
        },
        {
          "background": "#ec9706",
          "foreground": "#100e23",
          "leading_diamond": "<transparent,background>\ue0b0</>",
          "properties": {
            "always_enabled": true
          },
          "style": "diamond",
          "template": " \ufbab {{ .FormattedMs }}\u2800",
          "trailing_diamond": "\ue0b0",
          "type": "executiontime"
        }
      ],
      "type": "prompt"
    }
  ],
  "final_space": true,
  "version": 2
}

Solution

  • Your background template has a leading and trailing space in the resulting color string which evaluates as invalid. As we do not trim (and not plan to), removing the spaces will result in a working setup:

    "background_templates": [
        "{{ if eq .Env.ElevatedPINNotAuthorized \"True\" }}#ff0000{{ end }}"
    ],