Improve performance
This commit is contained in:
+22
-15
@@ -1,12 +1,13 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# omarchy:summary=Open a generic image selector menu
|
# omarchy:summary=Open a generic image selector menu
|
||||||
# omarchy:args=[--selected <image>] [--colors-file <path>] <image-dir>...
|
# omarchy:args=[--selected <image>] [--colors-file <path>] [--print-name] <image-dir>...
|
||||||
|
|
||||||
OMARCHY_PATH=${OMARCHY_PATH:-$HOME/.local/share/omarchy}
|
OMARCHY_PATH=${OMARCHY_PATH:-$HOME/.local/share/omarchy}
|
||||||
|
|
||||||
selected_image=""
|
selected_image=""
|
||||||
colors_file=""
|
colors_file=""
|
||||||
|
print_name=false
|
||||||
image_dirs=()
|
image_dirs=()
|
||||||
|
|
||||||
while [[ $# -gt 0 ]]; do
|
while [[ $# -gt 0 ]]; do
|
||||||
@@ -19,8 +20,12 @@ while [[ $# -gt 0 ]]; do
|
|||||||
colors_file="$2"
|
colors_file="$2"
|
||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
|
--print-name)
|
||||||
|
print_name=true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
--help|-h)
|
--help|-h)
|
||||||
echo "Usage: omarchy-menu-images [--selected <image>] [--colors-file <path>] <image-dir>..."
|
echo "Usage: omarchy-menu-images [--selected <image>] [--colors-file <path>] [--print-name] <image-dir>..."
|
||||||
exit 0
|
exit 0
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
@@ -31,7 +36,7 @@ while [[ $# -gt 0 ]]; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
if (( ${#image_dirs[@]} == 0 )); then
|
if (( ${#image_dirs[@]} == 0 )); then
|
||||||
echo "Usage: omarchy-menu-images [--selected <image>] [--colors-file <path>] <image-dir>..." >&2
|
echo "Usage: omarchy-menu-images [--selected <image>] [--colors-file <path>] [--print-name] <image-dir>..." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -56,7 +61,10 @@ selected_list_image=""
|
|||||||
|
|
||||||
if [[ -n $current_image ]]; then
|
if [[ -n $current_image ]]; then
|
||||||
for dir in "${image_dirs[@]}"; do
|
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)
|
selected_list_image=$(find -L "$dir" -maxdepth 1 -type f -samefile "$current_image" -print -quit 2>/dev/null)
|
||||||
[[ -n $selected_list_image ]] && break
|
[[ -n $selected_list_image ]] && break
|
||||||
fi
|
fi
|
||||||
@@ -125,16 +133,9 @@ fi
|
|||||||
rows_payload=${rows//$'\t'/$'\f'}
|
rows_payload=${rows//$'\t'/$'\f'}
|
||||||
rows_payload=${rows_payload//$'\n'/$'\v'}
|
rows_payload=${rows_payload//$'\n'/$'\v'}
|
||||||
|
|
||||||
selector_pid=$(quickshell list -p "$selector_qml" --json 2>/dev/null | jq -r '.[0].pid // empty')
|
if [[ -S $socket_path && $selector_qml -nt $socket_path ]]; then
|
||||||
if [[ -n $selector_pid ]]; then
|
quickshell kill -p "$selector_qml" >/dev/null 2>&1 || true
|
||||||
selector_age=$(ps -o etimes= -p "$selector_pid" 2>/dev/null | tr -d ' ')
|
rm -f "$socket_path"
|
||||||
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
|
fi
|
||||||
|
|
||||||
if [[ ! -S $socket_path ]]; then
|
if [[ ! -S $socket_path ]]; then
|
||||||
@@ -163,5 +164,11 @@ while [[ ! -e $done_file ]]; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
if [[ -s $selection_file ]]; then
|
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
|
fi
|
||||||
|
|||||||
+41
-24
@@ -5,9 +5,11 @@
|
|||||||
OMARCHY_PATH=${OMARCHY_PATH:-$HOME/.local/share/omarchy}
|
OMARCHY_PATH=${OMARCHY_PATH:-$HOME/.local/share/omarchy}
|
||||||
USER_THEMES_PATH="$HOME/.config/omarchy/themes"
|
USER_THEMES_PATH="$HOME/.config/omarchy/themes"
|
||||||
OMARCHY_THEMES_PATH="$OMARCHY_PATH/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)
|
mkdir -p "$preview_dir"
|
||||||
trap 'rm -rf "$preview_dir"' EXIT
|
|
||||||
|
|
||||||
find_preview() {
|
find_preview() {
|
||||||
local theme_path="$1"
|
local theme_path="$1"
|
||||||
@@ -32,31 +34,46 @@ add_theme_preview() {
|
|||||||
ln -s "$preview" "$preview_dir/$theme_name.$extension"
|
ln -s "$preview" "$preview_dir/$theme_name.$extension"
|
||||||
}
|
}
|
||||||
|
|
||||||
while IFS= read -r theme_path; do
|
theme_signature=""
|
||||||
theme_name=${theme_path##*/}
|
for theme_dir in "$USER_THEMES_PATH" "$OMARCHY_THEMES_PATH"; do
|
||||||
preview=$(find_preview "$theme_path")
|
if [[ -d $theme_dir ]]; then
|
||||||
|
theme_signature+="$theme_dir:$(stat -Lc '%Y' "$theme_dir")"$'\n'
|
||||||
if [[ -z $preview ]]; then
|
|
||||||
preview=$(find_preview "$OMARCHY_THEMES_PATH/$theme_name")
|
|
||||||
fi
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
add_theme_preview "$theme_name" "$preview"
|
if [[ ! -f $signature_file ]] || ! cmp -s "$signature_file" <(printf '%s' "$theme_signature"); then
|
||||||
done < <(find -L "$USER_THEMES_PATH" -mindepth 1 -maxdepth 1 \( -type d -o -type l \) -print 2>/dev/null | sort)
|
rm -rf "$preview_dir"
|
||||||
|
mkdir -p "$preview_dir"
|
||||||
|
|
||||||
while IFS= read -r theme_path; do
|
while IFS= read -r theme_path; do
|
||||||
theme_name=${theme_path##*/}
|
theme_name=${theme_path##*/}
|
||||||
preview=$(find_preview "$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)
|
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)
|
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=""
|
||||||
selected_preview=$(omarchy-menu-images \
|
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" \
|
--selected "$selected_preview" \
|
||||||
"$preview_dir")
|
"$preview_dir"
|
||||||
|
|
||||||
if [[ -n $selected_preview ]]; then
|
|
||||||
selected_theme=${selected_preview##*/}
|
|
||||||
selected_theme=${selected_theme%.*}
|
|
||||||
printf '%s\n' "$selected_theme"
|
|
||||||
fi
|
|
||||||
|
|||||||
Reference in New Issue
Block a user