-
Notifications
You must be signed in to change notification settings - Fork 1
/
svtm-ctl
executable file
·92 lines (77 loc) · 2.01 KB
/
svtm-ctl
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
#!/bin/sh
die() {
echo "$0: $*" 1>&2
exit 1
}
numbers() {
grep '^[0-9]\+$' "$@"
}
# lowest available number from stdin starting from 1
# similar behavior to open(3) - opens lowest file descriptor
lowest() {
sort -n | awk 'NR != $1 { i = NR; exit } END { print i ? i : NR+1 }'
}
next() {
# lowest number after focus OR very first
awk '!first && NR==1 { first=$1 }
$1 > '${focus:-1}' && ($1 < new || !new) { new=$1 }
END { print new ? new : first }'
}
prev() {
# highest number below focus OR very last
awk '$1 < '${focus:-1}' && $1 > new { new=$1 }
$1 > tail { tail = $1 }
END { print new ? new : tail }'
}
create() {
lowest | if read i; then
if [ -f "$SVTM_DIR/$focus/pid" ] && read pid < "$SVTM_DIR/$focus/pid"; then
cd "$(realpath /proc/$pid/cwd)"
fi
abduco -fn "$SVTM_SESSION-$i" svtm-vt "$SVTM_DIR/$i" "$@"
echo "$i"
fi
}
# kill the currently focused process
close() {
prev
if read pid < "$SVTM_DIR/$focus/pid"; then
kill "$pid"
# abduco doesn't finish session unless it sends content
# to abduco server
abduco -a "$SVTM_SESSION-$focus" < /dev/tty
# TODO: https://github.com/martanne/abduco/issues/44
fi
}
edit() {
create sh -c "exec '$EDITOR' - < '$SVTM_DIR/$focus/dump'"
echo dump > "$SVTM_DIR/$focus/cmd"
}
if [ -z "$SVTM_SESSION" ]; then
die 'no SVTM session'
fi
if [ $# -lt 1 ]; then
die 'give arg'
fi
read focus < "$SVTM_MASTER_DIR/focus"
case "$1" in
# these functions read clients from stdin, and spit out the new client to focus on
edit|close|create|prev|next) ls "$SVTM_DIR/" | numbers | "$@" > "$SVTM_MASTER_DIR/focus"
if
[ -f "$SVTM_MASTER_DIR/focus" ] && \
read new < "$SVTM_MASTER_DIR/focus" && \
[ -n "$new" ] && [ "$focus" != "$new" ]
then
:
else
die "cannot change focus from '$focus' to '$new'"
fi;;
scroll) echo "$@" > "$SVTM_DIR/$focus/cmd";;
quit) xargs -r kill -s INT < "$SVTM_MASTER_DIR/pid";;
*) die "unknown command";;
esac
if [ -f "$SVTM_MASTER_DIR/view" ]; then
numbers "$SVTM_MASTER_DIR/view" | while read pid; do
kill "$pid"
done
fi