Some background for any who find this later
This is a question about colors being applied in the shell. NOT about how to CHANGE said colors (there's a bajillion answers for that). But how to READ them.
I know that a shell really doesn't define \e[31m
to mean "red"
, but rather, represent whatever color the user happens to have slotted into the "red"
slot in their terminal. This is what allows for the various color schemes/themes that a user can optionally apply to their terminal emulator application.
My question(s)
That said, is there any mechanism through which I can ascertain what color the user is... well... using for that slot? That is to say, if a user hypothetically is using (I'm gonna use hex here as a convenient descriptor; I don't care if it's in this format) #F00
(#FF0000
for you web guys) as their "red"
slot, is there a way I can see that value? I mean, a user COULD put in #00F
and render all things red as blue. That's totally their prerogative and business, but is there some means through which I can see that?
How about just in terms of the shell ("their active foreground color is the color located in the "white"
slot), even if I don't get to know what that color is, beyond the slot identifier?
Likewise, is there a way to reset JUST the foreground and NOT the background? Or vice-versa? Or is it \e[0m
or nothin'?
xtermcontrol --get-color1
for instance for those terminals that support those xterm sequences. – Stéphane Chazelas Jun 12 '23 at 12:49\e[39m
and\e[49m
, respectively. (Note that\e[0m
not only resets the foreground and background but a whole lot of other attributes as well.) – egmont Jun 12 '23 at 17:32ls
,grep
,apt
etc. None of them care to restore the previous color. And all of them produce "broken" (at least inconsistent, ugly) output if they start with colors other than the default. Follow what they are doing. Assume that your app starts with the default colors, and restore the default ones before you quit. The rest is not your app's problem. Don't overengineer it beyond the extent that standard utilities do. – egmont Jun 12 '23 at 21:43\e7
(DECSC) saves the cursor, as in its location as well as the currently chosen attributes,\e8
(DECRC) restores it. It's quite widely supported, but restoring the cursor position might not be what you want. Relatively recent\e[#{
(XTPUSHSGR) saves the attributes only and\e[#}
(XTPOPSGR) restores them. This might be exactly what you're looking for, except that it's only supported by a few terminal emulators, includingxterm
where this originated from. Then your boss should use such a terminal, or pay you to implement this feature in his/her favorite terminal emulator :P – egmont Jun 13 '23 at 05:19xterm
, I don't think too many. Good luck reliably reading an asynchronous response which you cannot be certain if is going to arrive or not; though!) – egmont Jun 13 '23 at 05:23