[swiftbar] Add swiftbar controls

This commit is contained in:
2026-01-12 18:03:01 -05:00
parent 94b5f324f7
commit fec4946e18
6 changed files with 195 additions and 12 deletions

View File

@ -0,0 +1,77 @@
#!/bin/bash
set -euo pipefail
PLIST="$HOME/Library/LaunchAgents/com.hungryroot.django.plist"
ICON="🔌"
MATCH="manage.py runserver"
LABEL="$(/usr/bin/plutil -extract Label raw -o - "$PLIST" 2>/dev/null || true)"
UID_NUM="$(/usr/bin/id -u)"
DOMAIN="gui/$UID_NUM"
TARGET="$DOMAIN/$LABEL"
is_running() {
/bin/ps waux | /usr/bin/grep -F "$MATCH" | /usr/bin/grep -v grep >/dev/null 2>&1
}
pids() {
/bin/ps waux | /usr/bin/grep -F "$MATCH" | /usr/bin/grep -v grep | /usr/bin/awk '{print $2}'
}
# --- Title ---
if is_running; then
echo "$ICON 🟢"
else
echo "$ICON 🔴"
fi
echo "---"
if is_running; then
echo "Stop | bash='$0' param1=stop terminal=false refresh=true"
echo "Restart | bash='$0' param1=restart terminal=false refresh=true"
else
echo "Start | bash='$0' param1=start terminal=false refresh=true"
fi
echo "---"
echo "Open http://0.0.0.0:8000 | href=http://0.0.0.0:8000"
echo "Debug | bash='$0' param1=debug terminal=true"
echo "Open plist | bash='/usr/bin/open' param1='$PLIST' terminal=false"
ACTION="${1:-}"
case "$ACTION" in
start)
[[ -n "${LABEL:-}" ]] || {
echo "ERROR: couldn't read Label from $PLIST" >&2
exit 1
}
/bin/launchctl bootstrap "$DOMAIN" "$PLIST" 2>/dev/null || true
/bin/launchctl kickstart -k "$TARGET" 2>/dev/null || true
;;
stop)
# THIS is the important part: unload the LaunchAgent so it wont restart.
[[ -n "${LABEL:-}" ]] || exit 0
/bin/launchctl bootout "$TARGET" 2>/dev/null || true
# Optional cleanup: kill any remaining process that matches
pids | /usr/bin/xargs -r kill -TERM 2>/dev/null || true
;;
restart)
"$0" stop
sleep 0.5
"$0" start
;;
debug)
echo "PLIST: $PLIST"
echo "LABEL: $LABEL"
echo "TARGET: $TARGET"
echo
echo "=== launchctl print (may fail in SwiftBar, still useful) ==="
/bin/launchctl print "$TARGET" 2>&1 || true
echo
echo "=== matching processes ==="
/bin/ps waux | /usr/bin/grep -F "$MATCH" | /usr/bin/grep -v grep || echo "(no matches)"
;;
esac