X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=opt%2Fbin%2Fucolour;h=5cfc19f5185b326945782f400526699e875328f1;hb=2f4249cac9aa299fd3e2c36b7b6e17c19ff92854;hp=796afdaedf953503812fff02c11f6e1f7cd900e6;hpb=6c50c01c0cb5486964d139dd4f12b15464d984ec;p=profile.git diff --git a/opt/bin/ucolour b/opt/bin/ucolour index 796afda..5cfc19f 100755 --- a/opt/bin/ucolour +++ b/opt/bin/ucolour @@ -6,46 +6,118 @@ # Options: -b Set background. # -B Set highlight background. # -c Set cursor. +# -d Set border. # -f Set foreground. # -F Set highlight foreground. # -m Set mouse. +# -P Palette index. +# -p Set palette reference with -P. +# Notes: Border defaults to background. +# The -P and -p flags must be used together. +# Colour spec can be a simple number, to copy the +# existing palette entry for that index. # +PROG=ucolour +tmux_prefix= +[ -n "$TMUX" ] && tmux_prefix="\033Ptmux;\033" + +function parse_colour() { + local arg=$1; shift + local safe=${arg//[1-9]/} + + if [ "$safe" = "0" -o -z "$safe" ]; then + # Hack to get existing. + local index=$( + local index + exec < /dev/tty + local oldstty=$(stty -g) + stty raw -echo min 0 + echo -en "$tmux_prefix\033]4;$arg;?\007" > /dev/tty + local ret=1 + local tries=0 + while [ $ret -gt 0 ]; do + tries=$((tries+1)) + IFS='' read -t 0 + ret=$? + if [ $tries -gt 2 ]; then + [ $tries -gt 3 ] && break + sleep 1 + fi + done + IFS='' read -r index + stty $oldstty + echo "$index" + ) + if [ -n "$index" ]; then + index=${index##*;} + index=${index%?} + echo "$index" + echo >&2 "$PROG: Colour $arg is $index." + return 0 + fi + else + echo "$arg" + return 0 + fi + + echo "" + return 1 +} + BG= FG= bg= +bd= fg= cu= mo= -tmux_prefix= -while getopts ":B:F:b:c:f:m:" opt; do +pc= +ps= +while getopts ":B:F:P:b:c:d:f:m:p:" opt; do case $opt in - B) BG=$OPTARG;; - F) FG=$OPTARG;; - b) bg=$OPTARG;; - c) cu=$OPTARG;; - f) fg=$OPTARG;; - m) mo=$OPTARG;; + B) BG=$(parse_colour $OPTARG);; + F) FG=$(parse_colour $OPTARG);; + P) pc=$OPTARG;; + b) bg=$(parse_colour $OPTARG);; + c) cu=$(parse_colour $OPTARG);; + d) bd=$(parse_colour $OPTARG);; + f) fg=$(parse_colour $OPTARG);; + m) mo=$(parse_colour $OPTARG);; + p) ps=$(parse_colour $OPTARG);; esac done shift $((OPTIND-1)) -if [ -z "$BG$FG$bg$cu$fg$mo" ]; then +if [ -n "$pc" -o -n "$ps" ]; then + if [ -z "$pc" -o -z "$ps" ]; then + echo >&2 "$PROG: Must use -P and -p together." + exit 1 + fi +fi +[ -n "$bg" -a -z "$bd" ] && bd=$bg +if [ -z "$BG$FG$bd$bg$cu$fg$mo$pc" ]; then echo >&2 "Usage: ucolour option [option ...]" echo >&2 "Options: -b Set background." echo >&2 " -B Set highlight background." echo >&2 " -c Set cursor." + echo >&2 " -d Set border." echo >&2 " -f Set foreground." echo >&2 " -F Set highlight foreground." echo >&2 " -m Set mouse." + echo >&2 " -P Palette index." + echo >&2 " -p Set palette reference with -P." + echo >&2 "Notes: Border defaults to background." + echo >&2 " The -P and -p flags must be used together." exit 1 fi -[ -n "$TMUX" ] && tmux_prefix="\033Ptmux;\033" [ -n "$BG" ] && echo -en "$tmux_prefix\033]17;$BG\007" [ -n "$FG" ] && echo -en "$tmux_prefix\033]19;$FG\007" [ -n "$bg" ] && echo -en "$tmux_prefix\033]11;$bg\007" +[ -n "$bd" ] && echo -en "$tmux_prefix\033]708;$bd\007" [ -n "$cu" ] && echo -en "$tmux_prefix\033]12;$cu\007" [ -n "$fg" ] && echo -en "$tmux_prefix\033]10;$fg\007" [ -n "$mo" ] && echo -en "$tmux_prefix\033]13;$mo\007" +[ -n "$pc" ] && echo -en "$tmux_prefix\033]4;$pc;$ps\007" exit 0