diff options
| -rwxr-xr-x | .local/bin/kak-shell | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/.local/bin/kak-shell b/.local/bin/kak-shell new file mode 100755 index 0000000..618b109 --- /dev/null +++ b/.local/bin/kak-shell @@ -0,0 +1,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" + + if SOME_VARIABLE=`push.sh 2>/dev/null` + then eval "$SOME_VARIABLE" + # Encode arguments as strings with posix shell push.sh magic. + Push -c args connect-detach "$@" + kak -c "$session" -e "$args" + else echo "push.sh not installed" >&2 + kak -c "$session" -e "connect-detach $@" + fi + sh connect.sh +} + +prompt_kakoune_session() { + kak_session_list=$(kak -l | sort) + kak_session=$(echo "$kak_session_list" | dmenu -p 'kakoune session') + 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 "$@" |
