From 0d1d3e8398fd0d7844edf1e2ad13d1adc245de17 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Wed, 18 May 2016 16:09:42 +0100 Subject: [PATCH 1/1] Read existing palette colour. --- opt/bin/ucolour | 67 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/opt/bin/ucolour b/opt/bin/ucolour index 2b5fc1f..5cfc19f 100755 --- a/opt/bin/ucolour +++ b/opt/bin/ucolour @@ -14,8 +14,57 @@ # -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= @@ -25,18 +74,17 @@ cu= mo= pc= ps= -tmux_prefix= while getopts ":B:F:P:b:c:d:f:m:p:" opt; do case $opt in - B) BG=$OPTARG;; - F) FG=$OPTARG;; + B) BG=$(parse_colour $OPTARG);; + F) FG=$(parse_colour $OPTARG);; P) pc=$OPTARG;; - b) bg=$OPTARG;; - c) cu=$OPTARG;; - d) bd=$OPTARG;; - f) fg=$OPTARG;; - m) mo=$OPTARG;; - p) ps=$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)) @@ -64,7 +112,6 @@ if [ -z "$BG$FG$bd$bg$cu$fg$mo$pc" ]; then 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" -- 2.7.4