-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsctl
executable file
·157 lines (139 loc) · 4.4 KB
/
tsctl
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
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
# terminalsession
# Copyright (C) 2022 Danilo Queiroz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###
# Global constants
###
readonly TS_ESCAPE_KEY="^A"
readonly TS_SOCK_DIR=${XDG_RUNTIME_DIR:-"/tmp"}/terminalsession
readonly txt_bold=$(tput bold)
readonly txt_normal=$(tput sgr0)
readonly VERSION=1.0
###
# Session management functions
###
function list_sessions() {
find ${TS_SOCK_DIR} -type s -exec basename {} \; 2>/dev/null
}
function attach_session() {
local session_name=${1:?"Error: missing session_name"}
TERMINALSESSION=$session_name dtach -A ${TS_SOCK_DIR}/${session_name} -e ${TS_ESCAPE_KEY} -z ${SHELL}
}
function kill_session() {
local session_name=${1:?"Error: missing session_name"}
pkill -u $USER -f "dtach -A ${TS_SOCK_DIR}/${session_name}"
}
###
# UX functions
###
function show_menu() {
list_sessions | fzf \
--bind="enter:replace-query+execute($0 attach {q})+clear-query+clear-screen+reload($0 list)" \
--bind="ctrl-k:execute($0 kill {})+clear-query+clear-screen+reload($0 list)" \
--bind="ctrl-r:clear-query+clear-screen+reload($0 list)" \
--bind="ctrl-h:preview($0 __help)" \
--bind="esc:close" \
--keep-right \
--preview-window="down,99%" \
--layout=reverse \
--cycle \
--no-multi \
--info=hidden \
--header-first \
--header "Press ${txt_bold}C-h${txt_normal} for help, ${txt_bold}C-c${txt_normal} to quit." \
--prompt "terminalsession> "
}
function show_interactive_help() {
cat << EOF
${txt_bold}Interactive mode help:${txt_normal}
- Select an existing session and press ${txt_bold}enter${txt_normal} to attach to it.
- To create and attach to a new session, just type the name of the session and
then press ${txt_bold}enter${txt_normal}.
- To kill an existing session, select the session and press ${txt_bold}C-k${txt_normal} to kill it -
it will send a ${txt_bold}SIGTERM${txt_normal} to the dtach process.
- To reload the sessions list, press ${txt_bold}C-r${txt_normal}.
- To detach from a session and return to the interactive mode, press ${txt_bold}C-a${txt_normal}.
- Use ${txt_bold}ESC${txt_normal} to close this help.
EOF
}
function show_version() {
printf "terminalsession version ${VERSION}.\n"
}
function show_help() {
cat << EOF
terminalsession is a simple terminal session manager built on top of
detach and fzf. You can create new sessions, list existing sessions,
attach to a session or kill a session. And that's all!
A session is simply a detachable '\$SHELL'.
Once attached to a session you can use 'CTRL+A' to detach from the
session. In each session, a environment variable '\$TERMINALSESSION'
is set with the name of the current session.
Usage:
tsctl <operation> <params>
Operations:
list lists current sessions
attach <session_name> attaches to an existing session or
creates a new one
kill <session_name> kills an existing session
version shows version info
help shows help info
If no operation is given, it enters interactive mode.
EOF
}
###
# App entry point
###
function check_is_terminalsession() {
if [ ! -z ${TERMINALSESSION} ]; then
echo "Trying to launch tsclt from within a terminalsession isn't supported."
exit 1
fi
}
function main() {
# ensure sock dir
if [ ! -d ${TS_SOCK_DIR} ]; then
mkdir -p ${TS_SOCK_DIR}
fi
local op=$1
shift
case $op in
list)
list_sessions
;;
attach)
check_is_terminalsession
attach_session $*
;;
kill)
check_is_terminalsession
kill_session $*
;;
version | -v | --version)
show_version
;;
help | -h | --help)
show_help
;;
__help)
show_interactive_help
;;
*)
check_is_terminalsession
show_menu
;;
esac
}
main $*