0

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'?

NerdyDeeds
  • 121
  • 5
  • 1
    xtermcontrol --get-color1 for instance for those terminals that support those xterm sequences. – Stéphane Chazelas Jun 12 '23 at 12:49
  • @StéphaneChazelas Right, but what color is actively IN USE? – NerdyDeeds Jun 12 '23 at 13:23
  • 1
    "Likewise, is there a way to reset JUST the foreground and NOT the background? Or vice-versa?" - Yes, \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:32
  • "but what color is actively IN USE?" - I'm not aware of any way to query that, and you shouldn't care. It's a silent agreement between apps that they get a "clean" state (default values) on startup and they should restore that when the app quits. And the program can itself keep track of the selected color if it changes that. Also there's a way to save and restore the attributes, just in case you cannot rely on the default being the active one at the start of your app and you need to restore that. What's your use case where you would need this feature? – egmont Jun 12 '23 at 17:39
  • @egmont In this particular case, I was seeking to be able to query the current color in use, that I might change to another and change BACK to whatever it was before I mucked with it. I know I can simply overwrite the current color, but, if the text output is currently red, and I switch it to green, then, reset or not, I have no way of programmatically knowing that it should return to red again. This surprised me, insofar as I'm an old hand at shell scripting, and it dawned on me I'd never had the need to do so, before, much less attempted to. I hoped someone might have the knowledge to share. – NerdyDeeds Jun 12 '23 at 21:17
  • Take a look at coloring utilities such as ls, 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
  • @egmont Oh, I'm in total agreement with you, there. The problem is that my BOSS doesn't share our viewpoint, and selfishly seems to think that "Nuh-uh! Not gonna do it!" isn't a valid position to take on the matter. – NerdyDeeds Jun 12 '23 at 22:46
  • 1
    \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, including xterm 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:19
  • 1
    I've just realized that this question has been marked as a duplicate, and over there an escape sequence is shown that lets you query the currently selected color slot, what you're looking for. I wasn't aware of that sequence. (I don't know how many terminal emulators support that apart from xterm, 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
  • 1
    @egmont Indeed it was, meaning this is just me saying thank you for the data you did provide. I cannot leverage it directly into a solution, but it DOES suggest another avenue. Thank you, sincerely, for taking the time. <3 – NerdyDeeds Jun 14 '23 at 17:22

0 Answers0