Commit 6fcf429bc218

Vincent Demeester <vincent@sbr.pm>
2026-01-30 11:23:32
fix(shpool-connect): show session status and improve error handling
- Add error handler with trap to show errors before closing - Display "(attached)" suffix for already-connected sessions - Strip status suffix before connecting Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a025280
Changed files (1)
pkgs
my
scripts
pkgs/my/scripts/bin/shpool-connect
@@ -3,10 +3,25 @@
 #
 # Features:
 # - Lists available hosts from SSH config (detects shpool-enabled hosts)
-# - Lists sessions on selected host
+# - Lists sessions on selected host (shows connected/disconnected status)
 # - Attaches to selected session using kitty (wayland) or current terminal
+# - Handles already-attached sessions (force detach/attach)
 set -euo pipefail
 
+# Error handler - show error and wait for user input before exiting
+error_handler() {
+    local exit_code=$?
+    local line_no=$1
+    if [[ $exit_code -ne 0 ]]; then
+        echo ""
+        echo "Error on line $line_no: Command exited with status $exit_code"
+        echo "Press Enter to close..."
+        read -r
+    fi
+    exit $exit_code
+}
+trap 'error_handler $LINENO' ERR
+
 # Extract shpool-enabled hosts from SSH config
 # These are hosts with the pattern "host/*" (shpool session aliases)
 get_shpool_hosts() {
@@ -64,19 +79,30 @@ if [[ -z "$selected_host" ]]; then
     exit 0
 fi
 
-# Step 2: Get sessions on the host
-sessions=$(ssh "$selected_host" shpool list 2>/dev/null | tail -n +2 | awk '{print $1}')
+# Step 2: Get sessions on the host with status
+# Format: "session-name (status)" where status is connected/disconnected
+session_list=$(ssh "$selected_host" shpool list 2>/dev/null | tail -n +2 | awk '{
+    status = $3
+    if (status == "connected") {
+        print $1 " (attached)"
+    } else {
+        print $1
+    }
+}')
 
-if [[ -z "$sessions" ]]; then
+if [[ -z "$session_list" ]]; then
     notify "shpool" "No sessions on $selected_host"
     exit 0
 fi
 
 # Add option to create new session
-sessions="[new session]"$'\n'"$sessions"
+sessions="[new session]"$'\n'"$session_list"
 
 selected_session=$(echo "$sessions" | select_item "Session on $selected_host: ")
 
+# Strip status suffix if present (e.g., "session (attached)" -> "session")
+selected_session="${selected_session% (attached)}"
+
 if [[ -z "$selected_session" ]]; then
     exit 0
 fi