diff --git a/packages/whst/default.nix b/packages/whst/default.nix index 4d8a1dc..6b809ac 100644 --- a/packages/whst/default.nix +++ b/packages/whst/default.nix @@ -1,6 +1,10 @@ { writeShellScriptBin, + imv, + curl, + imagemagick, }: + writeShellScriptBin "whst" '' print_help () { cat << EOF @@ -53,15 +57,32 @@ writeShellScriptBin "whst" '' url="https://http.cat/$status_code" - xdg-open "$url" >/dev/null 2>&1 + tmpfile="$(mktemp --suffix=.jpg)" - ret=$? - - if (( ret != 0 )); then - echo "Failed to open browser automatically. Here's the URL instead:" - echo "$url" + if ! ${curl}/bin/curl -fsSL "$url" -o "$tmpfile"; then + echo "Error: Failed to fetch image for status code $status_code" >&2 + rm -f "$tmpfile" exit 1 fi + dims="$(${imagemagick}/bin/identify -format '%w %h' "$tmpfile" 2>/dev/null)" + if [ -z "$dims" ]; then + echo "Error: Failed to determine image size." >&2 + rm -f "$tmpfile" + exit 1 + fi + + # Split into width and height using shell + set -- $dims + width="$1" + height="$2" + + if ! ${imv}/bin/imv -W "$width" -H "$height" "$tmpfile"; then + echo "Error: Failed to display image with imv." >&2 + rm -f "$tmpfile" + exit 1 + fi + + rm -f "$tmpfile" exit 0 ''