Repository navigation
Expand file tree
/
Copy pathwal
More file actions
362 lines (275 loc) · 9.59 KB
/
Copy pathwal
File metadata and controls
362 lines (275 loc) · 9.59 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/env bash
#
# wal - Generate and change colorschemes on the fly.
#
# Created by Dylan Araps
# Speed up script by not using unicode.
export LC_ALL=C
export LANG=C
shopt -s nullglob nocasematch
# Internal variables.
cache_dir="${HOME}/.cache/wal"
newline=$'\n'
# GENERATE COLORSCHEME
rand_img() {
# Make glob fails silent.
shopt -s nullglob
# Create an array of images and exclude the current wallpaper.
files=("${wal%/}"/*.{png,jpg,jpeg,jpe})
files=("${files[@]/"$old_wall"}")
# Reset glob.
shopt -u nullglob
# Pick a random image.
wal="${files[RANDOM % ${#files[@]}]}"
# Set the image to the first in the directory if the shuffle failes.
[[ ! -f "$wal" ]] && wal="${files[0]}"
}
get_colors() {
# Create the cache dir.
mkdir -p "${cache_dir}/schemes"
# Get the current wallpaper.
old_wall="$(< "${cache_dir}/wal")"
# Shuffle the image.
[[ -d "$wal" ]] && rand_img
# If the wallpaper doesn't exist, use the current one.
[[ ! -f "$wal" ]] && wal="$old_wall"
# Store cached colorscheme as 'dir-to-img.jpg'
cache_file="${cache_dir}/schemes/${wal//\//-}"
# Cache the wallpaper name
printf "%s\n" "$wal" > "${cache_dir}/wal"
# Generate 16 colors from the image and save them to a file.
if [[ -f "$cache_file" ]]; then
colors=($(< "$cache_file"))
else
colors=($(convert "${wal}" +dither -colors 16 -unique-colors txt:- | grep -E -o " \#.{6}"))
# Cache the scheme.
printf "%s\n" "${colors[@]}" > "$cache_file"
fi
# If imagemagick finds less than 16 colors, abort and warn the user.
(( "${#colors[@]}" <= 15 )) && {
out "Error: Imagemagick failed to generate a 16 color palette for ${wal##*/}"
out "Error: Imagemagick could only generate a ${#colors[@]} color palette."
out "Error: wal won't do guesswork to fill in the blanks."
out "Error: Please try a different image."
exit 1
}
out "colors: Generated colorscheme"
}
# SET COLORSCHEME
set_color() {
sequences+="\033]4;${1};${2}\007"
x_colors+="*color${1}: ${2}${newline}"
sh_colors+="color${1}='${2}'${newline}"
scss_colors+="\$color${1}: ${2};${newline}"
}
set_special() {
sequences+="\033]${1};${2}\007"
# Set X colors
case "$1" in
10) x_colors+="*foreground: ${2}${newline}" ;;
11) x_colors+="*background: ${2}${newline}" ;;
12) x_colors+="*cursor: ${2}${newline}" ;;
esac
}
send_sequences() {
# Create string of escape sequences to send to the terminals.
set_special 10 "${colors[15]}"
set_special 11 "${colors[0]}"
set_special 12 "${colors[15]}"
set_special 13 "${colors[15]}"
set_special 14 "${colors[0]}"
# This escape sequence doesn't work in VTE terminals.
[[ "$vte" != "on" ]] && set_special 708 "${colors[0]}"
set_color 0 "${colors[0]}"
set_color 1 "${colors[9]}"
set_color 2 "${colors[10]}"
set_color 3 "${colors[11]}"
set_color 4 "${colors[12]}"
set_color 5 "${colors[13]}"
set_color 6 "${colors[14]}"
set_color 7 "${colors[15]}"
set_color 9 "${colors[9]}"
set_color 10 "${colors[10]}"
set_color 11 "${colors[11]}"
set_color 12 "${colors[12]}"
set_color 13 "${colors[13]}"
set_color 14 "${colors[14]}"
set_color 15 "${colors[15]}"
# Create a comment color based on the brightness of the background.
case "${colors[0]:1:1}" in
[0-1]) set_color 8 "#666666" ;;
2) set_color 8 "#757575" ;;
[3-4]) set_color 8 "#999999" ;;
5) set_color 8 "#8a8a8a" ;;
[6-9]) set_color 8 "#a1a1a1" ;;
*) set_color 8 "${colors[7]}" ;;
esac
# Directing output to /dev/pts/* allows you to send output to all open terminals
# on your system.
for term in /dev/pts/[0-9]*; do
printf "%b" "$sequences" > "$term" &
done
out "colors: Set terminal colors"
}
set_wallpaper() {
# Get desktop environment.
de="${XDG_CURRENT_DESKTOP}"
# Fallback to using xprop.
[[ -z "$de" ]] && de="$(xprop -root | awk '/KDE_SESSION_VERSION|^_MUFFIN|xfce4|xfce5/')"
case "$de" in
*"MUFFIN"* | *"Cinnamon"*) gsettings set org.cinnamon.desktop.background picture-uri "file://${wal}" ;;
*"MATE"*) gsettings set org.mate.background picture-filename "$wal" ;;
*"GNOME"*) gsettings set org.gnome.desktop.background picture-uri "file://${wal}" ;;
*"XFCE"*)
xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/image-path --set "$wal" 2>/dev/null ||\
xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/workspace0/last-image --set "$wal" 2>/dev/null
;;
*)
if type -p feh >/dev/null; then
feh --bg-fill "$wal"
elif type -p nitrogen >/dev/null; then
nitrogen --set-zoom-fill "$wal"
elif type -p bgs >/dev/null; then
bgs "$wal"
elif type -p hsetroot >/dev/null; then
hsetroot -fill "$wal"
elif type -p habak >/dev/null; then
habak -mS "$wal"
else
gsettings set org.gnome.desktop.background picture-uri "file://${wal}"
fi
;;
esac
out "wallpaper: Set new wallpaper"
}
# EXPORT COLORS
export_sequences() {
printf "%s" "$sequences"
out "export: Exported escape sequences"
}
export_envar() {
printf "%s\n%s" "# wal - Colors generated by wal " "$sh_colors"
out "export: Exported sh colors"
}
export_scss() {
printf "%s\n%s" "// wal - Colors generated by wal " "$scss_colors"
out "export: Exported scss color variables"
}
export_rofi() {
x_colors+="rofi.color-window: ${colors[0]}, ${colors[0]}, ${colors[10]}${newline}"
x_colors+="rofi.color-normal: ${colors[0]}, ${colors[15]}, ${colors[0]}, ${colors[10]}, ${colors[15]}${newline}"
x_colors+="rofi.color-active: ${colors[0]}, ${colors[15]}, ${colors[0]}, ${colors[10]}, ${colors[15]}${newline}"
x_colors+="rofi.color-urgent: ${colors[0]}, ${colors[15]}, ${colors[0]}, ${colors[9]}, ${colors[15]}${newline}"
}
export_colors() {
export_sequences > "${cache_dir}/colors"
export_envar > "${cache_dir}/colors.sh"
export_scss > "${cache_dir}/colors.scss"
export_rofi
}
# RELOAD COLORSCHEME
reload_colors() {
# Source the current sequences
sequences="$(< "${HOME}/.cache/wal/colors")"
# If vte mode was used, remove the problem sequence.
[[ "$vte" == "on" ]] && sequences="${sequences/??\]708\;\#??????}"
printf "%b" "$sequences"
exit
}
reload_env() {
# Reload i3 if running.
pgrep i3 && i3-msg reload &
}
reload_xrdb() {
# Merge the colors into the X db so new terminals use them.
xrdb -merge >/dev/null 2>&1 <<< "$x_colors" && \
out "colors: Merged colors with X env"
}
# OTHER
get_full_path() {
# Go to the relative PATH.
if ! cd "${1%/*}"; then
printf "%s\n" "Error: Directory '$1' doesn't exist or is inaccessible" >&2
printf "%s\n" " Check that the directory exists or try another directory." >&2
exit 1
fi
# Final directory.
img_dir="$(pwd -P)"
img="${1/*\/}"
if [[ -e "${img_dir}/${img}" ]]; then
printf "%s\n" "${img_dir}/${1/*\/}"
else
printf "%s\n" "Error: File: '${img_dir}/${img}' not found." >&2
printf "%s\n" " Check that the file exists or try another file." >&2
exit 1
fi
}
out() {
[[ "$quiet" != "on" ]] && printf "%s\n" "$1" >&2
}
usage() { printf "%s" "\
Usage: wal [OPTION] -i '/path/to/dir'
Example: wal -i '${HOME}/Pictures/Wallpapers/'
wal -i '${HOME}/Pictures/1.jpg'
Flags:
-c Delete all cached colorschemes.
-h Display this help page.
-i '/path/to/dir' Which image to use.
'/path/to/img.jpg'
-o 'script_name' External script to run after 'wal'.
-q Quiet mode, don't print anything.
-r Reload current colorscheme.
-t Fix artifacts in VTE Terminals. (Termite, xfce4-terminal)
"
}
get_args() {
while getopts ":chi:o:qrt" opt; do
case "$opt" in
"c") rm -rf "${cache_dir}/schemes"; exit ;;
"h") usage; exit 1 ;;
"i")
[[ -f "${PWD}/${OPTARG/*\/}" ]] && wal="${PWD}/${OPTARG/*\/}"
[[ -z "$wal" ]] && wal="$(get_full_path "$OPTARG")"
;;
"o") external_script=("$OPTARG") ;;
"q") quiet="on" ;;
"r") reload="on" ;;
"t") vte="on" ;;
"?")
printf "%s\n" "Invalid option: -$OPTARG" >&2
exit 1
;;
":")
printf "%s\n" "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Reload colors.
[[ "$reload" == "on" ]] && reload_colors
# Check if -i was used.
if [[ -z "$wal" ]]; then
printf "%s\n" "Error: 'wal' must be run with '-i'" >&2
printf "%s\n" "Try 'wal -h' for more information." >&2
exit 1
fi
}
# FINISH UP
main () {
get_args "$@"
# Get 16 unique colors from the image.
get_colors
# Send the color changing sequences to every open terminal emulator.
send_sequences
# Set the wallpaper.
set_wallpaper
# Export the colors in other formats.
export_colors
# Merge colors into X env.
reload_xrdb
# Reload env
reload_env >/dev/null 2>&1
# Execute custom script.
type -p "${external_script[0]}" >/dev/null 2>&1 && "${external_script[@]}"
}
main "$@"