-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsh-setup-x-theme
executable file
·259 lines (235 loc) · 7.88 KB
/
sh-setup-x-theme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/sh
#
# Change X/xrdb theme between light/dark (solarized).
#
# This script's output is meant to be eval'd.
# It is used from my Zsh theme and in ~/.xsessionrc.
# See the theme_variant function for a wrapper arount it.
#
# $1: theme to use: auto/light/dark.
# $2: "save" to save the value to the config file.
#
# MY_X_THEME_VARIANT: set to the effective variant: "light" or "dark".
#
# TODO: query urxvt's current bg and use it for dircolors.
# see ~/bin/query-urxvt and ~/bin/xterm-bg.
config_file=~/.config/x-theme-variant
# set -x
# date >>/tmp/debug-sh-setup-x-theme.log
# exec 2>>/tmp/debug-sh-setup-x-theme.log
debug() {
if [ -n "$DEBUG" ]; then
echo "$(date +%FT%T) [sh-setup-x-theme] $*" >&2
fi
# echo "$(date +%FT%T) [sh-setup-x-theme:$$:$(ps -o cmd= $$)] $@" >> /tmp/debug-sh-setup-x-theme.log
}
get_variant_from_config() {
if [ -n "$TMUX" ]; then
tmux_variant="$(tmux show-env MY_X_THEME_VARIANT 2>/dev/null | cut -d= -f 2)"
config_variant="$tmux_variant"
debug "variant from tmux: $config_variant"
fi
if [ -z "$config_variant" ]; then
if [ -f $config_file ]; then
config_variant=$(cat $config_file)
debug "variant from cfg: $config_variant"
else
debug "Config file ($config_file) does not exist."
fi
if [ -z "$config_variant" ]; then
config_variant=auto
debug "variant from fallback: $config_variant"
fi
fi
}
# Init once and export the value.
# This gets used in Vim to auto-set the background, too.
# Handle options, in this case it does not / should not get sourced.
while [ "${1#-*}" != "$1" ]; do
case "$1" in
-d ) DEBUG=1 ;;
-f ) FORCE=1 ;;
-s ) SHELL_ONLY=1 ;;
-q ) # Query/info.
echo "Current theme variant: MY_X_THEME_VARIANT=$MY_X_THEME_VARIANT."
echo "BASE16_THEME: $BASE16_THEME."
get_variant_from_config
if [ -n "$tmux_variant" ]; then
echo "Theme from tmux: $config_variant."
else
echo "Theme from config: $config_variant."
fi
echo "Use 'auto', 'dark' or 'light' to change it."
exit
;;
*) echo "Unknown option: $1"; exit 1; ;;
esac
shift
done
debug "RUN: $0 $*"
given_variant=$1
debug "Requested variant: $given_variant"
# No argument provided: read config/env.
if [ -z "$given_variant" ]; then
get_variant_from_config
given_variant="$config_variant"
SAVE_VARIANT=0
else
FORCE=1
if [ "$SHELL_ONLY" != 1 ]; then
SAVE_VARIANT=1
fi
fi
variant="$given_variant"
debug "given_variant: $given_variant"
# Refresh terminal stuff (base16, dircolors) in a new urxvt instance.
TTY="$(tty)"
if [ "$FORCE" = 1 ] || [ "$_MY_X_THEME_VARIANT_TTY" != "$TTY" ]; then
REFRESH_TERM=1
fi
echo "export _MY_X_THEME_VARIANT_TTY=\$TTY"
# Get auto-theme from get-daytime-period for "auto",
# and validate value.
case $variant in
auto)
dt_period="$(~/.dotfiles/usr/bin/get-daytime-period)"
if [ "$dt_period" = 'Daytime' ]; then
variant=light
elif [ -n "$dt_period" ]; then
variant=dark
else
echo "get-daytime-period failed. Using 'dark' variant." >&2
variant=dark
fi
debug "=> variant: $variant"
;;
light|dark ) ;;
* ) echo "Invalid variant: $variant" >&2; exit 64
esac
# curbg="$(xrdb -query|sed -n -e '/^\*background:/ {p;q}' | tr -d '[:space:]' | cut -f2 -d:)"
# if [[ $curbg == '#fdf6e3' ]]; then
# if [[ $variant == "dark" ]]; then
# # xrdb -DSOLARIZED_DARK ~/.Xresources
# xrdb -merge ~/.dotfiles/lib/solarized-xresources/Xresources.dark
# changed_xrdb=1
# fi
# elif [[ $variant == "light" ]]; then
# # xrdb -DSOLARIZED_LIGHT ~/.Xresources
# xrdb -merge ~/.dotfiles/lib/solarized-xresources/Xresources.light
# changed_xrdb=1
# fi
if [ -n "$DISPLAY" ] && [ "$USER" != "root" ] && [ "$SHELL_ONLY" != 1 ] \
&& [ -z "$TMUX" ] && hash xrdb 2>/dev/null; then
# Query "mythemevariant" resource, used to hold the current state.
# Unset by default in ~/.Xresources.
cur_xrdb_theme="$(xrdb -query|sed -n -E -e '/mythemevariant:\t/ {s/.*:\t//p;q}')"
debug "cur_xrdb_theme=$cur_xrdb_theme"
if [ "$FORCE" = 1 ] || [ "$cur_xrdb_theme" != "$variant" ]; then
if [ "$variant" = dark ]; then
xrdb -DSOLARIZED_DARK -I"$HOME" -merge ~/.Xresources
elif [ "$variant" = light ]; then
xrdb -DSOLARIZED_LIGHT -I"$HOME" -merge ~/.Xresources
fi
echo "mythemevariant: $variant" | xrdb -merge
echo "Changed X/xrdb theme variant to $variant." >&2
fi
fi
DIRCOLORS_FILE=~/.dotfiles/lib/LS_COLORS/LS_COLORS
# Setup dircolors once.
if [ -n "$DIRCOLORS_FILE" ] \
&& ([ "$REFRESH_TERM" = 1 ] || [ "$DIRCOLORS_SETUP" != "$DIRCOLORS_FILE" ]); then
debug "Setting up DIRCOLORS_FILE=$DIRCOLORS_FILE"
debug "DIRCOLORS_SETUP=$DIRCOLORS_SETUP"
if [ -n "$DIRCOLORS_SETUP" ]; then
debug "Adjusting dircolors for variant=$variant."
else
debug "TODO: dircolors only during .xresources ($(basename "$0"))."
fi
# debug "running: dircolors -b $DIRCOLORS_FILE"
echo "$(dircolors -b $DIRCOLORS_FILE);"
echo "export DIRCOLORS_SETUP=$DIRCOLORS_FILE"
fi
# Used in ~/.vimrc.
echo "export MY_X_THEME_VARIANT=$variant;"
# Update tmux env, after sending escape codes eventually, which the
# "tmux display" might prevent to work correctly (?!). (might be obsolete)
# This also inits it initially.
if [ -n "$TMUX" ]; then # && [ "$SHELL_ONLY" != 1 ]; then
COLORS="$(tput colors)"
if [ "$COLORS" -lt 16 ]; then
debug "SKIP tmux: not enough colors ($COLORS are available (via 'tput colors'); TERM=$TERM)."
else
debug "tmux_variant: $tmux_variant"
tmux_tmp_conf="$(mktemp)"
if [ -z "$tmux_variant" ]; then
echo "echo 'theme-variant: setting tmux env: $variant.'"
tmux_session="$(tmux display-message -p '#S')"
tmux_windows=
for w in $(tmux list-windows -t "$tmux_session" -F '#{window_id}'); do
sed -n "s/ -g//; /^set /p; /^if/p; s/^\s*setw /\0-t $w /p" "$HOME/.dotfiles/lib/tmux-colors-solarized/tmuxcolors-$variant.conf" >> "$tmux_tmp_conf"
done
else
# echo "echo 'theme-variant: styling new tmux window ($variant).'"
tmux_windows=""
sed -n "s/ -g//; /^set /p; /^if/p; s/^\s*setw /\0$tmux_windows/p" "$HOME/.dotfiles/lib/tmux-colors-solarized/tmuxcolors-$variant.conf" > "$tmux_tmp_conf"
fi
echo "tmux set-env MY_X_THEME_VARIANT $variant"
echo "tmux source $tmux_tmp_conf"
echo "rm '$tmux_tmp_conf'"
fi
fi
# Setup base16-shell.
if [ "$REFRESH_TERM" = 1 ] || [ "${BASE16_THEME##*.}" != "$variant" ]; then
if [ "${TERM%%-*}" = 'linux' ]; then
echo "Skipping base16-shell with TERM=linux." >&2
else
echo "base16_theme solarized.$variant"
if [ -n "$TMUX" ]; then
# shellcheck disable=SC2016
echo 'tmux set-environment BASE16_THEME "$BASE16_THEME"'
fi
fi
fi
if [ "$MY_X_THEME_VARIANT" != "$variant" ] || [ "$FORCE" = 1 ]; then
if [ "${COLORTERM#rxvt}" != "$COLORTERM" ]; then
if [ "$variant" = light ]; then
escape_codes='\033]11;#fdf6e3\007 \033]10;#657b83\007 \033]12;#586e75\007 \033]13;#586e75\007 \033]708;#fdf6e3\007 \033]709;#fdf6e3\007'
else
escape_codes='\033]11;#002b36\007 \033]10;#839496\007 \033]12;#93a1a1\007 \033]13;#93a1a1\007 \033]708;#002b36\007 \033]709;#657b83\007'
fi
cat <<EOF
_printf_for_tmux() {
local i="\$1"
if [ -n "\$TMUX" ]; then
i="\\ePtmux;\\e\$i\\e\\\\"
fi
printf '%b' "\$i"
}
EOF
for i in $escape_codes; do
echo "_printf_for_tmux '$i'"
done
fi
fi
if [ "$SAVE_VARIANT" = 1 ]; then
# Save given value.
case $variant in
auto)
if [ -n "$TMUX" ]; then
tmux set-env -u MY_X_THEME_VARIANT
elif [ -f "$config_file" ]; then
rm "$config_file"
fi
;;
light|dark)
if [ -n "$TMUX" ]; then
tmux set-env MY_X_THEME_VARIANT "$variant"
echo "Saved variant=$1 in tmux." >&2
else
echo "$1" > "$config_file"
echo "Saved variant=$1." >&2
fi
;;
* ) echo "Invalid variant: $variant" >&2; exit 64
esac
fi