diff --git a/bin/omarchy-menu-input b/bin/omarchy-menu-input index b61f88e332da..876b0a82ef1c 100755 --- a/bin/omarchy-menu-input +++ b/bin/omarchy-menu-input @@ -34,16 +34,20 @@ done_file=$(mktemp) rm -f "$done_file" trap 'rm -f "$selection_file" "$done_file"' EXIT -payload=$(perl -MEncode=decode -MJSON::PP=encode_json -e ' - my $payload = { - mode => "input", - prompt => decode("UTF-8", $ARGV[0]), - selectionFile => decode("UTF-8", $ARGV[1]), - doneFile => decode("UTF-8", $ARGV[2]) - }; - $payload->{width} = int($ARGV[3]) if length($ARGV[3] // ""); - print encode_json($payload) -' "$prompt" "$selection_file" "$done_file" "$menu_width") +payload=$(jq -nc \ + --arg prompt "$prompt" \ + --arg selectionFile "$selection_file" \ + --arg doneFile "$done_file" \ + --arg width "$menu_width" \ + ' + { + mode: "input", + prompt: $prompt, + selectionFile: $selectionFile, + doneFile: $doneFile + } + + (if ($width | length) > 0 then { width: ($width | tonumber? // 0 | trunc) } else {} end) + ') omarchy-shell shell summon omarchy.menu "$payload" >/dev/null diff --git a/bin/omarchy-menu-select b/bin/omarchy-menu-select index 18f3ed140b79..561785f0bafe 100755 --- a/bin/omarchy-menu-select +++ b/bin/omarchy-menu-select @@ -109,24 +109,34 @@ done_file=$(mktemp) rm -f "$done_file" 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[@]}") -payload=$(perl -MEncode=decode -MJSON::PP=encode_json,decode_json -e ' - my $payload = { - mode => "select", - prompt => decode("UTF-8", $ARGV[0]), - options => decode_json($ARGV[1]), - multiple => $ARGV[6] ? JSON::PP::true : JSON::PP::false, - selected => decode_json($ARGV[7]), - onChange => decode_json($ARGV[8]), - changeKey => $ARGV[9], - selectionFile => decode("UTF-8", $ARGV[2]), - doneFile => decode("UTF-8", $ARGV[3]) - }; - $payload->{width} = int($ARGV[4]) if length($ARGV[4] // ""); - $payload->{maxHeight} = int($ARGV[5]) if length($ARGV[5] // ""); - print encode_json($payload) -' "$prompt" "$options_json" "$selection_file" "$done_file" "$menu_width" "$menu_maxheight" "$multiple" "$selected_json" "$on_change" "$change_key") +payload=$( + if (( ${#selected[@]} > 0 )); then + printf '%s\0' "${selected[@]}" + fi | jq -Rsc \ + --arg prompt "$prompt" \ + --arg selectionFile "$selection_file" \ + --arg doneFile "$done_file" \ + --arg width "$menu_width" \ + --arg maxHeight "$menu_maxheight" \ + --argjson multiple "$( (( multiple )) && echo true || echo false )" \ + --argjson onChange "$on_change" \ + --arg changeKey "$change_key" \ + ' + { + mode: "select", + prompt: $prompt, + options: $ARGS.positional, + multiple: $multiple, + selected: (if length == 0 then [] else split("\u0000")[:-1] end), + onChange: $onChange, + changeKey: $changeKey, + selectionFile: $selectionFile, + doneFile: $doneFile + } + + (if ($width | length) > 0 then { width: ($width | tonumber? // 0 | trunc) } else {} end) + + (if ($maxHeight | length) > 0 then { maxHeight: ($maxHeight | tonumber? // 0 | trunc) } else {} end) + ' --args -- "${options[@]}" +) omarchy-shell shell summon omarchy.menu "$payload" >/dev/null || exit 2 diff --git a/test/shell.d/menu-test.sh b/test/shell.d/menu-test.sh index d91b66ff513f..7082d1803c53 100644 --- a/test/shell.d/menu-test.sh +++ b/test/shell.d/menu-test.sh @@ -443,3 +443,60 @@ const missingGlyphs = fontGlyphRows .map(({ id, code }) => `${id} U+${code.toString(16)}`) assertDeepEqual(missingGlyphs, [], 'every menu glyph drawn in the Omarchy font is one the font carries') JS + +# Verify omarchy-menu-select and omarchy-menu-input serialize payloads faithfully +# (including UTF-8, tabs, dash-prefixed options, multi-select, and dimensions). +select_tmp=$(mktemp -d) +trap 'rm -rf "$select_tmp"' EXIT +mkdir -p "$select_tmp/bin" + +cat >"$select_tmp/bin/omarchy-shell" <<'SH' +#!/bin/bash +payload="$4" +printf '%s\n' "$payload" >"$PAYLOAD_OUT" +sel_file=$(jq -r '.selectionFile' <<<"$payload") +done_file=$(jq -r '.doneFile' <<<"$payload") +printf '%s\n' "$FAKE_ANSWER" >"$sel_file" +: >"$done_file" +SH +chmod +x "$select_tmp/bin/omarchy-shell" + +select_answer=$(PATH="$select_tmp/bin:$PATH" PAYLOAD_OUT="$select_tmp/select.json" FAKE_ANSWER="-dash" \ + "$ROOT/bin/omarchy-menu-select" "Pick (æøå)" "-dash" $'✓\tSecond\tsub' -- \ + --multiple --selected "-dash" --selected $'Second\tsub' \ + --on-change '["apply","--set"]' --change-key "k1" --width 640 --maxheight 480) +[[ $select_answer == "-dash" ]] || fail "omarchy-menu-select returns the selected value" "$select_answer" +jq -e ' + .mode == "select" + and .prompt == "Pick (æøå)" + and .options == ["-dash", "✓\tSecond\tsub"] + and .multiple == true + and .selected == ["-dash", "Second\tsub"] + and .onChange == ["apply", "--set"] + and .changeKey == "k1" + and .width == 640 + and .maxHeight == 480 +' "$select_tmp/select.json" >/dev/null || fail "omarchy-menu-select builds the expected JSON payload" "$(cat "$select_tmp/select.json")" +pass "omarchy-menu-select builds the expected JSON payload" + +# Empty selected list and omitted dimensions stay clean (empty array, no null width/maxHeight). +PATH="$select_tmp/bin:$PATH" PAYLOAD_OUT="$select_tmp/select-min.json" FAKE_ANSWER="One" \ + "$ROOT/bin/omarchy-menu-select" "Simple" "One" >/dev/null +jq -e ' + .mode == "select" + and .multiple == false + and .selected == [] + and (has("width") | not) + and (has("maxHeight") | not) +' "$select_tmp/select-min.json" >/dev/null || fail "omarchy-menu-select omits unset dimensions and keeps selected empty" "$(cat "$select_tmp/select-min.json")" +pass "omarchy-menu-select omits unset dimensions and keeps selected empty" + +input_answer=$(PATH="$select_tmp/bin:$PATH" PAYLOAD_OUT="$select_tmp/input.json" FAKE_ANSWER="15" \ + "$ROOT/bin/omarchy-menu-input" "Reminder (æøå)" --width 400) +[[ $input_answer == "15" ]] || fail "omarchy-menu-input returns the entered value" "$input_answer" +jq -e ' + .mode == "input" + and .prompt == "Reminder (æøå)" + and .width == 400 +' "$select_tmp/input.json" >/dev/null || fail "omarchy-menu-input builds the expected JSON payload" "$(cat "$select_tmp/input.json")" +pass "omarchy-menu-input builds the expected JSON payload"