87 lines
2.4 KiB
Bash
Executable File
87 lines
2.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# ---
|
|
# @file_name: kitty_handler.sh
|
|
# @description: A script to launch an new instance, or if opened, then a new tab in the kitty terminal emulator
|
|
# @date: 2025-11-13
|
|
# @version: 0.0.1
|
|
# @usage: ./kitty_handler.sh [-h|--help]
|
|
#
|
|
# @author: Jamie Albert
|
|
# @author_contact: <mailto:jamie.albert@flatmail.me
|
|
# @license: GNU Affero General Public License v3.0 (Included in LICENSE)
|
|
# Copyright (C) 2025, Jamie Albert
|
|
# ---
|
|
set -euo pipefail
|
|
|
|
show_help() {
|
|
cat <<'EOF'
|
|
Usage: ./kitty_handler.sh [OPTIONS]
|
|
|
|
A script to launch an new instance, or if opened, then a new tab in the kitty terminal emulator.
|
|
|
|
OPTIONS:
|
|
-h, --help Show this help message and exit
|
|
-l, --list Returns current kitty instances
|
|
-ni, --new-instance Launches a new kitty instance
|
|
-nt, --new-tab Launches a new kitty tab
|
|
|
|
EXAMPLES:
|
|
./kitty_handler.sh -h Show this help
|
|
./kitty_handler.sh -l Returns current kitty instances
|
|
./kitty_handler.sh -ni Launches a new kitty instance
|
|
./kitty_handler.sh -nt Launches a new kitty tab
|
|
EOF
|
|
exit 0
|
|
}
|
|
|
|
# ---
|
|
# shellcheck disable=1091
|
|
# ---
|
|
main() {
|
|
[[ $# -eq 0 ]] && set -- "-nt"
|
|
[[ "$1" == "-h" || "$1" == "--help" ]] && show_help
|
|
|
|
. /usr/local/share/dao/libs/libs_dao.sh
|
|
case "$1" in
|
|
-nt | --new-tab)
|
|
dao::info "Looking for a kitty instance"
|
|
socket=$(find /tmp -name "kitty.socket-*" -type s 2>/dev/null | head -1 || true)
|
|
if pgrep -x kitty >/dev/null; then
|
|
new_window=$(kitty @ --to "unix:${socket}" launch --type=tab)
|
|
kitty @ --to "unix:${socket}" focus-window --match "id:${new_window}"
|
|
else
|
|
/usr/bin/kitty
|
|
fi
|
|
;;
|
|
-ni | --new-instance)
|
|
/usr/bin/kitty
|
|
;;
|
|
-l | --list)
|
|
if pgrep -x kitty >/dev/null; then
|
|
dao::info "Available Kitty sessions:"
|
|
/usr/bin/kitty @ ls | python3 -c "
|
|
import sys, json
|
|
data = json.load(sys.stdin)
|
|
for i, os_window in enumerate(data):
|
|
print(f'OS Window {i+1}:')
|
|
for tab in os_window['tabs']:
|
|
print(f' Tab {tab[\"id\"]}: {tab[\"title\"]} (Focused: {tab.get(\"is_focused\", False)})')
|
|
for window in tab['windows']:
|
|
print(f' Window {window[\"id\"]}: {window[\"title\"]}')
|
|
if 'cwd' in window:
|
|
print(f' CWD: {window[\"cwd\"]}')
|
|
if 'pid' in window:
|
|
print(f' PID: {window[\"pid\"]}')
|
|
"
|
|
else
|
|
dao::error "No kitty instances are running."
|
|
fi
|
|
;;
|
|
*)
|
|
dao::error 1 "Unknown option: $1" >&2
|
|
;;
|
|
esac
|
|
}
|
|
|
|
main "$@"
|