From 0cdea984fb922c26a0e0b4a733cd87ba4bd14f24 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 14 Oct 2026 18:35:30 +0000 Subject: [PATCH] refactor: shorten Raycast binary lookup and tidy sources - raycast: replace the hand-written candidate loop in all five scripts with path+= and whence -p. Lookup order is unchanged: FERRY_BINARY, the inherited PATH, /opt/homebrew/bin, /usr/local/bin, ~/.local/bin, then build/ next to raycast/. - src/ferry.m: make the verbose flag a local in main instead of a file-scope global. - tests/follow.m: name the fullscreen space type instead of using 4. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01P1i79aMaG5M7eTyaXZuVS9 --- raycast/ferry-to-space-1.sh | 25 +++++-------------------- raycast/ferry-to-space-2.sh | 25 +++++-------------------- raycast/ferry-to-space-3.sh | 25 +++++-------------------- raycast/ferry-to-space-4.sh | 25 +++++-------------------- raycast/ferry-to-space-5.sh | 25 +++++-------------------- src/ferry.m | 7 +++---- tests/follow.m | 6 ++++-- 7 files changed, 32 insertions(+), 106 deletions(-) diff --git a/raycast/ferry-to-space-1.sh b/raycast/ferry-to-space-1.sh index de744e9..3f5183b 100755 --- a/raycast/ferry-to-space-1.sh +++ b/raycast/ferry-to-space-1.sh @@ -10,28 +10,13 @@ set -euo pipefail space=1 -script_dir="${0:A:h}" -project_dir="${script_dir:h}" -if [[ -n "${FERRY_BINARY:-}" ]]; then - binary_path="$FERRY_BINARY" -elif binary_path="$(command -v ferry 2>/dev/null)" && [[ -x "$binary_path" ]]; then - : -else - binary_path="" - for candidate in \ - "/opt/homebrew/bin/ferry" \ - "/usr/local/bin/ferry" \ - "$HOME/.local/bin/ferry" \ - "$project_dir/build/ferry"; do - if [[ -x "$candidate" ]]; then - binary_path="$candidate" - break - fi - done -fi +# Raycast runs scripts with a minimal PATH, so also search the usual install +# locations and this checkout's build/ directory, after the inherited PATH. +path+=(/opt/homebrew/bin /usr/local/bin "$HOME/.local/bin" "${0:A:h:h}/build") +binary_path="${FERRY_BINARY:-$(whence -p ferry || true)}" -if [[ -z "$binary_path" || ! -x "$binary_path" ]]; then +if [[ ! -x "$binary_path" ]]; then print -u2 "ferry binary not found. Install it with: brew install softmaxe/tap/ferry" exit 127 fi diff --git a/raycast/ferry-to-space-2.sh b/raycast/ferry-to-space-2.sh index 303e70b..7e6db18 100755 --- a/raycast/ferry-to-space-2.sh +++ b/raycast/ferry-to-space-2.sh @@ -10,28 +10,13 @@ set -euo pipefail space=2 -script_dir="${0:A:h}" -project_dir="${script_dir:h}" -if [[ -n "${FERRY_BINARY:-}" ]]; then - binary_path="$FERRY_BINARY" -elif binary_path="$(command -v ferry 2>/dev/null)" && [[ -x "$binary_path" ]]; then - : -else - binary_path="" - for candidate in \ - "/opt/homebrew/bin/ferry" \ - "/usr/local/bin/ferry" \ - "$HOME/.local/bin/ferry" \ - "$project_dir/build/ferry"; do - if [[ -x "$candidate" ]]; then - binary_path="$candidate" - break - fi - done -fi +# Raycast runs scripts with a minimal PATH, so also search the usual install +# locations and this checkout's build/ directory, after the inherited PATH. +path+=(/opt/homebrew/bin /usr/local/bin "$HOME/.local/bin" "${0:A:h:h}/build") +binary_path="${FERRY_BINARY:-$(whence -p ferry || true)}" -if [[ -z "$binary_path" || ! -x "$binary_path" ]]; then +if [[ ! -x "$binary_path" ]]; then print -u2 "ferry binary not found. Install it with: brew install softmaxe/tap/ferry" exit 127 fi diff --git a/raycast/ferry-to-space-3.sh b/raycast/ferry-to-space-3.sh index bb90dee..7731635 100755 --- a/raycast/ferry-to-space-3.sh +++ b/raycast/ferry-to-space-3.sh @@ -10,28 +10,13 @@ set -euo pipefail space=3 -script_dir="${0:A:h}" -project_dir="${script_dir:h}" -if [[ -n "${FERRY_BINARY:-}" ]]; then - binary_path="$FERRY_BINARY" -elif binary_path="$(command -v ferry 2>/dev/null)" && [[ -x "$binary_path" ]]; then - : -else - binary_path="" - for candidate in \ - "/opt/homebrew/bin/ferry" \ - "/usr/local/bin/ferry" \ - "$HOME/.local/bin/ferry" \ - "$project_dir/build/ferry"; do - if [[ -x "$candidate" ]]; then - binary_path="$candidate" - break - fi - done -fi +# Raycast runs scripts with a minimal PATH, so also search the usual install +# locations and this checkout's build/ directory, after the inherited PATH. +path+=(/opt/homebrew/bin /usr/local/bin "$HOME/.local/bin" "${0:A:h:h}/build") +binary_path="${FERRY_BINARY:-$(whence -p ferry || true)}" -if [[ -z "$binary_path" || ! -x "$binary_path" ]]; then +if [[ ! -x "$binary_path" ]]; then print -u2 "ferry binary not found. Install it with: brew install softmaxe/tap/ferry" exit 127 fi diff --git a/raycast/ferry-to-space-4.sh b/raycast/ferry-to-space-4.sh index 9d02489..92b3b3d 100755 --- a/raycast/ferry-to-space-4.sh +++ b/raycast/ferry-to-space-4.sh @@ -10,28 +10,13 @@ set -euo pipefail space=4 -script_dir="${0:A:h}" -project_dir="${script_dir:h}" -if [[ -n "${FERRY_BINARY:-}" ]]; then - binary_path="$FERRY_BINARY" -elif binary_path="$(command -v ferry 2>/dev/null)" && [[ -x "$binary_path" ]]; then - : -else - binary_path="" - for candidate in \ - "/opt/homebrew/bin/ferry" \ - "/usr/local/bin/ferry" \ - "$HOME/.local/bin/ferry" \ - "$project_dir/build/ferry"; do - if [[ -x "$candidate" ]]; then - binary_path="$candidate" - break - fi - done -fi +# Raycast runs scripts with a minimal PATH, so also search the usual install +# locations and this checkout's build/ directory, after the inherited PATH. +path+=(/opt/homebrew/bin /usr/local/bin "$HOME/.local/bin" "${0:A:h:h}/build") +binary_path="${FERRY_BINARY:-$(whence -p ferry || true)}" -if [[ -z "$binary_path" || ! -x "$binary_path" ]]; then +if [[ ! -x "$binary_path" ]]; then print -u2 "ferry binary not found. Install it with: brew install softmaxe/tap/ferry" exit 127 fi diff --git a/raycast/ferry-to-space-5.sh b/raycast/ferry-to-space-5.sh index 1ede3d8..efc7119 100755 --- a/raycast/ferry-to-space-5.sh +++ b/raycast/ferry-to-space-5.sh @@ -10,28 +10,13 @@ set -euo pipefail space=5 -script_dir="${0:A:h}" -project_dir="${script_dir:h}" -if [[ -n "${FERRY_BINARY:-}" ]]; then - binary_path="$FERRY_BINARY" -elif binary_path="$(command -v ferry 2>/dev/null)" && [[ -x "$binary_path" ]]; then - : -else - binary_path="" - for candidate in \ - "/opt/homebrew/bin/ferry" \ - "/usr/local/bin/ferry" \ - "$HOME/.local/bin/ferry" \ - "$project_dir/build/ferry"; do - if [[ -x "$candidate" ]]; then - binary_path="$candidate" - break - fi - done -fi +# Raycast runs scripts with a minimal PATH, so also search the usual install +# locations and this checkout's build/ directory, after the inherited PATH. +path+=(/opt/homebrew/bin /usr/local/bin "$HOME/.local/bin" "${0:A:h:h}/build") +binary_path="${FERRY_BINARY:-$(whence -p ferry || true)}" -if [[ -z "$binary_path" || ! -x "$binary_path" ]]; then +if [[ ! -x "$binary_path" ]]; then print -u2 "ferry binary not found. Install it with: brew install softmaxe/tap/ferry" exit 127 fi diff --git a/src/ferry.m b/src/ferry.m index 5a5231f..a878e24 100644 --- a/src/ferry.m +++ b/src/ferry.m @@ -51,8 +51,6 @@ typedef int64_t (*bridged_operation_fn)(void *operation); -static bool g_verbose; - static void fail(int code, const char *fmt, ...) __attribute__((noreturn, format(printf, 2, 3))); static void fail(int code, const char *fmt, ...) { @@ -324,6 +322,7 @@ int main(int argc, char **argv) @autoreleasepool { uint64_t start = clock_gettime_nsec_np(CLOCK_UPTIME_RAW); bool follow = true; + bool verbose = false; uint32_t wid = 0; int index = 0; @@ -339,7 +338,7 @@ int main(int argc, char **argv) } else if (strcmp(arg, "--no-follow") == 0) { follow = false; } else if (strcmp(arg, "--verbose") == 0) { - g_verbose = true; + verbose = true; } else if (strcmp(arg, "--window") == 0 && i + 1 < argc) { wid = (uint32_t) parse_positive(argv[++i], UINT32_MAX); if (!wid) fail(2, "invalid window id '%s'", argv[i]); @@ -429,7 +428,7 @@ int main(int argc, char **argv) if (follow) focus_window(&psn, wid, window_ref); if (window_ref) CFRelease(window_ref); - if (g_verbose) { + if (verbose) { printf("window %u (via %s) -> space %d (id %llu): %s in %.1f ms, %s, total %.1f ms\n", wid, source, index, (unsigned long long) dst_sid, moved ? "moved" : "already there", move_ms, diff --git a/tests/follow.m b/tests/follow.m index 9677661..855982d 100644 --- a/tests/follow.m +++ b/tests/follow.m @@ -10,6 +10,8 @@ extern CFArrayRef SLSCopySpacesForWindows(int cid, int selector, CFArrayRef windows); extern int SLSSpaceGetType(int cid, uint64_t sid); +#define SPACE_TYPE_FULLSCREEN 4 + static void pump(double seconds) { NSDate *end = [NSDate dateWithTimeIntervalSinceNow:seconds]; @@ -102,14 +104,14 @@ int main(int argc, char **argv) ++index; uint64_t sid = [space[@"id64"] unsignedLongLongValue]; if (sid == src) src_index = index; - else if (current && !dst && SLSSpaceGetType(cid, sid) != 4) { + else if (current && !dst && SLSSpaceGetType(cid, sid) != SPACE_TYPE_FULLSCREEN) { dst = sid; dst_index = index; } } } if (displays) CFRelease(displays); - if (!src_index || !dst_index || SLSSpaceGetType(cid, src) == 4) { + if (!src_index || !dst_index || SLSSpaceGetType(cid, src) == SPACE_TYPE_FULLSCREEN) { fputs("SKIP: test-follow requires two normal Spaces on the active display\n", stderr); return 77; }