Permanent process for speed

This commit is contained in:
David Heinemeier Hansson
2026-05-11 15:29:11 -04:00
committed by Ryan Hughes
parent 5487c17d19
commit 0b2332e45d
3 changed files with 184 additions and 30 deletions
+95 -14
View File
@@ -36,7 +36,11 @@ if (( ${#image_dirs[@]} == 0 )); then
fi
selection_file=$(mktemp)
trap 'rm -f "$selection_file"' EXIT
done_file=$(mktemp)
rm -f "$done_file"
trap 'rm -f "$selection_file" "$done_file"' EXIT
socket_path="${XDG_RUNTIME_DIR:-/run/user/$UID}/omarchy-image-selector.sock"
selector_qml="$OMARCHY_PATH/default/quickshell/wallpaper-switcher.qml"
image_dirs_env=""
for dir in "${image_dirs[@]}"; do
@@ -60,26 +64,103 @@ if [[ -n $current_image ]]; then
fi
cache_dir=${XDG_CACHE_HOME:-$HOME/.cache}/omarchy/image-selector
index_file="$cache_dir/index.tsv"
rows=""
mkdir -p "$cache_dir"
cache_key=$(printf '%s' "$image_dirs_env" | md5sum | cut -d ' ' -f 1)
rows_cache_file="$cache_dir/$cache_key.rows"
rows_signature_file="$cache_dir/$cache_key.signature"
rows_signature=""
for dir in "${image_dirs[@]}"; do
if [[ -d $dir ]]; then
while IFS= read -r -d '' image; do
hash=$(md5sum "$image" | cut -d ' ' -f 1)
thumbnail="$cache_dir/$hash.jpg"
if [[ ! -f $thumbnail ]]; then
magick "$image" -auto-orient -resize '1536x864^' -gravity center -extent '1536x864' -strip -quality 82 "$thumbnail"
fi
done < <(find -L "$dir" -maxdepth 1 -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.webp' \) -print0 2>/dev/null)
rows_signature+="$dir:$(stat -Lc '%Y' "$dir")"$'\n'
fi
done
OMARCHY_IMAGE_SELECTOR_SELECTION_FILE=$selection_file \
OMARCHY_IMAGE_SELECTOR_DIRS="$image_dirs_env" \
OMARCHY_IMAGE_SELECTOR_SELECTED="$selected_list_image" \
OMARCHY_IMAGE_SELECTOR_COLORS_FILE="$colors_file" \
quickshell -p "$OMARCHY_PATH/default/quickshell/wallpaper-switcher.qml" >/dev/null
thumbnail_for() {
local image="$1"
local signature hash thumbnail
signature=$(stat -Lc '%s:%Y' "$image") || return
hash=$(awk -F '\t' -v path="$image" -v sig="$signature" '$1 == path && $2 == sig { print $3; exit }' "$index_file" 2>/dev/null)
if [[ -z $hash ]]; then
hash=$(md5sum "$image" | cut -d ' ' -f 1)
printf '%s\t%s\t%s\n' "$image" "$signature" "$hash" >>"$index_file"
fi
thumbnail="$cache_dir/$hash.jpg"
if [[ ! -f $thumbnail ]]; then
magick "$image" -auto-orient -resize '1536x864^' -gravity center -extent '1536x864' -strip -quality 82 "$thumbnail"
fi
printf '%s' "$thumbnail"
}
if [[ -f $rows_cache_file && -f $rows_signature_file ]] && cmp -s "$rows_signature_file" <(printf '%s' "$rows_signature"); then
rows=$(<"$rows_cache_file")
else
for dir in "${image_dirs[@]}"; do
if [[ -d $dir ]]; then
while IFS= read -r -d '' image; do
thumbnail=$(thumbnail_for "$image")
[[ -n $thumbnail ]] || continue
if [[ -z $rows ]]; then
rows="$image"$'\t'"$thumbnail"
else
rows+=$'\n'"$image"$'\t'"$thumbnail"
fi
done < <(find -L "$dir" -maxdepth 1 -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' -o -iname '*.webp' \) -print0 2>/dev/null)
fi
done
printf '%s' "$rows" >"$rows_cache_file"
printf '%s' "$rows_signature" >"$rows_signature_file"
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
fi
if [[ ! -S $socket_path ]]; then
quickshell kill -p "$selector_qml" >/dev/null 2>&1 || true
quickshell -d -p "$selector_qml" >/dev/null
for ((i = 0; i < 100; i++)); do
if [[ -S $socket_path ]]; then
break
fi
sleep 0.01
done
fi
if [[ ! -S $socket_path ]]; then
echo "Image selector failed to start" >&2
exit 1
fi
printf '%s\t%s\t%s\t%s\t%s\n' "$rows_payload" "$selected_list_image" "$selection_file" "$done_file" "$colors_file" |
socat -u - "UNIX-CONNECT:$socket_path"
while [[ ! -e $done_file ]]; do
sleep 0.01
done
if [[ -s $selection_file ]]; then
cat "$selection_file"