Commit 6a76ae3

Ansari <ping@ansari.wtf>
2025-12-20 05:04:17
Refactor application launch commands to use smart-launch script; remove deprecated launch scripts
1 parent eeccca0
.config/waybar/config.jsonc
@@ -17,7 +17,7 @@
     "format": "󰣇",
     "interval": 0,
     "tooltip-format": "About System",
-    "on-click": "launch-or-focus-tui \"about\" \"bash -c 'fastfetch; read -n 1 -s'\"",
+    "on-click": "smart-launch --tui 'about-system' 'bash -c \"fastfetch; read -n 1 -s\"'"
   },
 
   "hyprland/workspaces#main": {
.local/share/applications/arch-cleanup.desktop
@@ -1,7 +1,7 @@
 [Desktop Entry]
 Version=1.0
 Name=Arch Cleanup
-Exec=launch-or-focus-tui "arch-cleanup" "/home/robin/.wraith/scripts/arch-spring-cleanup.sh"
+Exec=smart-launch --tui "arch-cleanup" "/home/robin/.wraith/scripts/arch-spring-cleanup.sh"
 Terminal=false
 Type=Application
 Icon=/home/robin/.assets/icons/shell.png
.local/share/applications/docker-desktop.desktop
@@ -1,7 +1,7 @@
 [Desktop Entry]
 Version=1.0
 Name=Docker Desktop
-Exec=launch-or-focus-tui "docker-desktop" "lazydocker"
+Exec=smart-launch --tui "docker-desktop" "lazydocker"
 Terminal=false
 Type=Application
 Icon=/home/robin/.assets/icons/docker-desktop.png
.local/share/applications/google-gemini.desktop
@@ -1,7 +1,7 @@
 [Desktop Entry]
 Version=1.0
 Name=Google Gemini
-Exec=launch-or-focus-webapp "chrome-gemini.google.com" "https://gemini.google.com"
+Exec=smart-launch --web "chrome-gemini.google.com" "https://gemini.google.com"
 Terminal=false
 Type=Application
 Icon=/home/robin/.assets/icons/google-gemini.png
.local/share/applications/install-apps.desktop
@@ -1,7 +1,7 @@
 [Desktop Entry]
 Version=1.0
 Name=Install Apps
-Exec=launch-or-focus-tui "install-apps" "install-app"
+Exec=smart-launch --tui "install-apps" "install-app"
 Terminal=false
 Type=Application
 Icon=/home/robin/.assets/icons/shell.png
.local/share/applications/youtube-music.desktop
@@ -1,7 +1,7 @@
 [Desktop Entry]
 Version=1.0
 Name=YT Music
-Exec=launch-or-focus-webapp "chrome-music.youtube.com" "https://music.youtube.com"
+Exec=smart-launch --web "chrome-music.youtube.com" "https://music.youtube.com"
 Terminal=false
 Type=Application
 Icon=/home/robin/.assets/icons/youtube-music.png
.wraith/bin/install-app
@@ -19,11 +19,11 @@ if [ "$#" -lt 3 ]; then
     APP_LOCATION=$(gum input --prompt "URL> " --placeholder "https://github.com")
     DOMAIN=$(echo "$APP_LOCATION" | awk -F[/:] '{print $4}' | sed 's/^www\.//')
     APP_ID="chrome-$DOMAIN"
-    [[ "$SINGLETON" == "yes" ]] && CUSTOM_EXEC="launch-or-focus-webapp \"$APP_ID\" \"$APP_LOCATION\"" || CUSTOM_EXEC="launch-webapp \"$APP_LOCATION\""
+    [[ "$SINGLETON" == "yes" ]] && CUSTOM_EXEC="smart-launch --web \"$APP_ID\" \"$APP_LOCATION\"" || CUSTOM_EXEC="smart-launch --web -n \"$APP_ID\" \"$APP_LOCATION\""
   else
     APP_ID=$(echo "$APP_NAME" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed 's/[^a-z0-9-]//g')
     APP_LOCATION=$(gum input --prompt "Path> " --placeholder "/usr/bin/htop")
-    [[ "$SINGLETON" == "yes" ]] && CUSTOM_EXEC="launch-or-focus-tui \"$APP_ID\" \"$APP_LOCATION\"" || CUSTOM_EXEC="launch-tui \"$APP_ID\" \"$APP_LOCATION\""
+    [[ "$SINGLETON" == "yes" ]] && CUSTOM_EXEC="smart-launch --tui \"$APP_ID\" \"$APP_LOCATION\"" || CUSTOM_EXEC="smart-launch --tui -n \"$APP_ID\" \"$APP_LOCATION\""
   fi
 else
   APP_NAME="$1"; APP_LOCATION="$2"; ICON_REF="$3"; CUSTOM_EXEC="$4"; MIME_TYPES="$5"
.wraith/bin/launch-or-focus
@@ -1,27 +0,0 @@
-#!/usr/bin/env bash
-
-WINDOW_PATTERN="$1"
-# Default to uwsm-app, but allow override
-LAUNCH_COMMAND="${2:-"uwsm-app -- $WINDOW_PATTERN"}"
-
-# 1. Removed .title check to avoid matching terminal command history
-# 2. Added .initialClass check (helps with some electron apps)
-# 3. Added sort_by(.focusHistoryID) to ensure we grab the most recently used instance
-WINDOW_ADDRESS=$(hyprctl clients -j | jq -r --arg p "$WINDOW_PATTERN" '
-  map(select(
-    (.class | test("\\b" + $p + "\\b"; "i")) or 
-    (.initialClass | test("\\b" + $p + "\\b"; "i")) or 
-    (.class | contains($p)) or 
-    (.class | test($p; "i"))
-  )) 
-  | sort_by(.focusHistoryID) 
-  | reverse 
-  | .[0].address // empty'
-)
-
-if [[ -n $WINDOW_ADDRESS && $WINDOW_ADDRESS != "null" ]]; then
-  hyprctl dispatch focuswindow "address:$WINDOW_ADDRESS"
-else
-  # Use eval to handle arguments in LAUNCH_COMMAND correctly
-  eval exec setsid $LAUNCH_COMMAND &>/dev/null &
-fi
\ No newline at end of file
.wraith/bin/launch-or-focus-tui
@@ -1,6 +0,0 @@
-#!/bin/bash
-
-APP_ID="org.wraith.$(basename $1)"
-LAUNCH_COMMAND="launch-tui $@"
-
-exec launch-or-focus "$APP_ID" "$LAUNCH_COMMAND"
\ No newline at end of file
.wraith/bin/launch-or-focus-webapp
@@ -1,7 +0,0 @@
-#!/bin/bash
-
-WINDOW_PATTERN="$1"
-shift
-LAUNCH_COMMAND="launch-webapp $@"
-
-exec launch-or-focus "$WINDOW_PATTERN" "$LAUNCH_COMMAND"
.wraith/bin/launch-tui
@@ -1,6 +0,0 @@
-#!/bin/bash
-
-APP_ID="org.wraith.$(basename $1)"
-COMMAND="$2"
-
-exec setsid uwsm-app -- xdg-terminal-exec --app-id=$APP_ID -e "$COMMAND" "${@:3}"
\ No newline at end of file
.wraith/bin/launch-webapp
@@ -1,10 +0,0 @@
-#!/bin/bash
-
-browser=$(xdg-settings get default-web-browser)
-
-case $browser in
-google-chrome* | brave-browser* | microsoft-edge* | opera* | vivaldi* | helium*) ;;
-*) browser="chromium" ;;
-esac
-
-exec setsid uwsm-app -- $browser --app="$1" "${@:2}"
\ No newline at end of file
.wraith/bin/smart-launch
@@ -0,0 +1,123 @@
+#!/usr/bin/env bash
+
+# ==============================================================================
+# smart-launch
+# A unified script to focus a Hyprland window if it exists, or launch it if not.
+# Supports Standard GUI apps, TUIs (via xdg-terminal-exec), and Web Apps.
+# ==============================================================================
+
+show_help() {
+    echo "Usage: $(basename "$0") [OPTIONS] <PATTERN> [COMMAND/URL]"
+    echo ""
+    echo "Options:"
+    echo "  -t, --tui      Launch as a Terminal UI (sets specific App ID)"
+    echo "  -w, --web      Launch as a Web App (using --app mode)"
+    echo "  -c, --cmd      Custom launch command (overrides default behavior)"
+    echo "  -n, --new      Force a new instance (do not focus existing windows)"
+    echo "  -h, --help     Show this help message"
+    echo ""
+    echo "Examples:"
+    echo "  Standard:      $(basename "$0") firefox"
+    echo "  New Instance:  $(basename "$0") --new firefox"
+    echo "  TUI:           $(basename "$0") --tui btop"
+}
+
+# --- Browser Detection ---
+get_browser() {
+    local browser
+    browser=$(xdg-settings get default-web-browser)
+    case $browser in
+        google-chrome*|brave-browser*|microsoft-edge*|opera*|vivaldi*|helium*) ;;
+        *) browser="chromium" ;;
+    esac
+    echo "$browser"
+}
+
+# --- Argument Parsing ---
+MODE="standard"
+FORCE_NEW=0
+
+while [[ "$#" -gt 0 ]]; do
+    case $1 in
+        -t|--tui) MODE="tui"; shift ;;
+        -w|--web|--webapp) MODE="web"; shift ;;
+        -c|--cmd) MODE="custom"; shift ;;
+        -n|--new) FORCE_NEW=1; shift ;;
+        -h|--help) show_help; exit 0 ;;
+        -*) echo "Unknown option: $1"; show_help; exit 1 ;;
+        *) break ;;
+    esac
+done
+
+if [[ -z "$1" ]]; then
+    echo "Error: Missing arguments."
+    show_help
+    exit 1
+fi
+
+# --- Logic Configuration ---
+
+WINDOW_PATTERN="$1"
+LAUNCH_CMD=""
+
+case $MODE in
+    tui)
+        BIN_NAME=$(basename "$1")
+        BIN_NAME=${BIN_NAME// /-}
+        BIN_NAME=${BIN_NAME//./-}
+        BIN_NAME=${BIN_NAME%-}
+        APP_ID="org.wraith.${BIN_NAME}"
+        WINDOW_PATTERN="$APP_ID"
+        
+        shift
+        if [[ -n "$1" ]]; then CMD_ARGS="$@"; else CMD_ARGS="$BIN_NAME"; fi
+        LAUNCH_CMD="uwsm-app -- xdg-terminal-exec --app-id='$APP_ID' -e $CMD_ARGS"
+        ;;
+
+    web)
+        shift
+        URL="$1"
+        shift
+        EXTRA_ARGS="$@"
+        BROWSER=$(get_browser)
+        LAUNCH_CMD="uwsm-app -- $BROWSER --app=$URL $EXTRA_ARGS"
+        ;;
+
+    custom)
+        shift
+        LAUNCH_CMD="$@"
+        ;;
+
+    standard)
+        LAUNCH_CMD="uwsm-app -- $WINDOW_PATTERN"
+        ;;
+esac
+
+# --- Hyprland Detection Logic ---
+
+WINDOW_ADDRESS=""
+
+# Only check for existing windows if NOT forcing a new instance
+if [[ $FORCE_NEW -eq 0 ]]; then
+    WINDOW_ADDRESS=$(hyprctl clients -j | jq -r --arg p "$WINDOW_PATTERN" '
+      map(select(
+        (.class | test("\\b" + $p + "\\b"; "i")) or
+        (.initialClass | test("\\b" + $p + "\\b"; "i")) or
+        (.class | contains($p)) or
+        (.class | test($p; "i"))
+      ))
+      | sort_by(.focusHistoryID)
+      | reverse
+      | .[0].address // empty'
+    )
+fi
+
+# --- Execution ---
+
+if [[ -n $WINDOW_ADDRESS && $WINDOW_ADDRESS != "null" ]]; then
+    echo "Focusing window: $WINDOW_PATTERN ($WINDOW_ADDRESS)"
+    hyprctl dispatch focuswindow "address:$WINDOW_ADDRESS"
+else
+    echo "Launching new instance: $LAUNCH_CMD"
+    eval exec setsid $LAUNCH_CMD &>/dev/null &
+fi
\ No newline at end of file