From 000b7c79afb61f535d2b3135a96dcf47b8448d9a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 11 May 2026 17:42:55 +0200 Subject: [PATCH] Improve performance --- bin/omarchy-menu-images | 37 +++++++++++++--------- bin/omarchy-theme-switcher | 65 ++++++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 39 deletions(-) diff --git a/bin/omarchy-menu-images b/bin/omarchy-menu-images index 9f50c729..ad8e0dc2 100755 --- a/bin/omarchy-menu-images +++ b/bin/omarchy-menu-images @@ -1,12 +1,13 @@ #!/bin/bash # omarchy:summary=Open a generic image selector menu -# omarchy:args=[--selected ] [--colors-file ] ... +# omarchy:args=[--selected ] [--colors-file ] [--print-name] ... OMARCHY_PATH=${OMARCHY_PATH:-$HOME/.local/share/omarchy} selected_image="" colors_file="" +print_name=false image_dirs=() while [[ $# -gt 0 ]]; do @@ -19,8 +20,12 @@ while [[ $# -gt 0 ]]; do colors_file="$2" shift 2 ;; + --print-name) + print_name=true + shift + ;; --help|-h) - echo "Usage: omarchy-menu-images [--selected ] [--colors-file ] ..." + echo "Usage: omarchy-menu-images [--selected ] [--colors-file ] [--print-name] ..." exit 0 ;; *) @@ -31,7 +36,7 @@ while [[ $# -gt 0 ]]; do done if (( ${#image_dirs[@]} == 0 )); then - echo "Usage: omarchy-menu-images [--selected ] [--colors-file ] ..." >&2 + echo "Usage: omarchy-menu-images [--selected ] [--colors-file ] [--print-name] ..." >&2 exit 1 fi @@ -56,7 +61,10 @@ selected_list_image="" if [[ -n $current_image ]]; then for dir in "${image_dirs[@]}"; do - if [[ -d $dir ]]; then + if [[ -d $dir && -f $selected_image && ${selected_image%/*} == "$dir" ]]; then + selected_list_image="$selected_image" + break + elif [[ -d $dir ]]; then selected_list_image=$(find -L "$dir" -maxdepth 1 -type f -samefile "$current_image" -print -quit 2>/dev/null) [[ -n $selected_list_image ]] && break fi @@ -125,16 +133,9 @@ fi rows_payload=${rows//$'\t'/$'\f'} rows_payload=${rows_payload//$'\n'/$'\v'} -selector_pid=$(quickshell list -p "$selector_qml" --json 2>/dev/null | jq -r '.[0].pid // empty') -if [[ -n $selector_pid ]]; then - selector_age=$(ps -o etimes= -p "$selector_pid" 2>/dev/null | tr -d ' ') - qml_mtime=$(stat -c %Y "$selector_qml") - now=$(date +%s) - - if [[ -n $selector_age ]] && (( now - selector_age < qml_mtime )); then - quickshell kill -p "$selector_qml" >/dev/null 2>&1 || true - rm -f "$socket_path" - fi +if [[ -S $socket_path && $selector_qml -nt $socket_path ]]; then + quickshell kill -p "$selector_qml" >/dev/null 2>&1 || true + rm -f "$socket_path" fi if [[ ! -S $socket_path ]]; then @@ -163,5 +164,11 @@ while [[ ! -e $done_file ]]; do done if [[ -s $selection_file ]]; then - cat "$selection_file" + if [[ $print_name == true ]]; then + selection=$(<"$selection_file") + selection=${selection##*/} + printf '%s\n' "${selection%.*}" + else + cat "$selection_file" + fi fi diff --git a/bin/omarchy-theme-switcher b/bin/omarchy-theme-switcher index 3b498394..89480c96 100755 --- a/bin/omarchy-theme-switcher +++ b/bin/omarchy-theme-switcher @@ -5,9 +5,11 @@ OMARCHY_PATH=${OMARCHY_PATH:-$HOME/.local/share/omarchy} USER_THEMES_PATH="$HOME/.config/omarchy/themes" OMARCHY_THEMES_PATH="$OMARCHY_PATH/themes" +CACHE_PATH="${XDG_CACHE_HOME:-$HOME/.cache}/omarchy/theme-selector" +preview_dir="$CACHE_PATH/previews" +signature_file="$CACHE_PATH/signature" -preview_dir=$(mktemp -d) -trap 'rm -rf "$preview_dir"' EXIT +mkdir -p "$preview_dir" find_preview() { local theme_path="$1" @@ -32,31 +34,46 @@ add_theme_preview() { ln -s "$preview" "$preview_dir/$theme_name.$extension" } -while IFS= read -r theme_path; do - theme_name=${theme_path##*/} - preview=$(find_preview "$theme_path") - - if [[ -z $preview ]]; then - preview=$(find_preview "$OMARCHY_THEMES_PATH/$theme_name") +theme_signature="" +for theme_dir in "$USER_THEMES_PATH" "$OMARCHY_THEMES_PATH"; do + if [[ -d $theme_dir ]]; then + theme_signature+="$theme_dir:$(stat -Lc '%Y' "$theme_dir")"$'\n' fi +done - add_theme_preview "$theme_name" "$preview" -done < <(find -L "$USER_THEMES_PATH" -mindepth 1 -maxdepth 1 \( -type d -o -type l \) -print 2>/dev/null | sort) +if [[ ! -f $signature_file ]] || ! cmp -s "$signature_file" <(printf '%s' "$theme_signature"); then + rm -rf "$preview_dir" + mkdir -p "$preview_dir" -while IFS= read -r theme_path; do - theme_name=${theme_path##*/} - preview=$(find_preview "$theme_path") - add_theme_preview "$theme_name" "$preview" -done < <(find -L "$OMARCHY_THEMES_PATH" -mindepth 1 -maxdepth 1 -type d -print 2>/dev/null | sort) + while IFS= read -r theme_path; do + theme_name=${theme_path##*/} + preview=$(find_preview "$theme_path") + + if [[ -z $preview ]]; then + preview=$(find_preview "$OMARCHY_THEMES_PATH/$theme_name") + fi + + add_theme_preview "$theme_name" "$preview" + done < <(find -L "$USER_THEMES_PATH" -mindepth 1 -maxdepth 1 \( -type d -o -type l \) -print 2>/dev/null | sort) + + while IFS= read -r theme_path; do + theme_name=${theme_path##*/} + preview=$(find_preview "$theme_path") + add_theme_preview "$theme_name" "$preview" + done < <(find -L "$OMARCHY_THEMES_PATH" -mindepth 1 -maxdepth 1 -type d -print 2>/dev/null | sort) + + printf '%s' "$theme_signature" >"$signature_file" +fi current_theme=$(cat "$HOME/.config/omarchy/current/theme.name" 2>/dev/null) -selected_preview=$(find -L "$preview_dir" -maxdepth 1 -type f -name "$current_theme.*" -print -quit 2>/dev/null) -selected_preview=$(omarchy-menu-images \ +selected_preview="" +for extension in png jpg jpeg webp; do + if [[ -e $preview_dir/$current_theme.$extension ]]; then + selected_preview="$preview_dir/$current_theme.$extension" + break + fi +done +exec omarchy-menu-images \ + --print-name \ --selected "$selected_preview" \ - "$preview_dir") - -if [[ -n $selected_preview ]]; then - selected_theme=${selected_preview##*/} - selected_theme=${selected_theme%.*} - printf '%s\n' "$selected_theme" -fi + "$preview_dir"