Kubernetes stuff.
[profile.git] / opt / bin / ucolour
1 #!/bin/bash
2 #
3 # ucolour: Set urxvt (or terminal which understands the same escape sequences)
4 #          colours.
5 # Usage: ucolour option <colour> [option <colour> ...]
6 # Options: -b   Set background.
7 #          -B   Set highlight background.
8 #          -c   Set cursor.
9 #          -d   Set border.
10 #          -f   Set foreground.
11 #          -F   Set highlight foreground.
12 #          -m   Set mouse.
13 #          -P   Palette index.
14 #          -p   Set palette reference with -P.
15 # Notes: Border defaults to background.
16 #        The -P and -p flags must be used together.
17 #        Colour spec can be a simple number, to copy the
18 #        existing palette entry for that index. 
19 #
20
21 PROG=ucolour
22 tmux_prefix=
23 [ -n "$TMUX" ] && tmux_prefix="\033Ptmux;\033"
24
25 function parse_colour() {
26   local arg=$1; shift
27   local safe=${arg//[1-9]/}
28
29   if [ "$safe" = "0" -o -z "$safe" ]; then
30     # Hack to get existing.
31     local index=$(
32       local index
33       exec < /dev/tty
34       local oldstty=$(stty -g)
35       stty raw -echo min 0
36       echo -en "$tmux_prefix\033]4;$arg;?\007" > /dev/tty
37       local ret=1
38       local tries=0
39       while [ $ret -gt 0 ]; do
40         tries=$((tries+1))
41         IFS='' read -t 0
42         ret=$?
43         if [ $tries -gt 2 ]; then
44           [ $tries -gt 3 ] && break
45           sleep 1
46         fi
47       done
48       IFS='' read -r index
49       stty $oldstty
50       echo "$index"
51     )
52     if [ -n "$index" ]; then
53       index=${index##*;}
54       index=${index%?}
55       echo "$index"
56       echo >&2 "$PROG: Colour $arg is $index."
57       return 0
58     fi
59   else
60     echo "$arg"
61     return 0
62   fi
63
64   echo ""
65   return 1
66 }
67
68 BG=
69 FG=
70 bg=
71 bd=
72 fg=
73 cu=
74 mo=
75 pc=
76 ps=
77 while getopts ":B:F:P:b:c:d:f:m:p:" opt; do
78   case $opt in
79     B) BG=$(parse_colour $OPTARG);;
80     F) FG=$(parse_colour $OPTARG);;
81     P) pc=$OPTARG;;
82     b) bg=$(parse_colour $OPTARG);;
83     c) cu=$(parse_colour $OPTARG);;
84     d) bd=$(parse_colour $OPTARG);;
85     f) fg=$(parse_colour $OPTARG);;
86     m) mo=$(parse_colour $OPTARG);;
87     p) ps=$(parse_colour $OPTARG);;
88   esac
89 done
90 shift $((OPTIND-1))
91
92 if [ -n "$pc" -o -n "$ps" ]; then
93   if [ -z "$pc" -o -z "$ps" ]; then
94     echo >&2 "$PROG: Must use -P and -p together."
95     exit 1
96   fi
97 fi
98 [ -n "$bg" -a -z "$bd" ] && bd=$bg
99 if [ -z "$BG$FG$bd$bg$cu$fg$mo$pc" ]; then
100   echo >&2 "Usage: ucolour option <colour> [option <colour> ...]"
101   echo >&2 "Options: -b   Set background."
102   echo >&2 "         -B   Set highlight background."
103   echo >&2 "         -c   Set cursor."
104   echo >&2 "         -d   Set border."
105   echo >&2 "         -f   Set foreground."
106   echo >&2 "         -F   Set highlight foreground."
107   echo >&2 "         -m   Set mouse."
108   echo >&2 "         -P   Palette index."
109   echo >&2 "         -p   Set palette reference with -P."
110   echo >&2 "Notes: Border defaults to background."
111   echo >&2 "       The -P and -p flags must be used together."
112   exit 1
113 fi
114
115 [ -n "$BG" ] && echo -en "$tmux_prefix\033]17;$BG\007"
116 [ -n "$FG" ] && echo -en "$tmux_prefix\033]19;$FG\007"
117 [ -n "$bg" ] && echo -en "$tmux_prefix\033]11;$bg\007"
118 [ -n "$bd" ] && echo -en "$tmux_prefix\033]708;$bd\007"
119 [ -n "$cu" ] && echo -en "$tmux_prefix\033]12;$cu\007"
120 [ -n "$fg" ] && echo -en "$tmux_prefix\033]10;$fg\007"
121 [ -n "$mo" ] && echo -en "$tmux_prefix\033]13;$mo\007"
122 [ -n "$pc" ] && echo -en "$tmux_prefix\033]4;$pc;$ps\007"
123 exit 0