sisyphus/scripts/toggle-light-dark.sh

146 lines
4.3 KiB
Bash
Raw Normal View History

2023-10-28 12:49:49 +02:00
#!/usr/bin/env bash
# Toggle light- or dark-mode for your applications
2024-06-19 12:50:49 +02:00
# Usage: toggle [-m light|dark] [-g]
2023-10-28 12:49:49 +02:00
#################
### Variables ###
#################
THEME_LIGHT='tdpeuter-light'
THEME_DARK='tdpeuter-dark'
THEME_DEFAULT="${THEME_LIGHT}"
2024-06-19 12:50:49 +02:00
STATE_DIR="${HOME}/.local/state/sisyphus"
STATE_FILE="${STATE_DIR}/theme"
BG_DIR="${HOME}/Nextcloud/Afbeeldingen/wallpapers"
2023-10-28 12:49:49 +02:00
declare -A theme_next
theme_next[${THEME_LIGHT}]="${THEME_DARK}"
theme_next[${THEME_DARK}]="${THEME_LIGHT}"
declare -A gsettings_alt
gsettings_alt[${THEME_LIGHT}]='default'
gsettings_alt[${THEME_DARK}]='prefer-dark'
2024-06-19 12:50:49 +02:00
declare -A gtk_theme
gtk_theme[${THEME_LIGHT}]='Adwaita'
gtk_theme[${THEME_DARK}]='Adwaita-dark'
declare -A wallpaper
2024-06-19 12:50:49 +02:00
wallpaper[${THEME_LIGHT}]="bg-light"
wallpaper[${THEME_DARK}]="bg-dark"
2024-11-10 20:18:46 +01:00
declare -A icon
icon[${THEME_LIGHT}]="\uf185"
icon[${THEME_DARK}]="\uf186"
2023-10-28 12:49:49 +02:00
#############
### Logic ###
#############
# Parse options
2024-06-19 12:50:49 +02:00
while getopts ":m:g" option; do
2023-10-28 12:49:49 +02:00
case "${option}" in
m)
if [ "${OPTARG}" == 'light' ]; then
theme="${THEME_LIGHT}"
elif [ "${OPTARG}" == 'dark' ]; then
theme="${THEME_DARK}"
else
>&2 printf "Error: Invalid mode: '%s'.\nShould be either 'light' or 'dark'\n" "${option}"
exit 1
fi
;;
2024-06-19 12:50:49 +02:00
g)
2024-11-10 20:18:46 +01:00
current_state="$(cat "${STATE_FILE}")"
next_state="${theme_next[${current_state}]}"
if [ "${current_state}" == "${THEME_DARK}" ]; then
2024-06-19 12:50:49 +02:00
class="activated"
2024-11-10 20:18:46 +01:00
percentage=100
2024-06-19 12:50:49 +02:00
fi
2024-11-10 20:18:46 +01:00
printf '{"text": "%s", "alt": "%s", "tooltip": "Set theme to %s", "percentage": %d, "class": "%s"}' \
"${icon[${next_state}]}" "${gsettings_alt[${next_state}]}" "${next_state}" "${percentage:=0}" "${class:="none"}"
2024-06-19 12:50:49 +02:00
exit 0
;;
2023-10-28 12:49:49 +02:00
*)
>&2 printf "Error: Invalid option: '%s'.\n" "${option}"
exit 1
;;
esac
done
shift $(( OPTIND - 1 ))
# Check if the state file exists
if ! [ -d "$(dirname ${STATE_FILE})" ]; then
mkdir -p "$(dirname ${STATE_FILE})"
fi
# Choose next theme
previous_theme="$(cat ${STATE_FILE})"
if ! [[ -z "${previous_theme}" || "${theme}" ]]; then
theme="${theme_next[${previous_theme}]}"
fi
echo "${theme:=${THEME_DEFAULT}}" > "${STATE_FILE}"
######################
### Set all themes ###
######################
2024-06-19 12:50:49 +02:00
# Update terminal colors by sending it OSC sequences.
# Alternatively, you could use theme.sh (https://github.com/lemnos/theme.sh)
# Function below loosely based on theme.sh and https://codeberg.org/dnkl/foot/issues/708
function update_terminal_colors() {
for pid in $(pgrep zsh); do
if [ "${theme}" == "${THEME_LIGHT}" ]; then
printf "\033]10;#000000\007" >> /proc/${pid}/fd/0
printf "\033]11;#ffffff\007" >> /proc/${pid}/fd/0
elif [ "${theme}" == "${THEME_DARK}" ]; then
printf "\033]10;#ffffff\007" >> /proc/${pid}/fd/0
printf "\033]11;#000000\007" >> /proc/${pid}/fd/0
fi
done
}
# Foot
if [ "$(command -v foot)" ] ; then
# Make color theme switch 'permanent'.
echo "include=~/.config/foot/themes/${theme}.ini" > ~/.config/foot/theme.ini &
# We will have to change the terminal colors ourselves.
update_terminal_colors &
fi
# GNOME (GTK)
2023-10-28 12:49:49 +02:00
if [ "$(command -v gsettings)" ]; then
gsettings set org.gnome.desktop.interface color-scheme "${gsettings_alt[${theme}]}" &
2024-06-19 12:50:49 +02:00
gsettings set org.gnome.desktop.interface gtk-theme "${gtk_theme[${theme}]}" &
2023-10-28 12:49:49 +02:00
fi
# Kitty
if [ "$(command -v kitty)" ]; then
kitten themes --reload-in all --config-file-name theme.conf "${theme}" &
fi
# Sway
if [ "$(command -v swaybg)" ]; then
2024-06-19 12:50:49 +02:00
bg_path="${BG_DIR}/${wallpaper[${theme}]}"
/run/current-system/sw/bin/cp "${bg_path}" "${STATE_DIR}/bg" && swaymsg reload &
2024-11-10 20:18:46 +01:00
# pkill --signal 13 "waybar" &
2023-10-28 12:49:49 +02:00
fi
2023-11-02 17:05:09 +01:00
# Vifm
if [ "$(command -v vifm)" ]; then
echo "colorscheme ${theme} Default-256 Default" > ~/.config/vifm/theme.conf
# Update all running instances
vifm --remote -c "colorscheme ${theme}" &
2023-11-02 17:05:09 +01:00
fi
2023-10-28 12:49:49 +02:00
# Vim
if [ "$(command -v vim)" ]; then
echo "colorscheme ${theme}" > ~/.vim/theme.conf
# Update all running instances
for server in $(vim --serverlist); do
vim --servername "${server}" --remote-send "<C-\><C-N>:colorscheme ${theme}<CR>"
done
2023-10-28 12:49:49 +02:00
fi