dotfiles

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

set-theme (3974B)


      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   # Look up the config file
     69   DEFAULT_MACOS_CONFIG="$HOME"/.config/alacritty/alacritty.yml
     70   CFG="$DEFAULT_MACOS_CONFIG"
     71   [ -L "$DEFAULT_MACOS_CONFIG" ] && {
     72     printf "Following symlink to config...\n"
     73       CFG=$($READLINK -f "$DEFAULT_MACOS_CONFIG")
     74     }
     75 
     76   # Replace 'colors: ...' with 'colors: *(dark|light)' depending on theme
     77   tmpfile=$(mktemp)
     78   trap 'rm $tmpfile' INT TERM EXIT
     79   sed "/^colors: /c\\
     80 colors: *${1}" "$CFG" > "$tmpfile" \
     81     && cat "$tmpfile" > "$CFG" \
     82     && rm "$tmpfile"
     83   trap - INT TERM EXIT
     84 }
     85 
     86 set_terminal_and_wallpaper() {
     87   if [ "$1" = "light" ]; then
     88     wal --saturate 0.8 -i "$(realpath ~/Pictures/Backgrounds/light.jpg)" -l;
     89   else
     90     wal -i "$(realpath ~/Pictures/Backgrounds/dark.jpg)";
     91   fi
     92 }
     93 
     94 set_p10k() {
     95   [ -f ~/.dotfiles/shell/p10k-"$1".zsh ] && ln -sf ~/.dotfiles/shell/p10k-"$1".zsh ~/.p10k.zsh
     96 }
     97 
     98 set_tmux() {
     99   [ -f ~/.dotfiles/tmux/tmux-"$1".conf ] && ln -sf ~/.dotfiles/tmux/tmux-"$1".conf ~/.config/tmux/tmux.conf
    100 }
    101 
    102 [ $# -eq 1 ] || { printf "Arguments: dark, light\n" && exit 1; }
    103 case "$1" in
    104   dark)
    105     theme="dark";;
    106   light)
    107     theme="light";;
    108   toggle)
    109     if [ -f "$HOME/.config/dark-theme" ]; then theme="light"
    110     else theme="dark"; fi;;
    111   *) printf "Arguments: dark, light\n" && exit 1;;
    112 esac
    113 
    114 case "$theme" in
    115   light) rm "$HOME/.config/dark-theme";;
    116   dark) touch "$HOME/.config/dark-theme";;
    117 esac
    118 
    119 os=$(uname -s | tr '[:upper:]' '[:lower:]')
    120 set_terminal_and_wallpaper "$theme"
    121 case "$os" in
    122   darwin*)
    123     set_macos "$theme"
    124     # I don't use iterm2
    125     # set_iterm2 "$theme"
    126     alacritty_change_theme "$theme"
    127     ;;
    128   linux*)
    129     set_gtk "$theme"
    130     ;;
    131 esac
    132 
    133 set_p10k "$theme"
    134 set_tmux "$theme"
    135 set_emacs "$theme"
    136 set_sioyek "$theme"
    137 pgrep dwm >/dev/null 2>&1 && command -v xdotool >/dev/null 2>&1 && xdotool key 'shift+super+r'
    138 exit 0