helix-switch-theme.sh (1583B)
1 #!/usr/bin/env bash
2
3 # exit on error
4 set -eo pipefail
5
6 # configuration
7 config_dir="$HOME/.config/helix"
8 config="$config_dir/config.toml"
9 config_theme="$config_dir/autothemes.toml"
10
11 # gather dark and light themes from config file
12 if [ -f "$config_theme" ]; then
13 dark_theme=$(
14 grep -e "^\s*theme_dark" "$config_theme" | \
15 sed -E -e 's/^\s*theme_dark\s*=\s*\"([^"]+)\".*$/\1/'
16 )
17 light_theme=$(
18 grep -e "^\s*theme_light" "$config_theme" | \
19 sed -E -e 's/^\s*theme_light\s*=\s*\"([^"]+)\".*$/\1/'
20 )
21 else
22 echo "
23 Config file $config_theme does not exist. Please create the file, e.g.:
24
25 theme_dark = \"adwaita-dark\"
26 theme_light = \"adwaita-light\"
27 "
28 exit 1
29 fi
30
31 # functions
32 function get_theme {
33 set +e
34 grep -c -e "^\s*theme\s*=\s*\"${1}\"" ${config}
35 set -e
36 }
37 function set_theme {
38 sed -E -e "s/^\s*(theme\s*=\s*)\"[^\"]+\"/\1\"${1}\"/" -i ${config}
39 }
40
41 # check whether helix currently uses the dark theme
42 is_dark=$(get_theme "${dark_theme}")
43 is_light=$(get_theme "${light_theme}")
44
45 # check whether input was given
46 if [ -z $1 ]; then
47 if [[ $is_dark != 0 ]]; then
48 target="light"
49 else
50 target="dark"
51 fi
52 else
53 target="$1"
54 fi
55
56
57 if [ "$target" == "dark" ]; then
58 if [[ $is_dark != 1 ]]; then
59 set_theme "${dark_theme}"
60 echo "[✓] Switched helix to dark!"
61 pkill -USR1 hx
62 else
63 echo "[-] Did not switch helix theme."
64 fi
65 else
66 if [[ $is_light != 1 ]]; then
67 set_theme "${light_theme}"
68 echo "[✓] Switched helix to light!"
69 pkill -USR1 hx
70 else
71 echo "[-] Did not switch helix theme."
72 fi
73 fi