Skip to content

Instantly share code, notes, and snippets.

@blueyed
Last active March 19, 2024 01:02
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save blueyed/c8470c2aad3381c33ea3 to your computer and use it in GitHub Desktop.
Save blueyed/c8470c2aad3381c33ea3 to your computer and use it in GitHub Desktop.
Query background setting from terminal (xterm / rxvt-unicode)
#!/bin/sh
#
# Query a property from the terminal, e.g. background color.
#
# XTerm Operating System Commands
# "ESC ] Ps;Pt ST"
oldstty=$(stty -g)
# What to query?
# 11: text background
Ps=${1:-11}
stty raw -echo min 0 time 0
# stty raw -echo min 0 time 1
printf "\033]$Ps;?\033\\"
# xterm needs the sleep (or "time 1", but that is 1/10th second).
sleep 0.00000001
read -r answer
# echo $answer | cat -A
result=${answer#*;}
stty $oldstty
# Remove escape at the end.
echo $result | sed 's/[^rgb:0-9a-f/]\+$//'
@SinisterStairs
Copy link

Thanks! I'm using this query to change things like LS_COLORS and PS1 based on the terminal's background color (which automatically switches).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment