AutoYADM commit: 2025-05-19 17:57:38
This commit is contained in:
parent
d9fb3ba788
commit
a2f513b31d
1 changed files with 82 additions and 45 deletions
|
@ -1,51 +1,88 @@
|
|||
function kak-session -w kak --description "kakoune where sessions are derived from parent git dirs"
|
||||
# TODO(enricozb): https://github.com/enricozb/tabs.kak/issues/12
|
||||
# TODO(enricozb): https://github.com/enricozb/tabs.kak/issues/12
|
||||
|
||||
set files # all arguments without a `-` prefix
|
||||
set flags # all arguments with a `-` prefix
|
||||
set files # all arguments without a `-` prefix
|
||||
set flags # all arguments with a `-` prefix
|
||||
|
||||
for arg in $argv
|
||||
if string match --quiet --regex '^-' -- $arg
|
||||
set --append flags $arg
|
||||
for arg in $argv
|
||||
if string match --quiet --regex '^-' -- $arg
|
||||
set --append flags $arg
|
||||
else
|
||||
set --append files $arg
|
||||
end
|
||||
end
|
||||
|
||||
# set git_dir if there is at least one file
|
||||
if set --query files[1]
|
||||
set git_dir (git -C (dirname $files[1]) rev-parse --show-toplevel 2>/dev/null)
|
||||
end
|
||||
|
||||
function fdup
|
||||
set target $argv[1]
|
||||
set dir (path dirname (pwd))
|
||||
if test -n "$argv[2]"
|
||||
set dir (path dirname "$argv[2]")
|
||||
end
|
||||
while test "$dir" != /
|
||||
set result (fd --max-depth 1 -H -I -t f "$target" "$dir")
|
||||
if test -n "$result"
|
||||
echo (path dirname $result)
|
||||
return 0
|
||||
end
|
||||
set dir (path dirname $dir)
|
||||
end
|
||||
|
||||
return 1
|
||||
end
|
||||
|
||||
set kakroot (fdup .kakroot $files[1])
|
||||
|
||||
if test -n "$kakroot"
|
||||
set session_id (echo -n "$(path basename "$kakroot")" | tr --complement '[:alpha:]_-' '-')
|
||||
# if there is no existing session
|
||||
if not command kak -l | rg -xqF "$session_id"
|
||||
set fifo_dir (mktemp -d --suffix=kak-init.XXXXXX)
|
||||
set fifo "$fifo_dir/fifo"
|
||||
mkfifo "$fifo"
|
||||
|
||||
setsid kak -d -s "$session_id" -E "cd %[$kakroot]; echo -to-file $fifo ready" &
|
||||
|
||||
# once $fifo has data, the kakoune server has initialized.
|
||||
cat $fifo >/dev/null
|
||||
|
||||
command rm -r "$fifo_dir"
|
||||
end
|
||||
|
||||
command kak -c "$session_id" $flags $files
|
||||
else if [ -n "$git_dir" ]
|
||||
# when in a git directory, share sessions
|
||||
# the shared session id is {repo}__{branch} where repo is the remote repo name
|
||||
# or the basename of the git directory.
|
||||
if set remote (git -C $git_dir remote get-url origin 2>/dev/null)
|
||||
set repo (basename -s .git $remote)
|
||||
else
|
||||
set repo (basename $git_dir)
|
||||
end
|
||||
set branch (git -C $git_dir symbolic-ref --short HEAD)
|
||||
|
||||
set session_id (echo -n "$repo" | tr --complement '[:alpha:]_-' '-')
|
||||
|
||||
# if there is no existing session
|
||||
if not command kak -l | rg -xqF "$session_id"
|
||||
set fifo_dir (mktemp -d --suffix=kak-init.XXXXXX)
|
||||
set fifo "$fifo_dir/fifo"
|
||||
mkfifo "$fifo"
|
||||
|
||||
setsid kak -d -s "$session_id" -E "cd %[$git_dir]; echo -to-file $fifo ready" &
|
||||
|
||||
# once $fifo has data, the kakoune server has initialized.
|
||||
cat $fifo >/dev/null
|
||||
|
||||
command rm -r "$fifo_dir"
|
||||
end
|
||||
|
||||
command kak -c "$session_id" $flags $files
|
||||
else
|
||||
set --append files $arg
|
||||
command kak $flags $files
|
||||
end
|
||||
end
|
||||
|
||||
# set git_dir if there is at least one file
|
||||
if set --query files[1]
|
||||
set git_dir (git -C (dirname $files[1]) rev-parse --show-toplevel 2>/dev/null)
|
||||
end
|
||||
|
||||
# when in a git directory, share sessions
|
||||
if [ -n "$git_dir" ]
|
||||
# the shared session id is {repo}__{branch} where repo is the remote repo name
|
||||
# or the basename of the git directory.
|
||||
if set remote (git -C $git_dir remote get-url origin 2>/dev/null)
|
||||
set repo (basename -s .git $remote)
|
||||
else
|
||||
set repo (basename $git_dir)
|
||||
end
|
||||
set branch (git -C $git_dir symbolic-ref --short HEAD)
|
||||
|
||||
set session_id (echo -n "$repo" | tr --complement '[:alpha:]_-' '-')
|
||||
|
||||
# if there is no existing session
|
||||
if not command kak -l | rg -xqF "$session_id"
|
||||
set fifo_dir (mktemp -d --suffix=kak-init.XXXXXX)
|
||||
set fifo "$fifo_dir/fifo"
|
||||
mkfifo "$fifo"
|
||||
|
||||
setsid kak -d -s "$session_id" -E "cd %[$git_dir]; echo -to-file $fifo ready" &
|
||||
|
||||
# once $fifo has data, the kakoune server has initialized.
|
||||
cat $fifo >/dev/null
|
||||
|
||||
command rm -r "$fifo_dir"
|
||||
end
|
||||
|
||||
command kak -c "$session_id" $flags $files
|
||||
else
|
||||
command kak $flags $files
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue