Allow overriding colours in uxrvt palette.
[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 #
18
19 BG=
20 FG=
21 bg=
22 bd=
23 fg=
24 cu=
25 mo=
26 pc=
27 ps=
28 tmux_prefix=
29 while getopts ":B:F:P:b:c:d:f:m:p:" opt; do
30   case $opt in
31     B) BG=$OPTARG;;
32     F) FG=$OPTARG;;
33     P) pc=$OPTARG;;
34     b) bg=$OPTARG;;
35     c) cu=$OPTARG;;
36     d) bd=$OPTARG;;
37     f) fg=$OPTARG;;
38     m) mo=$OPTARG;;
39     p) ps=$OPTARG;;
40   esac
41 done
42 shift $((OPTIND-1))
43
44 if [ -n "$pc" -o -n "$ps" ]; then
45   if [ -z "$pc" -o -z "$ps" ]; then
46     echo >&2 "$PROG: Must use -P and -p together."
47     exit 1
48   fi
49 fi
50 [ -n "$bg" -a -z "$bd" ] && bd=$bg
51 if [ -z "$BG$FG$bd$bg$cu$fg$mo$pc" ]; then
52   echo >&2 "Usage: ucolour option <colour> [option <colour> ...]"
53   echo >&2 "Options: -b   Set background."
54   echo >&2 "         -B   Set highlight background."
55   echo >&2 "         -c   Set cursor."
56   echo >&2 "         -d   Set border."
57   echo >&2 "         -f   Set foreground."
58   echo >&2 "         -F   Set highlight foreground."
59   echo >&2 "         -m   Set mouse."
60   echo >&2 "         -P   Palette index."
61   echo >&2 "         -p   Set palette reference with -P."
62   echo >&2 "Notes: Border defaults to background."
63   echo >&2 "       The -P and -p flags must be used together."
64   exit 1
65 fi
66
67 [ -n "$TMUX" ] && tmux_prefix="\033Ptmux;\033"
68 [ -n "$BG" ] && echo -en "$tmux_prefix\033]17;$BG\007"
69 [ -n "$FG" ] && echo -en "$tmux_prefix\033]19;$FG\007"
70 [ -n "$bg" ] && echo -en "$tmux_prefix\033]11;$bg\007"
71 [ -n "$bd" ] && echo -en "$tmux_prefix\033]708;$bd\007"
72 [ -n "$cu" ] && echo -en "$tmux_prefix\033]12;$cu\007"
73 [ -n "$fg" ] && echo -en "$tmux_prefix\033]10;$fg\007"
74 [ -n "$mo" ] && echo -en "$tmux_prefix\033]13;$mo\007"
75 [ -n "$pc" ] && echo -en "$tmux_prefix\033]4;$pc;$ps\007"
76 exit 0