2

NB, the question isn't about how to set colors, as such. It's specificially and explicitly targeting color selection using Set-PSReadLineOption and no other method whatsoever, including 3rd party tools and what not. Furthermore, it includes altering the color of both (the text as well as the background) and requires an established syntax (such as RGB or HSV) allowing arbitrary manipulation (i.e. no enumerated, predefined literals). And yes, simply it can't be done, go away will be an acceptable, if not happily appreciated, reply. By other words, as far as the scope of this question goes, I'm not interested in alternative ways to affect colors in my window.

Currently I have setup like this.

$options = @{
  HistoryNoDuplicates = $true
  ...
  Colors = @{
    "Selection" = "#bbdd99"
    ...
  }
}
Set-PSReadLineOption @options

It works and I'm happy with it. However, I'd like to control the background of the selected text as well. According to the docs for PSReadLineOption it's suppose to be possible and the exact example of such is provided right here.

Set-PSReadLineOption -Colors @{ "Comment"="`e[32;47m" }

There's even another, more sophisticated, example here (the second item, for "string").

Set-PSReadLineOption -Colors @{ "String" = "$([char]0x1b)[38;5;100m" }

That's great but I simply can't for the life of me grasp how to translate that weirdly unusual syntax into the hexa-values (which a normal person can handle) for colors to be able to alter the background as well. And in case it's that weirdo for syntax that is an absolute requirement for altering the background's color, I'm equally clueless how to translate two hexadecimal values (one for the text, one for the back) into the form I'm using on my system (the first sample with @options variable).

Who came up with that syntax anyway? Are we to understand that MS doesn't want us to horse around with colors or something?!

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 2
    That weirdo syntax is a [virtual terminal sequence](https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#extended-colors), specifically `ESC[38;5;100m` to set the foreground color from an index table. More details [here](https://chrisyeh96.github.io/2020/03/28/terminal-colors.html). MS did not come up with this syntax, it's borrowed from existing (Unix) terminals, which also explains the use of color palettes rather than direct RGB values (it evolved along with terminal capabilities). – Jeroen Mostert Aug 07 '21 at 10:37
  • 1
    A full sequence to set both foreground and background using RGB syntax would be something like `@{ "Selection" = "$([char]0x1b)[48;2;255;0;0m$([char]0x1b)[38;2;255;255;0m" }` (to make it a lovely yellow on red). AFAICT this only supports decimal values (and since this is not a standardized sequence, it may not work on all terminals; it works on Windows at least). – Jeroen Mostert Aug 07 '21 at 10:54
  • @JeroenMostert I can live at peace with DEC instead of HEX, just as long I get to alter both text and back arbitrarily. Just to be clear - it's the part that is `255;0;0` and `255;255;0` that corresponds to *red* and *yellow*, the rest being fill-ins for controlling where the colors hit. Is that correctly understood? If so, feel free to post it as an answer to be accepted. I'm going to refrain from passing my opinion on borrowing stuff from other platforms without any attempt to improve usability, coherency and sanity, let alone examples. – Konrad Viltersten Aug 07 '21 at 15:17
  • Well, your real complaint is about PSReadLine not offering a native syntax for it -- that the Windows terminal devs used sequences as-is is by design, since it directly enables Unix/POSIX applications to work. The PSReadLine devs could of course have made something to wrap around these sequences and emit them instead of requiring you to write them out. This seems to be discussed [here](https://github.com/PowerShell/PowerShell/issues/2381). The lack of feedback seems to suggest they're fine with leaving things to the terminal. It also links to [Pansies](https://github.com/PoshCode/Pansies). – Jeroen Mostert Aug 07 '21 at 16:28
  • Pansies is neat since it's basically the wrapper you want -- it allows you to do `Set-PSReadLineOption -Colors @{ "Selection" = (Text "" -Fg "#ffff00" -Bg "#ff0000" -LeaveColor).ToString() }` to achieve the same effect without writing out the escape sequences manually. Of course you do need to take a dependency on a module, but typically changing things like selection colors is something you only do on your local installation anyway. – Jeroen Mostert Aug 07 '21 at 16:41
  • Here's a way to do it using RGB: https://stackoverflow.com/a/63083283/6654942 – js2010 Aug 07 '21 at 17:00
  • @JeroenMostert Yes, you're right. I just bitching a bit when people have opinions differing from mine. Nothing to worry about. If you'd be kind to grasp all the great info and throw it into a answer that I could accept, that would be great. (I can write an own answer based in your input but it's tacky and I don't like doing so. Also, you won't det the rep, which is the only reason we're doing this since this is how one goes to heaven, right?) – Konrad Viltersten Aug 07 '21 at 18:52

0 Answers0