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