A collection of scripts
1#!/bin/sh
2# shellcheck disable=SC1090,SC2154
3
4# Usage statement
5usage() {
6 printf '%s\n' "usage: ${0##*/} theme"
7}
8
9# Convert hex colors into rgb
10hex2rgb() {
11 hex=$1
12 hex_r=${hex%????}
13 hex_g=${hex#??}; hex_g=${hex_g%??}
14 hex_b=${hex#????}
15 printf '%d,%d,%d' "0x$hex_r" "0x$hex_g" "0x$hex_b"
16}
17
18# Set directory variables
19[ "$THM_CONFIG_DIR" ] \
20 && conf_dir="$THM_CONFIG_DIR" \
21 || conf_dir="${XDG_CONFIG_HOME:-$HOME/.config}/thm"
22[ "$THM_DEST_DIR" ] \
23 && dest_dir="$THM_DEST_DIR" \
24 || dest_dir="${XDG_CACHE_HOME:-$HOME/.cache}/thm"
25
26err() {
27 [ "$*" ] && err_msg="$*" || err_msg="error"
28 printf '%s%b' "${0##*/}: " "$err_msg\n" 1>&2
29 exit 1
30}
31
32# Ensure the theme file exist and source it
33case $1 in
34 -h|h|--help|help ) usage; exit 0 ;;
35 * ) [ "$1" ] || err "missing argument"
36 [ -f "$conf_dir/themes/$1.theme" ] || err "$1: theme not found"
37 theme_file="$conf_dir/themes/$1.theme"
38 . "$theme_file" ;;
39esac
40
41# Create RGB colors from the hex colors
42bg_color_rgb="$(hex2rgb "$bg_color")"
43fg_color_rgb="$(hex2rgb "$fg_color")"
44color0_rgb="$(hex2rgb "$color0")"
45color1_rgb="$(hex2rgb "$color1")"
46color2_rgb="$(hex2rgb "$color2")"
47color3_rgb="$(hex2rgb "$color3")"
48color4_rgb="$(hex2rgb "$color4")"
49color5_rgb="$(hex2rgb "$color5")"
50color6_rgb="$(hex2rgb "$color6")"
51color7_rgb="$(hex2rgb "$color7")"
52color8_rgb="$(hex2rgb "$color8")"
53color9_rgb="$(hex2rgb "$color9")"
54color10_rgb="$(hex2rgb "$color10")"
55color11_rgb="$(hex2rgb "$color11")"
56color12_rgb="$(hex2rgb "$color12")"
57color13_rgb="$(hex2rgb "$color13")"
58color14_rgb="$(hex2rgb "$color14")"
59color15_rgb="$(hex2rgb "$color15")"
60
61# Make sure the dest_dir and dest_dir/thm_old exist
62[ -d "$dest_dir/thm_old" ] || { mkdir -p "$dest_dir/thm_old" || err "failed to create $dest_dir/thm_old"; }
63
64# Try and empty the dest_dir before populating it
65mv "$dest_dir/"* "$dest_dir/thm_old" 2> /dev/null
66
67# Make a file with the name of the new theme
68printf '%s\n' "$theme_file" > "$dest_dir/current_thm"
69
70# Repalce template syntax with the actual colors
71for t in "${conf_dir}/templates/"*".template"
72do
73 file_name=${t##*/}
74 file_name=${file_name%.template}
75
76 sed "
77 s/{bg_color}/$bg_color/g
78 s/{fg_color}/$fg_color/g
79 s/{color0}/$color0/g
80 s/{color1}/$color1/g
81 s/{color2}/$color2/g
82 s/{color3}/$color3/g
83 s/{color4}/$color4/g
84 s/{color5}/$color5/g
85 s/{color6}/$color6/g
86 s/{color7}/$color7/g
87 s/{color8}/$color8/g
88 s/{color9}/$color9/g
89 s/{color10}/$color10/g
90 s/{color11}/$color11/g
91 s/{color12}/$color12/g
92 s/{color13}/$color13/g
93 s/{color14}/$color14/g
94 s/{color15}/$color15/g
95 s/{bg_color.rgb}/$bg_color_rgb/g
96 s/{fg_color.rgb}/$fg_color_rgb/g
97 s/{color0.rgb}/$color0_rgb/g
98 s/{color1.rgb}/$color1_rgb/g
99 s/{color2.rgb}/$color2_rgb/g
100 s/{color3.rgb}/$color3_rgb/g
101 s/{color4.rgb}/$color4_rgb/g
102 s/{color5.rgb}/$color5_rgb/g
103 s/{color6.rgb}/$color6_rgb/g
104 s/{color7.rgb}/$color7_rgb/g
105 s/{color8.rgb}/$color8_rgb/g
106 s/{color9.rgb}/$color9_rgb/g
107 s/{color10.rgb}/$color10_rgb/g
108 s/{color11.rgb}/$color11_rgb/g
109 s/{color12.rgb}/$color12_rgb/g
110 s/{color13.rgb}/$color13_rgb/g
111 s/{color14.rgb}/$color14_rgb/g
112 s/{color15.rgb}/$color15_rgb/g
113 " "$t" > "${dest_dir}/${file_name}"
114done
115
116# This allows for different configuration for different color schemes.
117# Configuration for one theme may not work very well for another theme. To use
118# this set theme_type in the theme file to any string and place theme specific
119# configuration in `${XDG_CONFIG_HOME:-$HOME/.config}/thm/templates/$theme_type`.
120# If not specified, only templates in the base template directory will be generated.
121# This will overwrite previously generated template files.
122[ "$theme_type" ] && [ -d "${conf_dir}/templates/$theme_type" ] && \
123 for t in "${conf_dir}/templates/$theme_type/"*".template"
124 do
125 file_name=${t##*/}
126 file_name=${file_name%.template}
127
128 sed "
129 s/{bg_color}/$bg_color/g
130 s/{fg_color}/$fg_color/g
131 s/{color0}/$color0/g
132 s/{color1}/$color1/g
133 s/{color2}/$color2/g
134 s/{color3}/$color3/g
135 s/{color4}/$color4/g
136 s/{color5}/$color5/g
137 s/{color6}/$color6/g
138 s/{color7}/$color7/g
139 s/{color8}/$color8/g
140 s/{color9}/$color9/g
141 s/{color10}/$color10/g
142 s/{color11}/$color11/g
143 s/{color12}/$color12/g
144 s/{color13}/$color13/g
145 s/{color14}/$color14/g
146 s/{color15}/$color15/g
147 s/{bg_color.rgb}/$bg_color_rgb/g
148 s/{fg_color.rgb}/$fg_color_rgb/g
149 s/{color0.rgb}/$color0_rgb/g
150 s/{color1.rgb}/$color1_rgb/g
151 s/{color2.rgb}/$color2_rgb/g
152 s/{color3.rgb}/$color3_rgb/g
153 s/{color4.rgb}/$color4_rgb/g
154 s/{color5.rgb}/$color5_rgb/g
155 s/{color6.rgb}/$color6_rgb/g
156 s/{color7.rgb}/$color7_rgb/g
157 s/{color8.rgb}/$color8_rgb/g
158 s/{color9.rgb}/$color9_rgb/g
159 s/{color10.rgb}/$color10_rgb/g
160 s/{color11.rgb}/$color11_rgb/g
161 s/{color12.rgb}/$color12_rgb/g
162 s/{color13.rgb}/$color13_rgb/g
163 s/{color14.rgb}/$color14_rgb/g
164 s/{color15.rgb}/$color15_rgb/g
165 " "$t" > "${dest_dir}/${file_name}"
166 done
167
168# Run extra user scripts
169[ -d "$conf_dir/scripts" ] || exit 0
170for i in "$conf_dir/scripts/"*
171do
172 [ -x "$i" ] && $i > /dev/null 2>&1 &
173done