Powershell >6.0... command line background color with ANSI sequences (PSReadLine) ?how?

356 Views Asked by At

In PowerShell >6.0 I can change the command line FOREGROUND colors with:

Set-PSReadLineOption -Colors @{ Keyword="#0FAFE0"; Variable="#987ABC" }+

but how do I change the BACKGROUND colors with RGB??? (#RRGGBB)
i can see some examples with ASCII console sequences
but none with the RGB format

1

There are 1 best solutions below

4
On

I don't think you can? SelectionColor uses the ansi escape code for black on white: "`e[30;47m" (ps 7 for the `e). https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

How about this from that wikipeda page:

ESC[ 38;2;⟨r⟩;⟨g⟩;⟨b⟩ m -- choose RGB foreground color

ESC[ 48;2;⟨r⟩;⟨g⟩;⟨b⟩ m -- choose RGB background color

Red foreground (255 0 0) blue (0 0 255) background.

Set-PSReadLineOption -Colors @{ variable = "`e[38;2;255;0;0m" + # fg
                                           "`e[48;2;0;0;255m" } # bg

In ps5 you have to say $([char]0x1b) instead of `e.

$e = [char]0x1b
Set-PSReadLineOption -Colors @{ variable = "$e[38;2;255;0;0m" + # fg
                                           "$e[48;2;0;0;255m" } # bg