term-switch-theme.sh (2046B)
1 #!/usr/bin/env bash
2
3 # exit on error
4 set -eo pipefail
5
6 # figure out which shell we are running
7 shell=$(echo $SHELL | sed -E -e 's/^.*\///')
8
9 if [[ ! -f /tmp/theme-switch-term-status ]]; then
10 touch /tmp/theme-switch-term-status
11 fi
12
13 if [ -z $1 ]; then
14 current=$(cat /tmp/theme-switch-term-status)
15 case $current in
16 "dark")
17 target="light"
18 ;;
19 "light")
20 target="dark"
21 ;;
22 esac
23 else
24 target="$1"
25 fi
26
27 if [[ $(cat /tmp/theme-switch-term-status) == "$target" ]]; then
28 echo "[-] Did not switch theme: $1"
29 exit 0
30 fi
31
32 # define theme color based on given input
33 if [[ "$target" == "dark" ]]; then
34 fg="#ffffff"
35 bg="#111111"
36 col0="#073642" # black
37 col1="#dc322f" # red
38 col2="#859900" # green
39 col3="#b58900" # yellow
40 col4="#268bd2" # blue
41 col5="#d33682" # magenta
42 col6="#2aa198" # cyan
43 col7="#eee8d5" # white
44 elif [[ "$target" == "light" ]]; then
45 fg="#000000"
46 bg="#f7f7f7"
47 col0="#08404f" # bright black
48 col1="#e35f5c" # bright red
49 col2="#9fb700" # bright green
50 col3="#d9a400" # bright yellow
51 col4="#4ba1de" # bright blue
52 col5="#dc619d" # bright magenta
53 col6="#32c1b6" # bright cyan
54 col7="#ffffff" # bright white
55 else
56 exit 1;
57 fi
58
59 # apply theme to all running shell sessions
60 for i in $(pgrep $shell); do
61 # foreground
62 printf "\e]10;${fg}\007" >> /proc/$i/fd/0
63 # background
64 printf "\e]11;${bg}\007" >> /proc/$i/fd/0
65 # black
66 printf "\e]0;${col0}\007" >> /proc/$i/fd/0
67 # red
68 printf "\e]1;${col1}\007" >> /proc/$i/fd/0
69 # green
70 printf "\e]2;${col2}\007" >> /proc/$i/fd/0
71 # yellow
72 printf "\e]3;${col3}\007" >> /proc/$i/fd/0
73 # blue
74 printf "\e]4;${col4}\007" >> /proc/$i/fd/0
75 # magenta
76 printf "\e]5;${col5}\007" >> /proc/$i/fd/0
77 # cyan
78 printf "\e]6;${col6}\007" >> /proc/$i/fd/0
79 # white
80 printf "\e]7;${col7}\007" >> /proc/$i/fd/0
81 done
82
83 # user output
84 if [[ $(cat /tmp/theme-switch-term-status) != "$target" ]]; then
85 echo "$target" | tee /tmp/theme-switch-term-status > /dev/null
86 echo "[✓] Switched $shell to $target!"
87 fi