Skip to content

Let the menu selector return an option's index - #12363

Open
linyiru wants to merge 1 commit into
omacom:quattrofrom
linyiru:menu-select-print-index
Open

linyiru wants to merge 1 commit into
omacom:quattrofrom
linyiru:menu-select-print-index

Conversation

@linyiru

@linyiru linyiru commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

omarchy-menu-select answers with the text of the row the menu displayed. A caller that needs an id rather than a label therefore has to cut the id back out of that text, which makes the display string a protocol.

Two callers do exactly that today:

# omarchy-games-retro-install: the core file name, out of "Name (core_libretro.so)"
core="${core##*(}"
core="${core%)}"

# omarchy-capture-screenrecording-with-webcam: the device path, out of "/dev/video0  Some Camera"
device="${selection%%[[:space:]]*}"

Each breaks the first time its row is written any other way: a core label with a second parenthesis, a device label with the name first. Neither is a defect today, and neither changes behaviour in this PR. The point is that the fragile coupling goes away.

--print-index answers with the position of the choice in the list the caller passed in, so the caller looks the id up in its own array:

index=$(omarchy-menu-select --print-index "Select Webcam" "${devices[@]}" -- --width 520 --maxheight 520) || exit 1
device="${devices[index]%%[[:space:]]*}"

The string surgery is still there, but now it runs on a line the caller built rather than on a string that came back from the UI, which is free to gain a glyph, a subtext, or a translation later.

No QML change. The inversion runs entirely in omarchy-menu-select. What the menu returns is the option with everything up to its first tab removed, which is shell/plugins/menu/Menu.qml:568-571 (split("\t"), shift the glyph when there is more than one field, keep the rest) rejoined at :768. That is ${option#*$'\t'} in bash, with two details the join adds: :768 omits the tab when the subtext is empty, and :132 writes the answer through printf '%s\n'. So the key drops that trailing tab, and the read takes off exactly one trailing newline rather than every one. With those, bash maps an answer back to a position without the menu knowing anything new. That also keeps this PR clear of #12338, which is open on the same file.

It refuses to guess. Mapping an answer back to a position is only sound while every option comes back as different, non-empty text. --print-index builds that map before summoning the menu and exits 1 with a message if two options would collide, or if one would come back as nothing, so an ambiguous list fails without ever showing the user a menu whose answer would be acted on wrongly. If the selection matches no option it also fails rather than print a position.

That holds only while the key matches Menu.qml's returned text byte for byte, which is what the assertions pin and what a change to that file could break. @llstrk's review found two shapes where it did not hold: an option whose subtext is empty, and one ending in a newline. Each returned a wrong row silently. Both are fixed and both have an assertion.

The flag has to come before the prompt, since every argument after the prompt is an option. omarchy-menu-images --print-name already has this shape, so the pattern is not new. docs/menu.md gains a paragraph describing the flag, next to the select protocol it inverts.

test/shell.d/menu-select-test.sh is new: 15 assertions, covering plain options, options fed on stdin, glyph-prefixed options, same-label-different-subtext rows, an option whose subtext is empty and one ending in a newline, the shapes that are refused, the unchanged answer when the flag is absent, dismissal, a selection matching nothing, and the two callers actually passing the flag. The stub stands in for omarchy-shell and replays the QML transformation in perl, including the trailing newline Menu.qml:132 writes and the empty file a cancel leaves, so what the test pins is the returned-text contract rather than this implementation of it.

./test/cli and ./test/shell were run at this commit and at b421b1b4 on bash 5.3.15 (Arch), same box, each in its own worktree. Both sides end with the same 8 failing assertions, and a per-file comparison across both suites differs in exactly one line, menu-select-test.sh ok=15 notok=0, present here and absent at the base because the file is new. Neither suite passes on a plain checkout, so that is a differential rather than a clean run. The existing test/shell.d/screenrecording-test.sh passes with its stub updated to answer with a position. Reverting any one of the three changes to bin/omarchy-menu-select stops the new file at that change's own assertion, so the assertions do bite.

Follow-up to #12345 §7, sent on its own as promised there. It is the mechanism the rest of that issue's §7 items depend on, which is why it comes first even though nothing user-visible moves.

🤖 Generated with Claude Code

@llstrk

llstrk commented Sep 25, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Verified: --print-index returns the correct position for both converted pickers and for every option shape the new test covers. Without the flag, omarchy-menu-select output is byte-identical to base.

  • test/shell.d/menu-select-test.sh passes (11 assertions), and screenrecording-test.sh passes at head and base. Reverting only bin/omarchy-menu-select fails the new test at its first assertion.
  • The webcam and RetroArch pickers resolve the same device and core as base for every pick, including a core label with a second parenthesis, the single-webcam path and cancel.
  • Lists whose rows come back as the same text are refused before the menu opens. Keys that are special to bash (@, *, a]b, $(...) and similar) index correctly and are never evaluated.

