theme-sel (5252B)
1 #!/bin/sh 2 OLDTHEME="$(cat "$HOME"/.cache/theme || echo "nord")" 3 XRES="${XRES:-${XDG_CONFIG_HOME:-$HOME/.config}/x11}" 4 XRES_SRC="${XRES}/xresources" 5 XRES_COLORS_DST="${XRES}/colors" 6 XRES_COLORS_SRC="${XRES}/themes" 7 8 find_themes() { 9 find "${XRES_COLORS_SRC}" -type f -exec basename {} \; | sort 10 } 11 12 generate_theme() { 13 SRC="$1" 14 DST="$2" 15 [ -f "${DST}.tmp" ] && rm "${DST}.tmp" 16 cp "${SRC}" "${DST}.tmp" 17 18 XRDB="$(xrdb -query)" 19 background="$(printf '%s\n' "$XRDB" | awk '/background/ {print $2;exit}')" 20 foreground="$(printf '%s\n' "$XRDB" | awk '/foreground/ {print $2;exit}')" 21 cursorcolor="$(printf '%s\n' "$XRDB" | awk '/cursorColor/ {print $2;exit}')" 22 23 sed "s|%clfg%|$foreground|g;s|%clbg%|$background|g;s|%clcr%|$cursorcolor|g;s|%HOME%|$HOME|g" "${DST}.tmp" > "${DST}.tmp2" 24 mv "${DST}.tmp2" "${DST}.tmp" 25 26 for i in $(seq 0 15); do 27 v="$(echo "$XRDB" | awk "/color$i/ {print \$2;exit}")" 28 sed "s|%cl${i}%|$v|g" "${DST}.tmp" > "${DST}.tmp2" 29 mv "${DST}.tmp2" "${DST}.tmp" 30 done 31 32 [ -f "${DST}" ] && rm "${DST}" 33 mv "${DST}.tmp" "${DST}" 34 } 35 36 apply_theme() { 37 THEME="$1" 38 XRES_COLORS_SRC="${XRES}/themes/$THEME" 39 cp -f "${XRES_COLORS_SRC}" "${XRES_COLORS_DST}" 40 xrdb "${XRES_SRC}" 41 generate_theme "${HOME}/.config/surf/styles/default.css.in" "${HOME}/.config/surf/styles/default.css" 42 generate_theme "${HOME}/.config/zathura/zathurarc.in" "${HOME}/.config/zathura/zathurarc" 43 echo "$THEME" > "$HOME"/.cache/theme 44 } 45 46 reload_theme() { 47 pidof tabbed >/dev/null 2>&1 && for PID in $(pidof tabbed); do kill -s USR1 "$PID" >/dev/null 2>&1; done 48 pidof st >/dev/null 2>&1 && for PID in $(pidof st); do kill -s USR1 "$PID" >/dev/null 2>&1; done 49 # Remove urgency mark for st, tabbed instances: 50 xdotool search --class st set_window --urgency 0 "%@" 51 xdotool search --class tabbed set_window --urgency 0 "%@" 52 pidof dwm >/dev/null 2>&1 && xdotool key "Super+F5" 53 #pidof bspwm && bspc wm -r 54 #pidof openbox && openbox --restart 55 56 # If you have colorful bar, consider restarting the bar 57 #dwm-bar & 58 59 case "$OLDTHEME" in 60 "${THEME}") NOTIF_TEXT="Reloaded ${THEME} theme." ;; 61 *) NOTIF_TEXT="Set the theme from ${OLDTHEME} to ${THEME}!" ;; 62 esac 63 notify "$(printf '%s\n' " Theme Selector" "$NOTIF_TEXT")" 64 } 65 66 case "$1" in 67 -l) 68 find_themes 69 exit 70 ;; 71 -x) 72 TODAY=$(date +%d) 73 COUNT=$(find_themes | wc -l) 74 THEME=$((TODAY%COUNT)) 75 [ "$THEME" -eq 0 ] && THEME=$COUNT 76 THEME=$(find_themes | sed -n "${THEME}p") 77 ;; 78 -s) 79 NUM=$(find_themes -l | awk "/$OLDTHEME/{print NR-1}") 80 THEME=$(find_themes | sed "s/$OLDTHEME/$OLDTHEME */g" | dmenu -i -n "$NUM" -p "Set Theme:") 81 THEME=${THEME%% *} 82 [ "$THEME" ] || exit 83 ;; 84 *) 85 THEME="${1:-$(cat "$HOME"/.cache/theme || echo "nord")}" 86 ;; 87 esac 88 89 apply_theme "$THEME" 90 91 if [ "$DISPLAY" ]; then 92 #bg-set 93 bg-gen 94 reload_theme 95 fi 96 97 # Old: 98 apply_theme_gtk() { 99 # Maps for themes 100 [ -d "$HOME/.themes" ] \ 101 && THEME_SEARCH+="$HOME/.themes " 102 [ -d "${XDG_DATA_HOME:-$HOME/.local/share}/themes" ] \ 103 && THEME_SEARCH+="${XDG_DATA_HOME:-$HOME/.local/share}/themes " 104 [ -d "/usr/share/themes" ] \ 105 && THEME_SEARCH+="/usr/share/themes " 106 [ -d "/usr/local/share/themes" ] \ 107 && THEME_SEARCH+="/usr/local/share/themes " 108 GTK_THEME="$(find $THEME_SEARCH -type d -iname "*${THEME}*" | head -1)" 109 [ -n "$GTK_THEME" ] && GTK_THEME="$(basename $GTK_THEME)" 110 111 [ -d "$HOME/.icons" ] \ 112 && ICONS_SEARCH+="$HOME/.icons " 113 [ -d "${XDG_DATA_HOME:-$HOME/.local/share}/icons" ] \ 114 && ICONS_SEARCH+="${XDG_DATA_HOME:-$HOME/.local/share}/icons " 115 [ -d "/usr/share/icons" ] \ 116 && ICONS_SEARCH+="/usr/share/icons" 117 [ -d "/usr/local/share/icons" ] \ 118 && ICONS_SEARCH+="/usr/local/share/icons" 119 GTK_ICONS="$(find $ICONS_SEARCH -type d -iname "*${THEME}*" | head -1)" 120 [ -n "$GTK_ICONES" ] && GTK_ICONS="$(basename $GTK_ICONS)" 121 122 _get_ini(){ awk '/\[/{prefix=$0; next} $1{print prefix $0}' "$1" | grep "$2" | sed 's/.*=[ "]//'; } 123 THEMES_MAPS="${XDG_CONFIG_HOME:-$HOME/.config}/themes.ini" 124 #GTK_THEME="$(_get_ini "$THEMES_MAPS" "\[${THEME}\]GTK_THEME")" 125 #GTK_ICONS="$(_get_ini "$THEMES_MAPS" "\[${THEME}\]GTK_ICONS")" 126 GTK_THEME="${GTK_THEME:-Adwaita-dark}" 127 GTK_ICONS="${GTK_THEME:-Adwaita}" 128 129 # set gtk theme, icons and cursor 130 if [ "$(pidof xsettingsd)" ]; then 131 sed -i -e "s|Net/ThemeName .*|Net/ThemeName \"${GTK_THEME}\"|g" "${HOME}/.config/xsettingsd/xsettingsd.conf" 132 sed -i -e "s|Net/IconThemeName .*|Net/IconThemeName \"${GTK_ICONS}\"|g" "${HOME}/.config/xsettingsd/xsettingsd.conf" 133 sed -i -e "s|Gtk/CursorThemeName .*|Gtk/CursorThemeName \"${THEME}\"|g" "${HOME}/.config/xsettingsd/xsettingsd.conf" 134 else 135 sed -i -e "s/gtk-theme-name=.*/gtk-theme-name=\"${GTK_THEME}\"/g" "${HOME}/.gtkrc-2.0" 136 sed -i -e "s/gtk-icon-theme-name=.*/gtk-icon-theme-name=\"${GTK_ICONS}\"/g" "${HOME}/.gtkrc-2.0" 137 sed -i -e "s/gtk-cursor-theme-name=.*/gtk-cursor-theme-name=\"${THEME}\"/g" "${HOME}/.gtkrc-2.0" 138 sed -i -e "s/gtk-theme-name=.*/gtk-theme-name=${GTK_THEME}/g" "${CFG}/gtk-3.0/settings.ini" 139 sed -i -e "s/gtk-icon-theme-name=.*/gtk-icon-theme-name=${GTK_ICONS}/g" "${CFG}/gtk-3.0/settings.ini" 140 sed -i -e "s/gtk-cursor-theme-name=.*/gtk-cursor-theme-name=${THEME}/g" "${CFG}/gtk-3.0/settings.ini" 141 fi 142 }