main
 1#!/usr/bin/env bash
 2# Launch a new kitty window, either in existing instance or new one
 3
 4# Use KITTY_LISTEN_ON if available (when launched from within kitty)
 5if [ -n "$KITTY_LISTEN_ON" ]; then
 6    exec kitty @ launch --type=os-window
 7else
 8    # Find a live kitty socket (kitty auto-appends PID to the socket name)
 9    # Check each socket's PID is still alive, clean up stale ones
10    SOCKET=""
11    while IFS= read -r line; do
12        sock="${line#* }"
13        pid="${sock##*-}"
14        if kill -0 "$pid" 2>/dev/null; then
15            SOCKET="$sock"
16            break
17        else
18            # Clean up stale socket from dead process
19            rm -f "$sock"
20        fi
21    done < <(find /tmp -maxdepth 1 -name 'my-kitty-*' -type s -printf '%T@ %p\n' 2>/dev/null | sort -rn)
22
23    if [ -n "$SOCKET" ]; then
24        exec kitty @ --to "unix:$SOCKET" launch --type=os-window
25    else
26        # No existing instance, start fresh
27        exec kitty
28    fi
29fi