-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpad.tmux
executable file
·144 lines (118 loc) · 3.37 KB
/
tpad.tmux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash
set -eo pipefail
# Configuration
LOG_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/tpad.log"
exec &>>"$LOG_FILE"
readonly CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly TPAD_SCRIPT="${CURRENT_DIR}/tpad.tmux"
declare -A DEFAULTS=(
[title]="#[fg=magenta,bold] TPad: @instance@ "
[dir]="$HOME"
[width]="60%"
[height]="60%"
[style]="fg=blue"
[border_style]="fg=cyan,rounded"
)
main() {
check_dependencies
case "${1:-}" in
toggle) toggle_popup "$2" ;;
"") initialize_instances ;;
*) show_help; exit 1 ;;
esac
}
initialize_instances() {
tmux show-options -g | awk -v FS="-" '/^@tpad/{ print $2}' | sort -u | while read -r instance; do
bind_key "$instance"
done
}
toggle_popup() {
local instance="$1"
local session="tpad_${instance}"
local current_session="$(tmux display-message -p '#{session_name}')"
if [[ "$current_session" == "$session" ]]; then
tmux detach
else
create_session_if_needed "$instance" "$session"
local popup_opts=()
while IFS= read -r opt; do
popup_opts+=("$opt")
done < <(build_popup_options "$instance")
tmux display-popup "${popup_opts[@]}" -E "tmux attach -t $session"
fi
}
create_session_if_needed() {
local instance="$1" session="$2"
tmux has-session -t "$session" 2>/dev/null && return
local dir="$(get_config "$instance" dir)"
local session_id="$(tmux new-session -dP -s "$session" -c "$dir" -F '#{session_id}')"
configure_session "$instance" "$session_id"
}
configure_session() {
local instance="$1" session_id="$2"
tmux set-option -s -t "$session_id" default-terminal "$TERM"
tmux set-option -s -t "$session_id" key-table "tpad_$instance"
tmux set-option -s -t "$session_id" status off
tmux set-option -s -t "$session_id" detach-on-destroy on
# Add small delay to ensure session is ready
sleep 0.1
local prefix="$(get_config "$instance" prefix)"
[[ "$prefix" ]] && tmux set-option -s -t "$session_id" prefix "$prefix"
local cmd="$(get_config "$instance" cmd)"
if [[ -n "$cmd" ]]; then
# Run the command and make the session exit when it's done
tmux send-keys -t "$session_id" "$cmd; exit" C-m
fi
}
get_config() {
local instance="$1" key="$2"
local tmux_var="@tpad-${instance}-${key}"
local val="$(tmux show-option -gqv "$tmux_var")"
if [[ -z "$val" ]]; then
val="${DEFAULTS[$key]/@instance@/${instance^}}"
fi
echo "$val"
}
bind_key() {
local instance="$1"
local key="$(get_config "$instance" bind)"
[[ -z "$key" ]] && return
tmux bind-key "$key" run-shell "$TPAD_SCRIPT toggle $instance"
tmux bind-key -T "tpad_$instance" "$key" run-shell "$TPAD_SCRIPT toggle $instance"
}
build_popup_options() {
local instance="$1"
declare -A opt_map=(
[T]="title"
[S]="style"
[s]="border_style"
[b]="border_lines"
[h]="height"
[w]="width"
[x]="pos_x"
[y]="pos_y"
[d]="dir"
[e]="env"
)
for opt in "${!opt_map[@]}"; do
local val="$(get_config "$instance" "${opt_map[$opt]}")"
[[ -n "$val" ]] && echo "-${opt}" "${val}"
done
}
check_dependencies() {
if ! command -v tmux &>/dev/null; then
echo "Error: tmux is required but not installed" >&2
exit 1
fi
}
show_help() {
cat <<EOF
TPad - Tmux Popup Manager
Usage:
tpad.tmux [command]
Commands:
(no command) Initialize all configured instances
toggle Toggle a popup instance
EOF
}
main "$@"