Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions bin/omarchy-menu-file
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,27 @@ for format in "${formats[@]}"; do
done
find_args+=(")" -printf '%T@\t%p\n')

find "${find_args[@]}" 2>/dev/null | sort -rn | cut -f2- |
omarchy-menu-select "$label" -- --width 800 --maxheight 500 "$@"
# -H follows a starting point that is itself a symlink (e.g. ~/Videos ->
# /mnt/storage/Videos) without also following symlinks found deeper in the
# walk (unlike -L). Without it find does not descend starting-point links, so
# transcode sees zero files and omarchy-menu-select fails on empty stdin.
# Cap entry count and total path bytes for ordinary valid-UTF-8 names so
# omarchy-menu-select is less likely to hit a single-argument MAX_ARG_STRLEN
# (~128KiB). LC_ALL=C makes awk length() count bytes (not UTF-8 characters).
# Encoded JSON expansion (escapes / invalid UTF-8 -> U+FFFD) is enforced in
# omarchy-menu-select itself; this budget is only a first cut at the lister.
# Temporarily drop pipefail so SIGPIPE from the consumer does not fail the pipeline.
set +o pipefail
find -H "${find_args[@]}" 2>/dev/null | sort -rn | cut -f2- | LC_ALL=C awk '
BEGIN { max_n = 500; max_bytes = 80000; n = 0; bytes = 0 }
{
len = length($0) + 1
if (n >= max_n || bytes + len > max_bytes) exit
print
n++
bytes += len
}
' | omarchy-menu-select "$label" -- --width 800 --maxheight 500 "$@"
status=${PIPESTATUS[4]:-$?}
set -o pipefail
exit "$status"
11 changes: 11 additions & 0 deletions bin/omarchy-menu-select
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ trap 'rm -f "$selection_file" "$done_file"' EXIT

options_json=$(perl -MEncode=decode -MJSON::PP=encode_json -e 'print encode_json([map { decode("UTF-8", $_) } @ARGV])' "${options[@]}")
selected_json=$(perl -MEncode=decode -MJSON::PP=encode_json -e 'print encode_json([map { decode("UTF-8", $_) } @ARGV])' "${selected[@]}")

# Bound the exact encoded options JSON before building the shell/IPC payload.
# Lister path-byte budgets cannot see escapes / invalid-UTF-8 expansion here.
# ${#var} is characters under UTF-8 LC_CTYPE; count bytes in C locale.
MAX_MENU_OPTIONS_JSON_BYTES=120000
options_json_bytes=$(LC_ALL=C; printf '%s' "${#options_json}")
if (( options_json_bytes > MAX_MENU_OPTIONS_JSON_BYTES )); then
echo "omarchy-menu-select: options payload too large (${options_json_bytes} bytes; max ${MAX_MENU_OPTIONS_JSON_BYTES})" >&2
exit 1
fi

payload=$(perl -MEncode=decode -MJSON::PP=encode_json,decode_json -e '
my $payload = {
mode => "select",
Expand Down
32 changes: 32 additions & 0 deletions test/shell.d/menu-file-symlink-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

real="$tmp/real-videos"
link="$tmp/Videos"
nested_real="$tmp/elsewhere"
mkdir -p "$real" "$nested_real"
printf 'media' >"$real/clip.webm"
printf 'nested' >"$nested_real/nested.webm"
ln -s "$nested_real" "$real/nested-link"
ln -s "$real" "$link"

stub="$tmp/bin"
mkdir -p "$stub"
cat >"$stub/omarchy-menu-select" <<'STUB'
#!/bin/bash
# Consume stdin and print paths so the test can assert find -H starting-point behavior.
cat
STUB
chmod +x "$stub/omarchy-menu-select"

output=$(PATH="$stub:$PATH" "$ROOT/bin/omarchy-menu-file" "Select media" "$link" "webm")
[[ $output == *"$link/clip.webm"* ]] || fail "menu-file follows a symlink starting point" "$output"
# find -H must not walk nested symlinks inside the root (would list under -L).
[[ $output != *nested.webm* ]] || fail "menu-file must not follow nested symlinks inside roots" "$output"
pass "menu-file follows a symlink starting point without nested walks"
57 changes: 57 additions & 0 deletions test/shell.d/menu-select-options-byte-budget-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

stub="$tmp/bin"
mkdir -p "$stub"

cat >"$stub/omarchy-shell" <<'STUB'
#!/bin/bash
echo "omarchy-shell should not be reached for oversized options" >&2
exit 99
STUB
chmod +x "$stub/omarchy-shell"

# 80 options x ~1400 invalid bytes: ~112k characters after U+FFFD encode but
# ~337k bytes — fails under a character-length cap, passes with byte counting.
opts=()
i=1
while [[ $i -le 80 ]]; do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Numeric checks break repository style

The new loop uses [[ $i -le 80 ]], and both exit-code checks use [[ $rc -eq 1 ]]. The repository requires (( )) for numeric comparisons. Use while (( i <= 80 )); do and (( rc == 1 )) for those checks. This requirement must be satisfied before merging.

File Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

name=$(python3 -c 'import sys; sys.stdout.buffer.write(b"n%03d-"%int(sys.argv[1])+b"\xff"*1400)' "$i")
opts+=("$name")
i=$((i + 1))
done

set +e
err=$(
LC_ALL=C.UTF-8 PATH="$stub:$PATH" \
"$ROOT/bin/omarchy-menu-select" "Pick" "${opts[@]}" 2>&1
)
rc=$?
set -e

[[ $rc -eq 1 ]] || fail "menu-select rejects oversized options under UTF-8 (exit $rc)" "$err"
[[ $err == *"options payload too large"* ]] || fail "menu-select oversized error message" "$err"
pass "menu-select rejects oversized encoded options by byte length under UTF-8"

cat >"$stub/omarchy-shell" <<'STUB'
#!/bin/bash
payload="$4"
done=$(printf '%s' "$payload" | perl -MJSON::PP=decode_json -e 'print decode_json(do { local $/; <STDIN> })->{doneFile}')
: >"$done"
exit 0
STUB
chmod +x "$stub/omarchy-shell"

set +e
out=$(LC_ALL=C.UTF-8 PATH="$stub:$PATH" "$ROOT/bin/omarchy-menu-select" "Pick" "a" "b" 2>&1)
rc=$?
set -e
[[ $out != *"options payload too large"* ]] || fail "small menu should not hit byte budget" "$out"
[[ $rc -eq 1 ]] || fail "small menu empty selection exits 1" "status=$rc out=$out"
pass "menu-select small options pass byte budget under UTF-8"
Loading