night-theme-switcher/common/night-theme-switcher.in

287 lines
10 KiB
Text
Raw Normal View History

2025-12-05 21:17:23 +00:00
#!/bin/bash
# SPDX-FileCopyrightText: Christian Schendel <doppelhelix@gmail.com>
# SPDX-License-Identifier: 0BSD
set -eo pipefail
## NOTE A patched version of qt6ct is required.
2025-12-07 07:55:39 +00:00
## NOTE The benefits are:
2025-12-05 21:17:23 +00:00
## NOTE KDE color schemes support,KDE QML applications theming support and
## NOTE KDE's icon engine support
## NOTE See https://aur.archlinux.org/pkgbase/qt6ct-kde/
version='@VERSION@'
pkgname='night-theme-switcher'
default_config_file="/usr/share/$pkgname/nts.skel"
user_name=$(getent passwd "$USER")
homedir=$(echo "$user_name" | cut -d: -f6)
# allow user to override from cli thus using multiple files as needed
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$homedir/.config}"
user_config_file=${user_config_file:-$XDG_CONFIG_HOME/$pkgname/$pkgname.conf}
### Begin insert of Arch script
# Avoid any encoding problems
export LANG=C
# check if messages are to be printed using color
unset all_off bold blue green red yellow
if [[ -t 2 ]]; then
# prefer terminal safe colored and bold text when tput is supported
if tput setaf 0 &>/dev/null; then
all_off="$(tput sgr0)"
bold="$(tput bold)"
blue="${bold}$(tput setaf 4)"
green="${bold}$(tput setaf 2)"
red="${bold}$(tput setaf 1)"
yellow="${bold}$(tput setaf 3)"
else
all_off="\e[1;0m"
bold="\e[1;1m"
blue="${bold}\e[1;34m"
green="${bold}\e[1;32m"
red="${bold}\e[1;31m"
yellow="${bold}\e[1;33m"
fi
fi
readonly all_off bold blue green red yellow
### End insert of Arch script
# dependency checks probably not needed but they do not hurt
if ! command -v qt6ct >/dev/null 2>&1; then
notify-send \
'Missing dependency' \
'Please install qt6ct.' \
-h string:x-canonical-private-synchronous:sys-notify \
-u critical \
-i dialog-error
mesg="qt6ct-kde is required to use this script."
2025-12-05 21:17:23 +00:00
echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}"
exit 1
fi
if ! command -v gsettings >/dev/null 2>&1; then
notify-send \
'Missing dependency' \
'gsettings not found.\nPlease install glib2.' \
-h string:x-canonical-private-synchronous:sys-notify \
-u critical \
-i dialog-error
mesg="glib2 is required to use this script."
echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}"
exit 1
fi
check_config() {
if [[ ! -f $default_config_file ]]; then
local mesg="$default_config_file is missing. Reinstall this package to continue."
echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}"
exit 1
2025-12-05 21:17:23 +00:00
fi
if [[ ! -f "$user_config_file" ]]; then
echo -e "${bold}------------------------------------------------------------${all_off}"
echo -e "${bold} No config file found so creating a fresh one in:${all_off}"
echo -e "${bold}${blue} $XDG_CONFIG_HOME/$pkgname/$pkgname.conf${all_off}"
2025-12-05 21:17:23 +00:00
echo
echo -e "${bold} Edit this file before invoking $pkgname again.${all_off}"
echo -e "${bold}------------------------------------------------------------${all_off}"
install -Dm644 $default_config_file "$user_config_file"
2025-12-05 21:17:23 +00:00
exit 0
else
# shellcheck source=nts.skel
. "$user_config_file"
# parse config file for correctness
if [[ ! -f "$kcolorscheme_light" ]]; then
local mesg="Invalid kcolorscheme_light defined in\n ${blue}$user_config_file"
echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}"
exit 1
fi
if [[ ! -f "$kcolorscheme_dark" ]]; then
local mesg="Invalid kcolorscheme_dark defined in ${blue}$user_config_file"
echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}"
exit 1
fi
if [[ ! -d "/usr/share/icons/$icon_theme_light" ]]; then
local mesg="Invalid icon_theme_light defined in\n ${blue}$user_config_file"
echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}"
exit 1
fi
if [[ ! -d "/usr/share/icons/$icon_theme_dark" ]]; then
local mesg="Invalid icon_theme_dark defined in ${blue}$user_config_file"
echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}"
exit 1
fi
if [[ ! -d "/usr/share/themes/$gtk_theme_light" ]]; then
local mesg="Invalid gtk_theme_light defined in\n ${blue}$user_config_file"
echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}"
exit 1
fi
if [[ ! -d "/usr/share/themes/$gtk_theme_dark" ]]; then
local mesg="Invalid gtk_theme_dark defined in ${blue}$user_config_file"
echo -e "${red}==> ERROR:${all_off}${bold} ${mesg}${all_off}"
exit 1
fi
2025-12-05 21:17:23 +00:00
fi
}
_set_theme() {
## gtk-theme
gsettings set org.gnome.desktop.interface color-scheme "$gtk_color_scheme"
gsettings set org.gnome.desktop.interface gtk-theme "$gtk_theme"
gsettings set org.gnome.desktop.interface icon-theme "$icon_theme"
## GTK2
if [[ -w "$HOME/.gtkrc-2.0" ]]; then
sed -i "s/gtk-theme-name=.*/gtk-theme-name=$gtk_theme/g" \
"$HOME/.gtkrc-2.0"
sed -i "s/gtk-icon-theme-name=.*/gtk-icon-theme-name=$icon_theme/g" \
"$HOME/.gtkrc-2.0"
fi
## GTK3
if [[ -w "$HOME/.config/gtk-3.0/settings.ini" ]]; then
sed -i "s/gtk-application-prefer-dark-theme=.*/gtk-application-prefer-dark-theme=$gtk_prefer_dark_theme/g" \
"$HOME/.config/gtk-3.0/settings.ini"
sed -i "s/gtk-theme-name=.*/gtk-theme-name=$gtk_theme/g" \
"$HOME/.config/gtk-3.0/settings.ini"
sed -i "s/gtk-icon-theme-name=.*/gtk-icon-theme-name=$icon_theme/g" \
"$HOME/.config/gtk-3.0/settings.ini"
fi
## GTK4
if [[ -w "$HOME/.config/gtk-4.0/settings.ini" ]]; then
sed -i "s/gtk-application-prefer-dark-theme=.*/gtk-application-prefer-dark-theme=$gtk_prefer_dark_theme/g" \
"$HOME/.config/gtk-4.0/settings.ini"
sed -i "s/gtk-icon-theme-name=.*/gtk-icon-theme-name=$icon_theme/g" \
"$HOME/.config/gtk-4.0/settings.ini"
sed -i "s/gtk-theme-name=.*/gtk-theme-name=$gtk_theme/g" \
"$HOME/.config/gtk-4.0/settings.ini"
fi
## QT
if [[ -w "$HOME/.config/qt5ct/qt5ct.conf" ]]; then
sed -i "s/icon_theme=.*/icon_theme=$icon_theme/g" \
"$HOME/.config/qt5ct/qt5ct.conf"
sed -i "s,custom_palette=.*,custom_palette=$qt_color_scheme_path,g" \
"$HOME/.config/qt5ct/qt5ct.conf"
fi
if [[ -w "$HOME/.config/qt6ct/qt6ct.conf" ]]; then
sed -i "s/icon_theme=.*/icon_theme=$icon_theme/g" \
"$HOME/.config/qt6ct/qt6ct.conf"
sed -i "s/custom_palette=.*/custom_palette=$qt_custom_palette/g" \
"$HOME/.config/qt6ct/qt6ct.conf"
sed -i "s,color_scheme_path=.*,color_scheme_path=$qt_color_scheme_path,g" \
"$HOME/.config/qt6ct/qt6ct.conf"
fi
## workaround if plasma-workspace is installed
if [ -x "$(command -v /usr/lib/plasma-changeicon)" ]; then
/usr/lib/plasma-changeicons "$icon_theme"
fi
}
switch-dark-light() {
gtk_color_scheme=$(gsettings get org.gnome.desktop.interface color-scheme)
if [[ ! $gtk_color_scheme == ''\''prefer-dark'\''' ]]; then
gtk_color_scheme=prefer-dark
gtk_prefer_dark_theme=true
gtk_theme=adw-gtk3-dark
icon_theme=Papirus-Dark
qt_custom_palette=true
qt_color_scheme_path=/usr/share/color-schemes/BreezeDark.colors
_set_theme
else
gtk_color_scheme=default
gtk_prefer_dark_theme=false
gtk_theme=adw-gtk3
icon_theme=Papirus
qt_custom_palette=true
qt_color_scheme_path=/usr/share/color-schemes/BreezeLight.colors
_set_theme
fi
}
restore-theme-on-login() {
find "$HOME/.config//gtk-3.0/" ! -name 'bookmarks' -type f -exec rm -f {} +
find "$HOME/.config//gtk-4.0/" ! -name 'bookmarks' -type f -exec rm -f {} +
gtk_color_scheme=$(gsettings get org.gnome.desktop.interface color-scheme)
if [[ ! $gtk_color_scheme == ''\''prefer-dark'\''' ]]; then
gtk_color_scheme=default
gtk_prefer_dark_theme=false
gtk_theme=adw-gtk3
icon_theme=Papirus
qt_custom_palette=true
qt_color_scheme_path=/usr/share/color-schemes/BreezeLight.colors
_set_theme
else
gtk_color_scheme=prefer-dark
gtk_prefer_dark_theme=true
gtk_theme=adw-gtk3-dark
icon_theme=Papirus-Dark
qt_custom_palette=true
qt_color_scheme_path=/usr/share/color-schemes/BreezeDark.colors
_set_theme
fi
}
preview() {
echo -e "${bold}$pkgname v$version${all_off}"
echo
echo -e "${bold} KColorScheme"
if [[ -f "$kcolorscheme_light" ]]; then
echo -en "${bold} Light:"
echo -e "$(tput cr)$(tput cuf 10)${blue}$kcolorscheme_light${all_off}${bold}${all_off}"
fi
if [[ -f "$kcolorscheme_dark" ]]; then
echo -en "${bold} Dark:"
echo -e "$(tput cr)$(tput cuf 10)${blue}$kcolorscheme_dark${all_off}${bold}${all_off}"
fi
echo
echo -e "${bold} Icon Theme"
if [[ -d "/usr/share/icons/$icon_theme_light" ]]; then
echo -en "${bold} Light:"
echo -e "$(tput cr)$(tput cuf 10)${blue}/usr/share/icons/$icon_theme_light${all_off}${bold}${all_off}"
2025-12-05 21:17:23 +00:00
fi
if [[ -d "/usr/share/icons/$icon_theme_dark" ]]; then
echo -en "${bold} Dark:"
echo -e "$(tput cr)$(tput cuf 10)${blue}/usr/share/icons/$icon_theme_dark${all_off}${bold}${all_off}"
2025-12-05 21:17:23 +00:00
fi
echo
echo -e "${bold} GTK Theme"
if [[ -d "/usr/share/themes/$gtk_theme_light" ]]; then
echo -en "${bold} Light:"
echo -e "$(tput cr)$(tput cuf 10)${blue}/usr/share/themes/$gtk_theme_light${all_off}${bold}${all_off}"
2025-12-05 21:17:23 +00:00
fi
if [[ -d "/usr/share/themes/$gtk_theme_dark" ]]; then
echo -en "${bold} Dark:"
echo -e "$(tput cr)$(tput cuf 10)${blue}/usr/share/themes/$gtk_theme_dark${all_off}${bold}${all_off}"
2025-12-05 21:17:23 +00:00
fi
}
case "$1" in
s)
check_config && switch-dark-light
;;
r)
check_config && restore-theme-on-login
;;
p)
check_config && preview
;;
*)
echo -e "${bold}$pkgname v$version${all_off}"
echo
echo -e "${bold} Usage: ${red}$0${all_off}${bold} ${all_off}${blue}[option]${all_off}"
echo
echo -e "${bold} s) swith between dark and light theme${all_off}"
echo -e "${bold} r) restore theme (usefull only once after login)${all_off}"
echo -e "${bold} p) Preview settings${all_off}"
;;
esac
# vim: set ts=4 sw=4 et: