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
7 changes: 4 additions & 3 deletions bin/omarchy-capture-screenrecording-with-webcam
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ if (( ${#devices[@]} == 0 )); then
fi

if (( ${#devices[@]} == 1 )); then
device="${devices[0]%%[[:space:]]*}"
index=0
else
selection=$(omarchy-menu-select "Select Webcam" "${devices[@]}" -- --width 520 --maxheight 520) || exit 1
device="${selection%%[[:space:]]*}"
index=$(omarchy-menu-select --print-index "Select Webcam" "${devices[@]}" -- --width 520 --maxheight 520) || exit 1
fi

device="${devices[index]%%[[:space:]]*}"

exec omarchy-capture-screenrecording \
--with-desktop-audio --with-microphone-audio \
--with-webcam --webcam-device="$device"
6 changes: 3 additions & 3 deletions bin/omarchy-games-retro-install
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ if (( $# == 0 )); then
exit 1
fi

core=$(omarchy-menu-select "RetroArch core" "${cores[@]}") || exit 0
[[ -n $core ]] || exit 0
core="${core##*(}"
core_index=$(omarchy-menu-select --print-index "RetroArch core" "${cores[@]}") || exit 0
[[ -n $core_index ]] || exit 0
core="${cores[core_index]##*(}"
core="${core%)}"

game_path=$(omarchy-menu-file "Retro game" "$HOME/Games/roms" "7z bin ccd chd cue dmg elf fds gb gba gbc iso lha m3u md n64 nds nes pbp sfc smc swc zip z64") || exit 0
Expand Down
86 changes: 81 additions & 5 deletions bin/omarchy-menu-select
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,40 @@
# omarchy:summary=Pick one option from a menu
# omarchy:group=menu
# omarchy:name=select
# omarchy:args=prompt [option...] [-- menu args...]
# omarchy:args=[--print-index] prompt [option...] [-- menu args...]
# omarchy:examples=omarchy menu select Format jpg png|omarchy-menu-select Resolution 4k 1080p 720p -- --width 400

# An option may lead with an icon, as "<glyph><TAB><label>", and may trail a
# subtext shown under the label, as "<glyph><TAB><label><TAB><subtext>". The
# menu shows the glyph but never returns it. A plain option returns the label
# alone; an option with a subtext returns "<label><TAB><subtext>", so callers
# with same-named rows get the subtext back as the stable key.
# with same-named rows get the subtext back as the stable key. An empty subtext
# is no subtext: the tab that would introduce it does not come back either.
#
# --print-index returns where the choice sits in the list that was passed in
# instead of the text of it, so a caller looks the answer up in its own data
# rather than parsing what was on screen. It must come before the prompt, since
# every argument after that is an option.

set -euo pipefail

print_index=false

while (( $# > 0 )) && [[ $1 == --* ]]; do
case "$1" in
--print-index)
print_index=true
;;
*)
echo "omarchy-menu-select: unknown option $1" >&2
exit 1
;;
esac
shift
done

if (( $# < 1 )); then
echo "Usage: omarchy-menu-select <prompt> [option...] [-- menu args...]" >&2
echo "Usage: omarchy-menu-select [--print-index] <prompt> [option...] [-- menu args...]" >&2
exit 1
fi

Expand Down Expand Up @@ -63,10 +84,48 @@ if (( ${#options[@]} == 0 )) && [[ ! -t 0 ]]; then
fi

if (( ${#options[@]} == 0 )); then
echo "Usage: omarchy-menu-select <prompt> [option...] [-- menu args...]" >&2
echo "Usage: omarchy-menu-select [--print-index] <prompt> [option...] [-- menu args...]" >&2
exit 1
fi

# What comes back is the option with everything up to its first tab removed,
# since the menu drops a leading glyph and keeps a subtext. Mapping that back to
# a position is only sound while no two options come back as the same text;
# choosing between them would act on a row the user did not pick.
declare -A option_indexes=()

if [[ $print_index == true ]]; then
for index in "${!options[@]}"; do
returned="${options[index]#*$'\t'}"
label="${returned%%$'\t'*}"

# Menu.qml:768 joins a subtext to its label only when the subtext is not
# empty, so an option that ends at its subtext tab comes back without that
# tab. Keeping it here would key the row on text the menu never sends, which
# costs either the pick of that row or, worse, the row after it.
if [[ $returned == "$label"$'\t' ]]; then
returned="$label"
fi

# An option that comes back as nothing would put a blank row on screen, and
# bash rejects an empty associative-array subscript, so the loop below would
# abort with an error of its own. Refuse the list here instead and name the
# problem: a caller that pipes in a blank line wants to hear about it rather
# than be handed a row that displays nothing.
if [[ -z $returned ]]; then
echo "omarchy-menu-select: --print-index needs options that come back as text" >&2
exit 1
fi

if [[ -n ${option_indexes[$returned]:-} ]]; then
echo "omarchy-menu-select: --print-index needs options that come back as different text" >&2
exit 1
fi

option_indexes["$returned"]="$index"
done
fi

selection_file=$(mktemp)
done_file=$(mktemp)
rm -f "$done_file"
Expand All @@ -93,7 +152,24 @@ while [[ ! -e $done_file ]]; do
done

if [[ -s $selection_file ]]; then
cat "$selection_file"
if [[ $print_index == true ]]; then
# Menu.qml:132 writes the selection with printf, so exactly one trailing
# newline is the menu's and has to come back off. Reading it with $( ) would
# take every trailing newline instead, which leaves an option that ends in
# one keyed under text that can never arrive, and hands its answer to
# whichever option matches the shortened text.
IFS= read -r -d '' selection <"$selection_file" || true
selection="${selection%$'\n'}"

if [[ -z ${option_indexes[$selection]:-} ]]; then
echo "omarchy-menu-select: the selection matches no option" >&2
exit 1
fi

printf '%s\n' "${option_indexes[$selection]}"
else
cat "$selection_file"
fi
else
exit 1
fi
2 changes: 2 additions & 0 deletions docs/menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,5 @@ renders under the label, filters with it, and comes back as
`label\tsubtext` so callers with same-named rows get a stable key. This is
how the pickers behind menu actions (`omarchy-menu-plugin`,
`omarchy-menu-timezone`, ...) present lists without owning any UI.

`omarchy-menu-select --print-index` answers with the option's position in the list it was handed instead of the text of the row, so a caller that carries an id looks it up in its own data rather than parsing the display text back apart. Because the position is only recoverable while every option comes back as different, non-empty text, the flag refuses a list where two options would answer alike or where one would answer as nothing, and it says so before the menu opens.
181 changes: 181 additions & 0 deletions test/shell.d/menu-select-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#!/bin/bash

set -euo pipefail

source "$(dirname "$0")/base-test.sh"

require_command perl

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

stub_bin="$tmp_dir/bin"
mkdir -p "$stub_bin"

# Stand in for the menu, answering the way Menu.qml does: a leading glyph is
# dropped, the label comes back with any subtext joined to it by a tab, and an
# empty subtext is no subtext at all. That transformation is what a caller
# asking for an index needs inverted. The answer is written the way
# Menu.qml:132 writes it, with a trailing newline, and a dismissed menu writes
# nothing at all rather than an empty line.
cat >"$stub_bin/omarchy-shell" <<'SH'
#!/bin/bash

printf '%s\n' "$3" >>"$OMARCHY_TEST_SUMMONS"

perl -MJSON::PP=decode_json -e '
my $payload = decode_json($ARGV[0]);
my $answer = $ENV{OMARCHY_TEST_SELECTION};

unless (defined $answer) {
my @parts = split /\t/, $payload->{options}[$ENV{OMARCHY_TEST_PICK}], -1;
shift @parts if @parts > 1;
my $label = shift @parts;
my $detail = join "\t", @parts;
$answer = length($detail) ? "$label\t$detail" : $label;
}

open my $selection, ">", $payload->{selectionFile} or die $!;
print $selection "$answer\n" if length $answer;
close $selection;

open my $done, ">", $payload->{doneFile} or die $!;
close $done;
' "$4"
SH

chmod +x "$stub_bin"/*

export PATH="$stub_bin:$ROOT/bin:$PATH"
export OMARCHY_TEST_SUMMONS="$tmp_dir/summons"
: >"$OMARCHY_TEST_SUMMONS"

errors="$tmp_dir/errors"

# Pick the option at $1, then hand everything else to the menu.
picking() {
local pick="$1"
shift

OMARCHY_TEST_PICK="$pick" omarchy-menu-select "$@" 2>"$errors"
}

[[ $(picking 2 --print-index "Format" jpg png webp) == "2" ]] ||
fail "an index names the option that was picked" "$(<"$errors")"
pass "an index names the option that was picked"

[[ $(printf '%s\n' one two three | picking 1 --print-index "Pick") == "1" ]] ||
fail "options read from stdin are indexed too" "$(<"$errors")"
pass "options read from stdin are indexed too"

# The glyph is the reason parsing the answer is fragile: what comes back is
# never what was passed in.
[[ $(picking 1 --print-index "Network" $'󰤨\tWi-Fi' $'󰈀\tEthernet') == "1" ]] ||
fail "an option with a glyph is still found by index" "$(<"$errors")"
pass "an option with a glyph is still found by index"

# Two rows that read alike are what the subtext is for, and an index tells them
# apart without the caller knowing the subtext is the key.
same_label=($'󰚩\tAgent\tomp' $'󰚩\tAgent\tclaude')
[[ $(picking 1 --print-index "Agent" "${same_label[@]}") == "1" ]] ||
fail "same-labelled options are told apart by index" "$(<"$errors")"
pass "same-labelled options are told apart by index"

# An empty subtext is no subtext: Menu.qml:768 drops the tab that would have
# introduced it, so an option ending at that tab has to be keyed without it or
# its own row becomes unreachable.
[[ $(picking 0 --print-index "Pick" $'󰚩\tAlpha\t' Beta) == "0" ]] ||
fail "an option whose subtext is empty is found by its own index" "$(<"$errors")"
pass "an option whose subtext is empty is found by its own index"

# The newline Menu.qml:132 adds is the menu's, and only that one comes off. Take
# every trailing newline instead and this row is keyed under text that can never
# arrive, while the answer for it goes to the row below.
[[ $(picking 0 --print-index "Pick" $'Alpha\n' Alpha) == "0" ]] ||
fail "an option that ends in a newline is found by its own index" "$(<"$errors")"
pass "an option that ends in a newline is found by its own index"

[[ $(picking 1 "Format" jpg png webp) == "png" ]] ||
fail "without the flag the selection still comes back as text" "$(<"$errors")"
[[ $(picking 1 "Agent" "${same_label[@]}") == $'Agent\tclaude' ]] ||
fail "without the flag a subtext still comes back joined to its label" "$(<"$errors")"
pass "without the flag the selection still comes back as text"

# An answer that two options could have produced is not invertible. Saying so
# before the menu opens beats acting on whichever row was reached first.
: >"$OMARCHY_TEST_SUMMONS"
if picking 0 --print-index "Agent" $'󰚩\tAgent' $'󰊤\tAgent'; then
fail "options that come back as the same text are refused"
fi
grep -q 'come back as different text' "$errors" ||
fail "options that come back as the same text are refused" "$(<"$errors")"
[[ ! -s $OMARCHY_TEST_SUMMONS ]] ||
fail "an unanswerable list is refused before the menu opens" "$(<"$OMARCHY_TEST_SUMMONS")"
pass "options that come back as the same text are refused"

# Dropping that tab is what makes these two collide: both rows read as "Alpha"
# on screen, so no answer can name one of them. Refusing beats returning the
# row that was not picked.
: >"$OMARCHY_TEST_SUMMONS"
if picking 0 --print-index "Pick" $'󰚩\tAlpha\t' Alpha; then
fail "an empty subtext colliding with a plain label is refused"
fi
grep -q 'come back as different text' "$errors" ||
fail "an empty subtext colliding with a plain label is refused" "$(<"$errors")"
[[ ! -s $OMARCHY_TEST_SUMMONS ]] ||
fail "an empty subtext colliding with a plain label is refused before the menu opens" "$(<"$OMARCHY_TEST_SUMMONS")"
pass "an empty subtext colliding with a plain label is refused"

# A row that comes back as nothing would show as a blank one, and bash cannot
# key it either. A glyph with no label is one way to write it and a blank line on
# stdin is the way it actually turns up.
: >"$OMARCHY_TEST_SUMMONS"
if picking 1 --print-index "Pick" $'󰚩\t' Beta; then
fail "an option that comes back as nothing is refused"
fi
grep -q 'needs options that come back as text' "$errors" ||
fail "an option that comes back as nothing is refused" "$(<"$errors")"
if printf '%s\n' one "" two | picking 2 --print-index "Pick"; then
fail "a blank line on stdin is refused too"
fi
grep -q 'needs options that come back as text' "$errors" ||
fail "a blank line on stdin is refused too" "$(<"$errors")"
[[ ! -s $OMARCHY_TEST_SUMMONS ]] ||
fail "an option that comes back as nothing is refused before the menu opens" "$(<"$OMARCHY_TEST_SUMMONS")"
pass "an option that comes back as nothing is refused"

if OMARCHY_TEST_SELECTION="Betamax" omarchy-menu-select --print-index "Format" jpg png 2>"$errors"; then
fail "a selection matching no option fails instead of guessing"
fi
grep -q 'matches no option' "$errors" ||
fail "a selection matching no option fails instead of guessing" "$(<"$errors")"
pass "a selection matching no option fails instead of guessing"

# Cancelling leaves the selection file empty, which is the one answer that has
# no option behind it.
if OMARCHY_TEST_SELECTION="" omarchy-menu-select --print-index "Format" jpg png 2>"$errors"; then
fail "a dismissed menu still exits non-zero" "$(<"$errors")"
fi
pass "a dismissed menu still exits non-zero"

if omarchy-menu-select --print-name "Format" jpg png 2>"$errors"; then
fail "an unknown option is refused"
fi
grep -q 'unknown option --print-name' "$errors" ||
fail "an unknown option is refused" "$(<"$errors")"
pass "an unknown option is refused"

# A prompt is still required, and the flag is not one.
if omarchy-menu-select --print-index 2>"$errors"; then
fail "the flag alone is not a prompt"
fi
grep -q 'Usage: omarchy-menu-select \[--print-index\]' "$errors" ||
fail "the flag alone is not a prompt" "$(<"$errors")"
pass "the flag alone is not a prompt"

# Callers that used to recover an id by cutting up the row they displayed.
grep -q -- '--print-index' "$ROOT/bin/omarchy-capture-screenrecording-with-webcam" ||
fail "the webcam picker keys on its own list"
grep -q -- '--print-index' "$ROOT/bin/omarchy-games-retro-install" ||
fail "the RetroArch core picker keys on its own list"
pass "the pickers that carry an id key on their own list"
5 changes: 4 additions & 1 deletion test/shell.d/screenrecording-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ cat >"$stub_bin/omarchy-menu-select" <<'SH'
#!/bin/bash

printf '%s\n' "$@" >"$OMARCHY_TEST_MENU_ARGS"
printf '%s\n' "$3"
# Stand in for picking the second webcam. Under --print-index the answer is a
# position in the list the caller passed in, not the row that was displayed.
printf '%s\n' 1
SH

cat >"$stub_bin/omarchy-capture-screenrecording" <<'SH'
Expand Down Expand Up @@ -135,6 +137,7 @@ pass "screenrecording webcam picker rejects output-only video devices"

expected_menu_args="$tmp_dir/expected-menu-args"
printf '%s\n' \
"--print-index" \
"Select Webcam" \
"/dev/video42 Built-in Webcam: Integrated Camera" \
"/dev/video2 USB Capture Card: External Camera" \
Expand Down