This repository has been archived by the owner on Jan 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
kak-shell
executable file
·84 lines (70 loc) · 1.62 KB
/
kak-shell
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
#!/bin/sh
# Usage:
#
# kak-shell [session] [commands]
#
# Example – Basic:
#
# kak-shell
#
# Example – Connect to a session from the command-line and attach:
#
# kak-shell kanto :attach
#
# Example – Connect to a session interactively and attach:
#
# kak-shell '' :attach
main() {
# Session
session=$1
shift
# Shell commands
commands=$@
# Prompt for a Kakoune session
if test -z "$session"; then
prompt_kakoune_session
[ "$text" ] || exit 1
session=$text
fi
# Connect to the given session and execute the shell commands
connect "$session" "$@"
}
connect() {
session=$1
shift
setsid kak -s "$session" -d < /dev/null > /dev/null 2>&1 &
wait_for_session "$session"
kak -c "$session" -e "connect-detach $@"
sh connect.sh
}
prompt_kakoune_session() {
kak_session_list=$(kak -l | sort)
echo 'Kakoune sessions:'
printf "$kak_session_list" | number_lines
echo '+ create new session'
printf 'Kakoune session:'
read kak_session
if is_number "$kak_session"; then
kak_session=$(echo "$kak_session_list" | get_line "$kak_session")
fi
text=$kak_session
}
wait_for_session() {
session=$1
# Wait for session
# Grep in quiet mode with fixed strings and whole line switches
while ! kak -l | grep -q -F -x "$session"; do
sleep 0.1
done
}
# Utility functions ────────────────────────────────────────────────────────────
is_number() {
test "$1" -eq "$1" 2> /dev/null
}
number_lines() {
awk '{ print NR, $0 }'
}
get_line() {
sed "${1}q;d"
}
main "$@"