![]() search |
TALISMAN
general
Information
Unix server |
|
Xterm color queries
|
Erlkönig: Xterm color queriesXterm color queries in generalFor querying an xterm for its current color settings, the relevant section of the ctlseqs.txt file follows. 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.
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, and there's a chance they were set by the user, and so won't reflect the defaults with/without -rv. Two obvious possibilities are:
Querying xterm's background from a shell scriptSome tweaking of the tty is required to do querying from the shell: (based on a script from http://invisible-island.net/xterm/xterm.faq.html). Here's an example in a toy script named xterm-bg #!/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 Demonstration in an xterm with a black background: $ ./xterm-bg rgb:0000/0000/0000 $ Demonstration in an xterm with a dark red background (xterm -bg rgbi:.3/0.0): $ ./xterm-bg rgb:5858/0000/0000 $ |