commit 1b91f4b2fd4e4325b93bb2129dc1f748c9459a70
parent bb0b0cb8fcdf2bc41985e9d5ba30e89ba4ae2f52
Author: Alex Balgavy <alex@balgavy.eu>
Date: Wed, 22 Sep 2021 11:54:01 +0200
setbg & set-theme: play nicer with Alacritty
I added a variant of alacritty-color-export into setbg, so that when
setting a new background, a color scheme based on that background and
the current theme (light/dark) will also get written into alacritty.yml.
That means that in set-theme, I only need to change the 'colors: *dark'
to 'colors: *light' and vice-versa.
Diffstat:
3 files changed, 176 insertions(+), 38 deletions(-)
diff --git a/alacritty/alacritty.yml b/alacritty/alacritty.yml
@@ -106,31 +106,56 @@ mouse:
program: linkhandler
args: []
modifiers: Command
-
-# BEGIN ACE
-colors:
- primary:
- background: '#f9f4f5'
- foreground: '#181924'
- cursor:
- text: '#f9f4f5'
- cursor: '#181924'
- normal:
- black: '#f9f4f5'
- red: '#1647c9'
- green: '#1930e2'
- yellow: '#1d3fe6'
- blue: '#9a24e6'
- magenta: '#3559e8'
- cyan: '#4b4bea'
- white: '#181924'
- bright:
- black: '#847071'
- red: '#1647c9'
- green: '#1930e2'
- yellow: '#1d3fe6'
- blue: '#9a24e6'
- magenta: '#3559e8'
- cyan: '#4b4bea'
- white: '#181924'
-# END ACE
+schemes:
+ dark: &dark
+ primary:
+ background: '#101119'
+ foreground: '#77e1ed'
+ cursor:
+ text: '#101119'
+ cursor: '#77e1ed'
+ normal:
+ black: '#101119'
+ red: '#605FDD'
+ green: '#9450AD'
+ yellow: '#A162DE'
+ blue: '#C864E3'
+ magenta: '#058CB0'
+ cyan: '#04ACD0'
+ white: '#77e1ed'
+ bright:
+ black: '#539da5'
+ red: '#605FDD'
+ green: '#9450AD'
+ yellow: '#A162DE'
+ blue: '#C864E3'
+ magenta: '#058CB0'
+ cyan: '#04ACD0'
+ white: '#77e1ed'
+ light: &light
+ primary:
+ background: '#fbf8fb'
+ foreground: '#1A1923'
+ cursor:
+ text: '#fbf8fb'
+ cursor: '#1A1923'
+ normal:
+ black: '#1A1923'
+ red: '#e75127'
+ green: '#e83076'
+ yellow: '#ed64ed'
+ blue: '#f399a3'
+ magenta: '#f4a2cc'
+ cyan: '#f6b7de'
+ white: '#1A1923'
+ bright:
+ black: '#8b7f89'
+ red: '#e75127'
+ green: '#e83076'
+ yellow: '#ed64ed'
+ blue: '#f399a3'
+ magenta: '#f4a2cc'
+ cyan: '#f6b7de'
+ white: '#1A1923'
+
+colors: *light+
\ No newline at end of file
diff --git a/scripts/set-theme b/scripts/set-theme
@@ -44,8 +44,24 @@ except Exception as exception:
EOF
}
-macos_alacritty_load_colors() {
- [ -x "$HOME"/.dotfiles/tools/alacritty-color-export/script.sh ] && "$HOME"/.dotfiles/tools/alacritty-color-export/script.sh
+# Change the 'colors' setting in the Alacritty config
+alacritty_change_theme() {
+ # Look up the config file
+ DEFAULT_MACOS_CONFIG="$HOME"/.config/alacritty/alacritty.yml
+ CFG="$DEFAULT_MACOS_CONFIG"
+ [ -L "$DEFAULT_MACOS_CONFIG" ] && {
+ printf "Following symlink to config...\n"
+ CFG=$($READLINK -f "$DEFAULT_MACOS_CONFIG")
+ }
+
+ # Replace 'colors: ...' with 'colors: *(dark|light)' depending on theme
+ tmpfile=$(mktemp)
+ trap 'rm $tmpfile' INT TERM EXIT
+ sed "/^colors: /c\\
+colors: *${1}" "$CFG" > "$tmpfile" \
+ && cat "$tmpfile" > "$CFG" \
+ && rm "$tmpfile"
+ trap - INT TERM EXIT
}
set_terminal_and_wallpaper() {
@@ -88,7 +104,7 @@ case "$os" in
set_macos "$theme"
# I don't use iterm2
# set_iterm2 "$theme"
- macos_alacritty_load_colors
+ alacritty_change_theme "$theme"
;;
esac
diff --git a/scripts/setbg b/scripts/setbg
@@ -110,19 +110,115 @@ printf "copying %s -> %s\n" "$imgpath" "$targetname"
cp -f "$imgpath" "$targetname"
# Set the background
+wal -c
# I want to word split here
# shellcheck disable=SC2086
-wal -c
wal $walflags -i "$(realpath "$targetname")"
-set -x
-
-# If on macOS and using Alacritty, gotta do something extra to set terminal colors
+# If on macOS and using Alacritty, gotta do something extra to set terminal colors.
+# Generated light and dark color schemes will be under the name 'light' and 'dark'
+# in the Alacritty config file.
+# 1. Find the config file.
+# 2. Remove the existing generated theme.
+# 3. Write the new colors.
os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$os" in
darwin*)
- [ -n "$ALACRITTY_LOG" ] \
- && [ -x "$HOME"/.dotfiles/tools/alacritty-color-export/script.sh ] \
- && "$HOME"/.dotfiles/tools/alacritty-color-export/script.sh
+ # Logfile defined probably means we're using alacritty
+ if [ -n "$ALACRITTY_LOG" ]; then
+ # Choose the scheme name we're changing
+ if [ -n "$light" ]; then scheme_name="light"
+ else scheme_name="dark"; fi
+
+ # Wal generates a shell script that defines color0..color15
+ SRC="$HOME"/.cache/wal/colors.sh
+
+ [ -e "$SRC" ] || die "Wal colors not found, exiting script. Have you executed Wal before?"
+ printf "Colors found, source ready.\n"
+
+ READLINK=$( command -v greadlink || command -v readlink )
+
+ # Get config file
+ # Default config path in Mac systems
+ DEFAULT_MACOS_CONFIG="$HOME"/.config/alacritty/alacritty.yml
+ [ -e "$DEFAULT_MACOS_CONFIG" ] || die "Alacritty config not found, exiting script."
+ CFG="$DEFAULT_MACOS_CONFIG"
+ [ -L "$DEFAULT_MACOS_CONFIG" ] && {
+ printf "Following symlink to config...\n"
+ CFG=$($READLINK -f "$DEFAULT_MACOS_CONFIG")
+ }
+ [ -f "$CFG" ] || die "$CFG does not exist or not readable."
+
+ # Get hex colors from Wal cache
+ # No need for shellcheck to check this, it comes from pywal
+ # shellcheck disable=SC1090
+ . "$SRC"
+
+ # Create temp file for sed results
+ tempfile=$(mktemp)
+ trap 'rm $tempfile' INT TERM EXIT
+
+ # Delete existing color declarations generated by this script
+ # That means, everything from '$scheme_name: &${scheme_name}' (indendented by 4 spaces) to the next potential scheme definition (indented by 4 spaces),
+ # deleting everything that starts with 8 spaces (the next level of indentation).
+ # Yes, this is currently very dependant on the file's indentation. Which is bad.
+ # If the scheme definition exists, delete it
+ if grep -q "$scheme_name: &${scheme_name}" "$CFG"; then
+ sed <"$CFG" "/^\( \{4\}\)${scheme_name}: &${scheme_name}/,/^ \{4\}[a-z]*: &[a-z]*\$/ { /^ \{8,\}/d; }" > "$tempfile" \
+ && cat "$tempfile" > "$CFG"
+ # If it doesn't:
+ else
+ printf "There's no existing 'generated' colors, adding...\n";
+ # If a 'schemes' declaration exists, place this scheme declaration under it
+ if grep -q "schemes:" "$CFG"; then
+ sed '/schemes:/r /dev/stdin' "$CFG" > "$tempfile" <<EOF
+ $scheme_name: &$scheme_name
+EOF
+ cat "$tempfile" > "$CFG"
+ # If not, create the full schemes declaration
+ else
+ cat "$CFG" - >"$tempfile" <<EOF
+schemes:
+ $scheme_name: &$scheme_name
+EOF
+ cat "$tempfile" > "$CFG"
+ fi
+ fi
+
+ # Write new color definitions
+ # We know $colorX is unset, we set it by sourcing pywal results above, so no need to shellcheck
+ # shellcheck disable=SC2154
+ { sed "/^ \{4\}$scheme_name/ r /dev/stdin" "$CFG" > "$tempfile" <<EOF
+ primary:
+ background: '$color0'
+ foreground: '$color7'
+ cursor:
+ text: '$color0'
+ cursor: '$color7'
+ normal:
+ black: '$color0'
+ red: '$color1'
+ green: '$color2'
+ yellow: '$color3'
+ blue: '$color4'
+ magenta: '$color5'
+ cyan: '$color6'
+ white: '$color7'
+ bright:
+ black: '$color8'
+ red: '$color9'
+ green: '$color10'
+ yellow: '$color11'
+ blue: '$color12'
+ magenta: '$color13'
+ cyan: '$color14'
+ white: '$color15'
+EOF
+ } && sed "/^colors: /c\\
+colors: *${scheme_name}" "$tempfile" > "$CFG" \
+ && rm "$tempfile"
+ trap - INT TERM EXIT
+ printf "'%s' exported to '%s'\n" "$SRC" "$CFG"
+ fi
;;
esac