dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

set-theme (3991B)


      1 #!/bin/sh
      2 # vim: foldmethod=syntax foldlevel=1
      3 export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
      4 set_macos() {
      5   case "$1" in
      6     dark) dark="true";;
      7     light) dark="false";;
      8   esac
      9   osascript - <<EOF
     10 on run
     11   tell application "System Events"
     12     tell appearance preferences
     13       set dark mode to $dark
     14     end tell
     15   end tell
     16 end run
     17 EOF
     18 }
     19 
     20 set_iterm2() {
     21   PYTHONPATH='' ITERM2_COOKIE=$(osascript -e 'tell application "iTerm2" to request cookie') ~/Library/ApplicationSupport/iTerm2/iterm2env/versions/3.8.6/bin/python3 \
     22     - "$1" <<EOF
     23 import iterm2
     24 from sys import argv
     25 async def main(connection):
     26     profiles = [p for p in await iterm2.PartialProfile.async_query(connection) if p.name.startswith("Default ")]
     27     if len(argv) > 1:
     28         theme = str(argv[1])
     29     else:
     30         import subprocess
     31         result = subprocess.run(['osascript', '-e', 'tell application "System Events" to tell appearance preferences to return (get dark mode as text)'], stdout=subprocess.PIPE)
     32         theme = str(result.stdout.rstrip().decode("utf-8"))
     33     for p in profiles:
     34         if "Light" in p.name and theme == "light":
     35             await p.async_make_default()
     36             return
     37         elif "Dark" in p.name and theme == "dark":
     38             await p.async_make_default()
     39             return
     40 try:
     41     iterm2.run_until_complete(main)
     42 except Exception as exception:
     43     print("Error: ", exception)
     44 EOF
     45 }
     46 
     47 set_gtk() {
     48   mkdir -p ~/.config/gtk-2.0 ~/.config/gtk-3.0
     49   [ -f $DOTFILES/gtk-2.0/"$1" ] && ln -sf $DOTFILES/gtk-2.0/"$1" ~/.config/gtk-2.0/theme
     50   [ -f $DOTFILES/gtk-3.0/"$1".ini ] && ln -sf $DOTFILES/gtk-3.0/"$1".ini ~/.config/gtk-3.0/settings.ini
     51 }
     52 
     53 set_sioyek() {
     54   sioyek_config="$(realpath ~/.config/sioyek/prefs_user.config)"
     55   if [ -f "$sioyek_config" ]; then
     56     sed "/source .*\.config/s!\./.*\.config!./$1.config!" "${sioyek_config}" > "${sioyek_config}.new" \
     57       && mv "${sioyek_config}.new" "${sioyek_config}"
     58   fi
     59 }
     60 
     61 set_emacs() {
     62   pgrep emacsclient >/dev/null 2>&1 \
     63     && emacsclient -d $DISPLAY -nw -e "(progn (za/$1-theme) (kill-terminal))"
     64 }
     65 
     66 # Change the 'colors' setting in the Alacritty config
     67 alacritty_change_theme() {
     68   set -x
     69   # Look up the config file
     70   DEFAULT_MACOS_CONFIG="$HOME"/.config/alacritty/alacritty.toml
     71   CFG="$DEFAULT_MACOS_CONFIG"
     72   [ -L "$DEFAULT_MACOS_CONFIG" ] && {
     73     printf "Following symlink to config...\n"
     74     CFG=$($READLINK -f "$DEFAULT_MACOS_CONFIG")
     75   }
     76 
     77   # Replace 'colors: ...' with 'colors: *(dark|light)' depending on theme
     78   tmpfile=$(mktemp)
     79   trap 'rm $tmpfile' INT TERM EXIT
     80   sed "s/theme-[a-z]*.toml/theme-${1}.toml/" "$CFG" >"$tmpfile" &&
     81     cat "$tmpfile" >"$CFG" &&
     82     rm "$tmpfile"
     83   trap - INT TERM EXIT
     84   set +x
     85 }
     86 
     87 set_terminal_and_wallpaper() {
     88   if [ "$1" = "light" ]; then
     89     wal --saturate 0.8 -i "$(realpath ~/Pictures/Backgrounds/light.jpg)" -l;
     90   else
     91     wal -i "$(realpath ~/Pictures/Backgrounds/dark.jpg)";
     92   fi
     93 }
     94 
     95 set_p10k() {
     96   [ -f ~/.dotfiles/shell/p10k-"$1".zsh ] && ln -sf ~/.dotfiles/shell/p10k-"$1".zsh ~/.p10k.zsh
     97 }
     98 
     99 set_tmux() {
    100   [ -f ~/.dotfiles/tmux/tmux-"$1".conf ] && ln -sf ~/.dotfiles/tmux/tmux-"$1".conf ~/.config/tmux/tmux.conf
    101 }
    102 
    103 [ $# -eq 1 ] || { printf "Arguments: dark, light\n" && exit 1; }
    104 case "$1" in
    105   dark)
    106     theme="dark";;
    107   light)
    108     theme="light";;
    109   toggle)
    110     if [ -f "$HOME/.config/dark-theme" ]; then theme="light"
    111     else theme="dark"; fi;;
    112   *) printf "Arguments: dark, light\n" && exit 1;;
    113 esac
    114 
    115 case "$theme" in
    116   light) rm "$HOME/.config/dark-theme";;
    117   dark) touch "$HOME/.config/dark-theme";;
    118 esac
    119 
    120 os=$(uname -s | tr '[:upper:]' '[:lower:]')
    121 set_terminal_and_wallpaper "$theme"
    122 case "$os" in
    123   darwin*)
    124     set_macos "$theme"
    125     # I don't use iterm2
    126     # set_iterm2 "$theme"
    127     alacritty_change_theme "$theme"
    128     ;;
    129   linux*)
    130     set_gtk "$theme"
    131     ;;
    132 esac
    133 
    134 set_p10k "$theme"
    135 set_tmux "$theme"
    136 set_emacs "$theme"
    137 set_sioyek "$theme"
    138 pgrep dwm >/dev/null 2>&1 && command -v xdotool >/dev/null 2>&1 && xdotool key 'shift+super+r'
    139 exit 0