One option shape does not invert correctly, so the "never a wrong row" guarantee does not hold in general. It does not affect the two callers converted here.

An empty trailing subtext produces a wrong index or a false "matches no option"

bin/omarchy-menu-select keys each option as ${option#*$'\t'}, which keeps a trailing tab. Menu.qml:768 returns picked.label alone when detail is empty, so an option shaped <glyph>\t<label>\t comes back without that tab. The test file's own stub already applies this rule (length($detail) ? "$label\t$detail" : $label), but no assertion uses it.

Synthetic options (G and H stand in for glyphs) through the PR's own stub and picking helper:

Options Pick Result at head
$'G\tAlpha\t', Beta 0 exit 1, "the selection matches no option"
$'G\tAlpha\t', Alpha 0 prints 1 (the row not picked)
$'G\tAlpha\t', $'H\tAlpha' 0 prints 1; the list is not refused, though both rows show as "Alpha"

Impact: Neither caller converted here produces tabs, so nothing changes for users in this PR. The glyph/label/subtext shape is what later callers would pass (for example, omarchy-menu-plugin builds icon\tname\tid rows). There, a valid pick becomes a silent no-op through the caller's || exit, or two same-label rows resolve to the wrong one.

Suggested change: After dropping the glyph, mirror line 768 by removing a trailing tab when it is the only one left:

returned="${options[index]#*$'\t'}"
label="${returned%%$'\t'*}"
if [[ $returned == "$label"$'\t' ]]; then
  returned="$label"
fi

With this change plus the key prefix suggested below, all 11 existing assertions still pass, the picks above return the correct index, and the two ambiguous lists are refused before the menu opens. One assertion such as picking 0 --print-index "Pick" $'󰚩\tAlpha\t' Beta expecting 0 would cover it.

Related edge case (trailing newline): an option argument that ends in a newline ($'A\n' next to A) also maps to the wrong row: picking the first returns 1, because $(cat "$selection_file") strips the option's own newline along with the one the menu appends. This was shown against the source-derived menu model only. Reading the file with IFS= read -r -d '' and removing exactly one trailing newline would keep the answer exact.

An empty returned text aborts with bash's own error

Under --print-index, an option whose returned text is empty (an empty argument, a blank stdin line, or $'󰚩\t') makes the script exit at line 100 with option_indexes: bad array subscript, before the menu opens. Bash associative arrays reject an empty key.

Impact: The failure is loud, not a wrong row, but one stray blank line in piped input rejects the whole list with an internal error.

Suggested change: Prefix the keys (for example option_indexes["=$returned"], with the same prefix at the two lookups), or reject empty returned text with the script's own message.

Documentation note: docs/menu.md describes the select protocol but not --print-index. A sentence there would help later callers find the flag.


Review information

Test scope: Command-only runs in an isolated sandbox on GNU bash 5.3.15. The menu was stood in for by the PR's own test stub and by a model of Menu.qml's returned-text logic derived line by line from source. The real Quickshell menu, live webcams and installed RetroArch cores were not exercised. The trailing-newline case was shown against the source-derived model only.

AI process: Opus 5.5 Medium coordination and synthesis, independent Opus 5.5 Xhigh and GPT 6 Sol Xhigh technical assessments, Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

omarchy-menu-select answers with the text of the row it displayed, so a
caller that needs an id cuts it back out of that text. The webcam picker
and the RetroArch core picker both do, and each one breaks as soon as the
label it parses is written any other way.

--print-index answers with the position of the choice in the list the
caller passed in, leaving the id to be looked up in the caller's own
data.

The key it maps has to be exactly what the menu sends back, which means
dropping the trailing tab the menu drops along with an empty subtext, and
taking off the one trailing newline the menu adds rather than every one of
them. It refuses a list whose options cannot be told apart by what comes
back, or one holding an option that comes back as nothing, which would be
a blank row and is not a key bash will take. It fails rather than guess
when the answer matches no option.
@linyiru
linyiru force-pushed the menu-select-print-index branch from 4682cb7 to 03837a4 Compare September 29, 2026 18:48
@linyiru

linyiru commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

All three taken, and the caveat in your test-scope note is gone: the newline case now reproduces through this PR's own harness rather than only against a model. The branch is rebased onto b421b1b4.

What changed

Finding Change
an empty trailing subtext gives a wrong index or a false "matches no option" your suggested hunk, verbatim: drop the trailing tab when it is the only one left. Your suggested assertion is in too, picking 0 --print-index "Pick" $'󰚩\tAlpha\t' Beta expecting 0
the same shape colliding with a plain label is not refused now refused, by the existing uniqueness check, once the key is the text the menu actually sends. Two assertions, one per collision shape in your table
an option ending in a newline maps to the wrong row IFS= read -r -d '' then strip exactly one trailing newline, as you suggested. IFS= read -r -d '' is already the idiom in this tree, at bin/omarchy-chromium-ytdlp-host:40
an empty returned text aborts with option_indexes: bad array subscript refused with the script's own message, which is the second of your two suggestions
docs/menu.md does not describe the flag a paragraph in "Select and input modes", saying what the flag answers with and what it refuses

11 ok lines to 15.

Why the newline case was invisible to the harness, and is not now. Menu.qml:132 writes the answer with printf '%s\n', and the stub's print $selection $answer did not append that newline, so the one byte the bug turns on was missing from every stub run. The stub now writes "$answer\n", and writes nothing at all when there is no answer, which is what a cancel does: Menu.qml:129-130 touches only doneFile and leaves selectionFile untouched. So "a dismissed menu still exits non-zero" now passes because the file is empty, which is the real reason, rather than because the answer was the empty string.

Why the keys are not prefixed. Prefixing would work, and it would accept these lists rather than refuse them. I refused instead because a row that comes back as nothing is a blank row on screen: exact or not, its index is not an answer anyone wants to act on, and a caller that piped in a stray blank line is better off hearing which line. That is a judgement about what the list ought to contain, not about what is recoverable, so if a maintainer would rather blank rows be accepted, the prefix is the change, plus a way for the stub to tell an empty answer apart from no answer, since OMARCHY_TEST_SELECTION="" currently means the second.

A claim in the PR body that your review disproved

The body said "every pathological option shape is a loud error, never a wrong row". Two of the shapes in your table returned a wrong row silently, so that was false when written, and stating it that broadly was the mistake rather than a slip in wording. The body now says what is actually pinned: the shapes the assertions cover, and that the guarantee rests on the key matching Menu.qml's returned text byte for byte, which is the thing a change to that file could break.

For the record, both sides of your table, run through the harness with the faithful stub, at the old head and at this commit:

head 4682cb78 03837a42
$'G\tAlpha\t', Beta, pick 0 exit 1, "matches no option" 0
$'G\tAlpha\t', Alpha, pick 0 1 refused before the menu opens
$'G\tAlpha\t', $'H\tAlpha', pick 0 1 refused before the menu opens
"", Beta, pick 1 bad array subscript refused, with the script's message
$'G\t', Beta, pick 1 bad array subscript refused, with the script's message
$'A\n', A, pick 0 1 0
jpg png webp, pick 2 2 2
$'R\tAgent\tomp' and sibling, pick 1 1 1

How this was run

./test/cli and ./test/shell at 03837a42 and at b421b1b4, same box, same afternoon, each in its own worktree. Both sides end with the same 8 failing assertions, and a per-file comparison across both suites differs in exactly one line: menu-select-test.sh ok=15 notok=0, present here and absent at the base because the file is new. Neither suite passes on a plain checkout, so that is a differential rather than a clean run.

Each of the three changes to bin/omarchy-menu-select was checked on its own: revert only the trailing-tab hunk, or only the newline hunk, or only the empty-text guard, and the run stops at that hunk's own assertion with every assertion before it still passing. The file fails fast, so that is the strongest form the harness reports.

Two things worth knowing if you re-run it. ./test/shell hangs partway unless stdin is redirected, which #13365 and #13061 already fix. And channel-sudo-boundary-test.sh ("HUP was ignored") and ssh-reconnect-test.sh ("Ctrl-C during a retry attempt stops the reconnect loop") are flaky under full-suite load, not per ref: my first base run failed both, the next passed both, and each passes five times out of five in isolation at either ref. A single full run is not a measurement for those two.

🤖 Generated with Claude Code

@jandrusk

Copy link
Copy Markdown

--print-index is a clean way to stop treating the display label as a protocol. Currently CONFLICTING against quattro — needs a rebase before review can finish. Happy to look again once it merges clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants