-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdtsh
executable file
·97 lines (82 loc) · 1.82 KB
/
dtsh
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
#!/bin/sh
#---
# Wrapper around the 'dtach' utility to manage multiple sessions
# stored in the same directory, creating/switching to them easily.
#---
DIR=$HOME/.config/dtsh
DEFAULT_NAME=s
DEFAULT_CMD=$SHELL
DTACH_ESCAPE='^\'
DTACH_OPTS="-e $DTACH_ESCAPE -z"
usage() {
cat <<EOF
Usage: ${0##*/} <session name>
${0##*/} -c [<session name>] [<command>]
Open sessions:
EOF
get_sessions
if [ "$DTSH_SESSION" ]
then
echo " Currently attached: $(basename $DTSH_SESSION)"
fi
}
# print list of current sessions
get_sessions() {
for s in "$DIR"/*
do
[ -S "$s" ] || continue
printf " $(basename $s)\n"
done
}
# Find and print the filename for the new session
# arguments will be used as name
new_session_name() {
local name=${*:-$DEFAULT_NAME}
local fname;
while true
do
fname="${DIR}/${name}${i}"
[ -S "$fname" ] || break
i=$((i+1))
done
printf "$fname"
}
# Shell commands to run after a new deattached session
# is created.
#
new_session_command() {
cat <<EOF
printf "This is a dtsh session: '$(basename "$DTSH_SESSION")'\n"
printf "To detach it from the terminal press: %s\n" "${DTACH_ESCAPE/\\/\\\\}"
exec ${@:-$DEFAULT_CMD}
EOF
}
if ! [ "$1" ]
then
usage
exit 1
elif [ "$1" = "-c" ]
then
shift
DTSH_SESSION=$(new_session_name $1)
shift
DTSH_CMD=$(new_session_command $@)
mkdir -p "$DIR"
export DTSH_SESSION
exec dtach -c "$DTSH_SESSION" $DTACH_OPTS sh -c "$DTSH_CMD"
else
s="$DIR/$*"
if [ "$DTSH_SESSION" = "$s" ]
then
echo "err: the session is already attached to the current terminal"
exit 2
elif ! [ -S "$s" ]
then
echo "The given session doesn't exist: '$*'"
echo "Use the -c flag to create it, or pick an existing session:"
get_sessions
exit 2
else
exec dtach -a "$s" $DTACH_OPTS
fi
fi