From fed2e42488194bfc45a55f687dd97b28a998a3f3 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 10 Mar 2024 11:00:17 +0100 Subject: more refactoring --- .local/share/bin/mode | 2 +- .local/share/mode/bin/mode | 107 ------------------------- .local/share/mode/lib/accent.sh | 10 --- .local/share/mode/lib/functions.sh | 4 - .local/share/mode/mode | 112 +++++++++++++++++++++++++++ .local/share/mode/plug.d/10_lib | 5 ++ .local/share/mode/plug.d/20_export_colors | 17 ++++ .local/share/mode/plug.d/20_export_mode | 9 +++ .local/share/mode/plug.d/20_export_vim_theme | 6 ++ .local/share/mode/plug.d/50_accent | 16 ++++ .local/share/mode/switch.d/chromium | 2 - .local/share/mode/switch.d/discord | 2 - .local/share/mode/themes/github-dark | 41 +++++----- .local/share/mode/themes/github-light | 41 +++++----- 14 files changed, 208 insertions(+), 166 deletions(-) delete mode 100755 .local/share/mode/bin/mode delete mode 100644 .local/share/mode/lib/accent.sh delete mode 100644 .local/share/mode/lib/functions.sh create mode 100755 .local/share/mode/mode create mode 100644 .local/share/mode/plug.d/10_lib create mode 100644 .local/share/mode/plug.d/20_export_colors create mode 100644 .local/share/mode/plug.d/20_export_mode create mode 100644 .local/share/mode/plug.d/20_export_vim_theme create mode 100644 .local/share/mode/plug.d/50_accent diff --git a/.local/share/bin/mode b/.local/share/bin/mode index 8b72998..5a5c6d9 120000 --- a/.local/share/bin/mode +++ b/.local/share/bin/mode @@ -1 +1 @@ -../mode/bin/mode \ No newline at end of file +../mode/mode \ No newline at end of file diff --git a/.local/share/mode/bin/mode b/.local/share/mode/bin/mode deleted file mode 100755 index a4e6947..0000000 --- a/.local/share/mode/bin/mode +++ /dev/null @@ -1,107 +0,0 @@ -#!/bin/sh -progname="$(basename "$0")" -data="$(readlink -f "$(dirname "$(readlink -f "$0")")/..")" -export data - -stupid() { - echo "run \`$progname --help\` for options" >&2 -} -usage() { - cat << EOF -$progname -- switch system themes - -usage: - $progname [-chr] action|theme - -options: - -c, --no-cfggen disable updating of configuration files - -h, --help display this help text - -r, --no-reload disable reloading of applications after applying theme - -actions: - restore switch to last set theme ($XDG_CACHE_HOME/mode/state/theme) - reload do not update configuration files, only reload applications - help same as --help option - -themes: - $(ls "$data/themes" | tr '\n' ' ' | fold -sw 60) - -examples: - mode dark switch to dark mode and reload applications - mode restore --no-reload make sure all configuration files are up-to-date - mode reload only reload applications - -EOF -} - -. "$data/lib/functions.sh" - -switch() { - theme_path="$(readlink -f "$data/themes/$theme")" - theme="$(basename "$theme_path")" - echo "setting theme to $theme..." - - . "$theme_path" - - case "$mode" in - dark|light) break ;; - *) - echo "error: theme $theme does not define a valid \$mode" >&2 - stupid && exit 1 - ;; - esac - export mode - - # create accent color differently for dark/light mode - . "$data/lib/accent.sh" - - for switch_function in "$data"/switch.d/* ; do - ! [ -x "$switch_function" ] && continue - "$switch_function" & - done - - wait $(jobs -rp) -} - -reload() { - echo "reloading programs..." - - for reload_function in "$data"/reload.d/* ; do - ! [ -x "$reload_function" ] && continue - "$reload_function" & - done - - wait $(jobs -rp) -} - -run_cfggen=1 -run_reload=1 -theme="" - -for arg in "$@" ; do - case "$arg" in - --) break ;; - -h|--help|help) usage ; exit 0 ;; - restore) theme="$(cat "$XDG_CACHE_HOME/mode/state/theme")" ;; - reload) run_reload=1 run_cfggen=0 ;; - -r|--no-reload) run_reload=0 ;; - -c|--no-cfggen) run_cfggen=0 ;; - *) - if [ -e "$data/themes/$arg" ] ; then - theme="$arg" - continue - fi - echo "error: unknown argument or theme: $arg" >&2 - stupid && exit 1 - ;; - esac -done -if [ $run_cfggen -eq 1 ] && [ -z "$theme" ] ; then - echo "error: no theme selected" >&2 - stupid && exit 1 -fi -export theme - -[ $run_cfggen -eq 1 ] && switch -[ $run_reload -eq 1 ] && reload - diff --git a/.local/share/mode/lib/accent.sh b/.local/share/mode/lib/accent.sh deleted file mode 100644 index 45643eb..0000000 --- a/.local/share/mode/lib/accent.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh -[ "$mode" == "light" ] && { - export accent="$color15" - export accent_text="$color0" -} -[ "$mode" == "dark" ] && { - export accent="$(mix_rgb $bg $color0 0.5)" - export accent_text="$fg" -} - diff --git a/.local/share/mode/lib/functions.sh b/.local/share/mode/lib/functions.sh deleted file mode 100644 index c7212e4..0000000 --- a/.local/share/mode/lib/functions.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -hex_to_rgb_array() { pastel format rgb "$1" | cut -c4- | tr '()' '[]' ; } -mix_rgb() { pastel mix --colorspace=RGB --fraction="$3" "$1" "$2" | pastel format hex ; } - diff --git a/.local/share/mode/mode b/.local/share/mode/mode new file mode 100755 index 0000000..06ec7dd --- /dev/null +++ b/.local/share/mode/mode @@ -0,0 +1,112 @@ +#!/bin/sh +progname="$(basename "$0")" +data="$(dirname "$(readlink -f "$0")")" +export data + +stupid() { + echo "run \`$progname --help\` for options" >&2 +} +usage() { + cat << EOF +$progname -- switch system themes + +usage: + $progname [-chr] action|theme + +options: + -c, --no-cfggen disable updating of configuration files + -h, --help display this help text + -r, --no-reload disable reloading of applications after applying theme + +actions: + restore switch to last set theme ($XDG_CACHE_HOME/mode/state/theme) + reload do not update configuration files, only reload applications + help same as --help option + list list all themes + +examples: + mode dark switch to dark mode and reload applications + mode restore --no-reload make sure all configuration files are up-to-date + mode reload only reload applications + +EOF +} + +switch() { + theme_path="$(readlink -f "$data/themes/$theme")" + theme="$(basename "$theme_path")" + echo "setting theme to $theme..." + + # load theme colors + aux variables + . "$theme_path" + + # load plugins (available to switch.d scripts only) + for plugin in "$data"/plug.d/* ; do . "$plugin" ; done + + # generate new config files / snippets (in parallel) + for switch_function in "$data"/switch.d/* ; do + ! [ -x "$switch_function" ] && continue + interpreter="$(basename "$(command -v $(head -n1 "$switch_function" | sed -n 's/^#!\(.*\)/\1/p'))")" + if [ "$interpreter" = "sh" ] ; then + # source scripts if they are POSIX sh (makes plugin functions available) + . "$switch_function" & + else + # else, just run them + "$switch_function" & + fi + done + + # join all processes started above + wait $(jobs -rp) +} + +reload() { + echo "reloading programs..." + + for reload_function in "$data"/reload.d/* ; do + ! [ -x "$reload_function" ] && continue + "$reload_function" & + done + + wait $(jobs -rp) +} + +run_cfggen=1 +run_reload=1 +theme="" + +for arg in "$@" ; do + case "$arg" in + --) break ;; + -h|--help|help) usage ; exit 0 ;; + restore) theme="$(cat "$XDG_CACHE_HOME/mode/state/theme")" ;; + reload) run_reload=1 run_cfggen=0 ;; + list) + find "$data/themes" -type f,l | while read -r theme ; do + printf '%s' "$(basename "$theme")" + link="$(readlink "$theme")" && printf ' (%s)' "$link" + printf '\n' + done + exit 0 + ;; + -r|--no-reload) run_reload=0 ;; + -c|--no-cfggen) run_cfggen=0 ;; + *) + if [ -e "$data/themes/$arg" ] ; then + theme="$arg" + continue + fi + echo "error: unknown argument or theme: $arg" >&2 + stupid && exit 1 + ;; + esac +done +if [ $run_cfggen -eq 1 ] && [ -z "$theme" ] ; then + echo "error: no theme selected" >&2 + stupid && exit 1 +fi +export theme + +[ $run_cfggen -eq 1 ] && switch +[ $run_reload -eq 1 ] && reload + diff --git a/.local/share/mode/plug.d/10_lib b/.local/share/mode/plug.d/10_lib new file mode 100644 index 0000000..61817bc --- /dev/null +++ b/.local/share/mode/plug.d/10_lib @@ -0,0 +1,5 @@ +#!/bin/sh +# utility library functions +hex_to_rgb_array() { pastel format rgb "$1" | cut -c4- | tr '()' '[]' ; } +mix_rgb() { pastel mix --colorspace=RGB --fraction="$3" "$1" "$2" | pastel format hex ; } + diff --git a/.local/share/mode/plug.d/20_export_colors b/.local/share/mode/plug.d/20_export_colors new file mode 100644 index 0000000..017446d --- /dev/null +++ b/.local/share/mode/plug.d/20_export_colors @@ -0,0 +1,17 @@ +#!/bin/sh +# colors must be #RRGGBB +check_color() { + ( + eval "color=\"\${$1}\"" + exit "$(echo "$color" | sed -n '/^#[0-9a-fA-F]\{6\}$/p' | wc -l)" + ) + if [ $? -ne 1 ] ; then + echo "error: color $1 is not properly formatted (#RRGGBB)" + stupid && exit 1 + fi + eval "export $1" +} +for color in $(seq 0 15 | sed 's/^/color/') bg fg ; do + check_color $color +done + diff --git a/.local/share/mode/plug.d/20_export_mode b/.local/share/mode/plug.d/20_export_mode new file mode 100644 index 0000000..78432b6 --- /dev/null +++ b/.local/share/mode/plug.d/20_export_mode @@ -0,0 +1,9 @@ +#!/bin/sh +# $mode = "light" | "dark" +if [ "$mode" != 'dark' ] && [ "$mode" != 'light' ] ; then + pastel format hsl-lightness "$bg" | awk '{ exit($1 > 0.5) }' + [ $? -eq 0 ] && mode=dark || mode=light + echo "warn: theme $theme did not define \$mode as \"light\" or \"dark\", guessed $mode" >&2 +fi +export mode + diff --git a/.local/share/mode/plug.d/20_export_vim_theme b/.local/share/mode/plug.d/20_export_vim_theme new file mode 100644 index 0000000..be76b8b --- /dev/null +++ b/.local/share/mode/plug.d/20_export_vim_theme @@ -0,0 +1,6 @@ +#!/bin/sh +# warn if the vim theme is not installed +if [ "$(find "$XDG_CONFIG_HOME/nvim" -name "$vim_theme_name.vim" | wc -l)" -eq 0 ] ; then + echo "warn: vim theme $vim_theme_name does not appear to be installed" >&2 +fi +export vim_theme_name diff --git a/.local/share/mode/plug.d/50_accent b/.local/share/mode/plug.d/50_accent new file mode 100644 index 0000000..6e0aa73 --- /dev/null +++ b/.local/share/mode/plug.d/50_accent @@ -0,0 +1,16 @@ +#!/bin/sh +# create accent color differently for dark/light mode + +# do not generate accent color if already explicitly defined by theme +[ -n "$accent" ] && return + +[ "$mode" == "light" ] && { + accent="$color15" + accent_text="$color0" +} +[ "$mode" == "dark" ] && { + accent="$(mix_rgb $bg $color0 0.5)" + accent_text="$fg" +} + +export accent accent_text diff --git a/.local/share/mode/switch.d/chromium b/.local/share/mode/switch.d/chromium index 5f4f342..6ccfca6 100755 --- a/.local/share/mode/switch.d/chromium +++ b/.local/share/mode/switch.d/chromium @@ -1,6 +1,4 @@ #!/bin/sh -. "$XDG_DATA_HOME/mode/lib/functions.sh" - PREFIX="$XDG_CACHE_HOME/mode/chromium" mkdir -p "$PREFIX" diff --git a/.local/share/mode/switch.d/discord b/.local/share/mode/switch.d/discord index 2a08662..f656783 100755 --- a/.local/share/mode/switch.d/discord +++ b/.local/share/mode/switch.d/discord @@ -1,6 +1,4 @@ #!/bin/sh -. "$data/lib/functions.sh" - cat << EOF > "$XDG_CONFIG_HOME/BetterDiscord/themes/mode.theme.css" /** * @name mode diff --git a/.local/share/mode/themes/github-dark b/.local/share/mode/themes/github-dark index f2113a8..577106c 100644 --- a/.local/share/mode/themes/github-dark +++ b/.local/share/mode/themes/github-dark @@ -1,24 +1,25 @@ #!/bin/sh -export mode='dark' +mode=dark -export color0='#484f58' -export color1='#ffa198' -export color2='#56d364' -export color3='#e3b341' -export color4='#79c0ff' -export color5='#d2a8ff' -export color6='#56d4dd' -export color7='#b1bac4' -export color8='#6e7681' -export color9='#ff7b72' -export color10='#3fb950' -export color11='#d29922' -export color12='#58a6ff' -export color13='#bc8cff' -export color14='#39c5cf' -export color15='#ffffff' +color0='#484f58' +color1='#ffa198' +color2='#56d364' +color3='#e3b341' +color4='#79c0ff' +color5='#d2a8ff' +color6='#56d4dd' +color7='#b1bac4' +color8='#6e7681' +color9='#ff7b72' +color10='#3fb950' +color11='#d29922' +color12='#58a6ff' +color13='#bc8cff' +color14='#39c5cf' +color15='#ffffff' -export bg='#0d1117' -export fg='#e6edf3' +bg='#0d1117' +fg='#e6edf3' + +vim_theme_name='ghdark' -export vim_theme_name='ghdark' diff --git a/.local/share/mode/themes/github-light b/.local/share/mode/themes/github-light index d4a5c28..d0162f8 100644 --- a/.local/share/mode/themes/github-light +++ b/.local/share/mode/themes/github-light @@ -1,24 +1,25 @@ #!/bin/sh -export mode='light' +mode=light -export color0='#d1d5da' -export color1='#cb2431' -export color2='#22863a' -export color3='#b08800' -export color4='#005cc5' -export color5='#5a32a3' -export color6='#3192aa' -export color7='#6a737d' -export color8='#959da5' -export color9='#d73a49' -export color10='#28a745' -export color11='#dbab09' -export color12='#0366d6' -export color13='#5a32a3' -export color14='#1b7c83' -export color15='#24292e' +color0='#d1d5da' +color1='#cb2431' +color2='#22863a' +color3='#b08800' +color4='#005cc5' +color5='#5a32a3' +color6='#3192aa' +color7='#6a737d' +color8='#959da5' +color9='#d73a49' +color10='#28a745' +color11='#dbab09' +color12='#0366d6' +color13='#5a32a3' +color14='#1b7c83' +color15='#24292e' -export bg='#ffffff' -export fg='#24292e' +bg='#ffffff' +fg='#24292e' + +vim_theme_name='github-light' -export vim_theme_name='github-light' -- cgit v1.2.3