Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to setup the prompt colors in .bashrc depending on the colors it is using for foreground and background. For example, blue prompt if background is light and beige if it is dark.

Is there a way to find out the current settings in a script?

share|improve this question

3 Answers 3

The email back from Thomas Dickey (xterm's maintainer) has this. Note in particular the part about "?". The Ps = 4 refers to (OSC Ps ; Pt ST) where OSC (the "Operating System Control" prefix) is ESC ] and ST (the "String Terminator" suffix) is backslash. The "4" is one of the possible subcommands to OSC.

"For the whole palette, that can be set/retrieved using the 88/256 color extension. In ctlseqs.txt, it's summarized here:

  Ps = 4  ; c ; spec -> Change Color Number c to the color
  specified by spec.  This can be a name or RGB specification as
  per XParseColor.  Any number of c name pairs may be given.
  The color numbers correspond to the ANSI colors 0-7, their
  bright versions 8-15, and if supported, the remainder of the
  88-color or 256-color table.

  If a "?" is given rather than a name or RGB specification,
  xterm replies with a control sequence of the same form which
  can be used to set the corresponding color.  Because more than
  one pair of color number and specification can be given in one
  control sequence, xterm can make more than one reply."

A bit later in the docs are more OSC subcommands, Ps = 10 and Ps = 11, and others.

  Ps = 1 0  -&gt; Change VT100 text foreground color to Pt.<br/>
  Ps = 1 1  -&gt; Change VT100 text background color to Pt.

Example - this queries the background using Ps = "11" (from just above) and Pt = "?", plugged into the (OSC Ps ; Pt ST). In the echo, \033 is being used for escape, and \\ for the final backslash.

echo -en "\033]11;?\033\\"

Output:

^[]11;rgb:0000/0000/0000^[\ 

Warning: The returned color does not reflect whether reverse video, like -rv, is enabled, and crawling through the ~260 colors available via OSC 4 ; c ; ? ST doesn't show any that both follow the background AND change with reverse video. Since many users set a dark background by using just "xterm -rv", this complicates determining whether the background is actually dark or not. Most colors don't adjust to -rv, either.

A script to do the full query and actually capture the reply from xterm:

#!/bin/bash
success=false
exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
col=11      # background
#          OSC   Ps  ;Pt ST
echo -en "\033]${col};?\033\\" >/dev/tty  # echo opts differ w/ OSes
result=
if IFS=';' read -r -d '\' color ; then
    result=$(echo $color | sed 's/^.*\;//;s/[^rgb:0-9a-f/]//g')
    success=true
fi
stty $oldstty
echo $result
$success
share|improve this answer
    
Wow...a response after two years...thank you. I didn't quite understand the explanation but the example does nothing for me...echoes nothing. –  Miserable Variable Mar 29 '12 at 17:08
    
Heh. I'm re-running it in a pretty recent xterm - an actual xterm - this time with all my X resources stripped out (which means it yields all "f"s for the colors instead of all "0"s). Using either /bin/echo or bash's builtin echo works fine. The example I tried was copy/pasted directly from the post above. My environment is Ubuntu 11.10 (a Linux/Debian derivative). I don't have a different OS handy to test. –  Alex North-Keys Mar 29 '12 at 18:23
    
Depending on how your prompt is configured, there's a chance that it's landing on top of xterm's attempt to stuff input into your terminal in response to your query. From your shell's perspective, xterm's reply just looks like you, typing. –  Alex North-Keys Mar 29 '12 at 18:45
    
Previously I had set PS1 to \$> when I did not see any output. Now I tried $> /bin/echo -en "\033]11;?\033\\" | od -cx and exactly the same string on my terminal: `0000000 033 ] 1 1 ; ? 033 ` –  Miserable Variable Mar 29 '12 at 20:27
    
Hmm, gnome terminal in Ubuntu 12.04 claims to be xterm-compatible, but doesn't print anything back. Why is that? –  Nik Reiman Sep 21 '12 at 13:03

kind of

put the settings into your ~/.Xdefaults file:

xterm*foreground: blue
xterm*background: white

in your shell you just grep the values:

awk '/xterm\*foreground:(.*)/ { print $2 }' < .Xdefaults

otherwise it's pretty hard to get some internal values of the xterm.

share|improve this answer
    
I am not even using X :) I am using an rxvt from cygwin that runs without an X server. Besides I would like to be able to use different colors on different hosts and for different tasks, which requires that I query the termincal for settings. –  Miserable Variable Jun 28 '10 at 6:56
    
well, you didnt specify your OS :) –  akira Jun 28 '10 at 7:14
    
sorry about that. The reason I did not specify is because I had expected the solution to be based on echoing some magic strings and get some info back on stdout. –  Miserable Variable Jun 28 '10 at 7:20
    
well, what you "echo" normally stays inside the shell (bash, zsh), it does not go to xterm (except in the final step when it comes to displaying the output of the commands). –  akira Jun 28 '10 at 8:13
    
-1 awk is the wrong tool for the job. –  g33kz0r Aug 20 '11 at 9:36

Actually I think you want this:

% xrdb -query

That will list the settings for you. See also:

http://docstore.mik.ua/orelly/unix3/upt/ch06_08.htm

To modify the runtime, use:

% echo "some*setting: somevalue" | xrdb -merge
share|improve this answer
    
-query lists all resources. specifying a resource does not work on my ubuntu. –  akira Aug 20 '11 at 10:46
    
echo "whatever" | xrdb -merge –  g33kz0r Aug 20 '11 at 13:32
    
reread the question: OP wants to set the colors of the BASH PROMPT according to the settings of the xterm. OP does not want to change the xresources. –  akira Aug 20 '11 at 14:47
    
See his comment to your answer. He wants to "query the termincal [sic]". Thus, he wants xrdb -query –  g33kz0r Aug 20 '11 at 19:07
    
if you want to be that specific: he wants to query rxvt withtout a running xserver on windows (which is possible) and xrdb -query just gives you the list of all resources .. which you have to awk / grep again to get to the foreground. and thats what i admitted already. xrdb -merge is completely offtopic here coz OP does not want to modify the xresources but the appearance of the bashprompt. –  akira Aug 20 '11 at 20:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.