session-or-client() { { project="$(cat -)" if [[ -z $project ]]; then return fi pushd $project export name=${PWD#$HOME/} export name=${name//\//-} export name=${name#-} export server=kaks@$name export client=kakc@$name tmux has-session -t $server || { tmux new -d -s $server -n $server bash -c '[[ -f .envrc ]] && eval "$(direnv export bash)"; { kak -s '$name' -d & }; tmux wait -S '$name'; wait' tmux wait "$name" } if [[ -z $TMUX ]]; then tmux has-session -t $client || tmux new -d -s $client -n $client kak -c $name else tmux new-window -n $client kak -c $name fi popd } debug 2>&1 echo $client } search() { if tty -s then fd -d1 "." -t d $HOME $HOME/projects | fzf -1 else output=$(mktemp -d /tmp/kakup.XXXXXXXX)/fifo mkfifo ${output} st -e bash -c "fd -d1 '.' -t d $HOME $HOME/projects | fzf -1 | tee ${output} >/dev/null 2>&1" & project="$(cat ${output})" rm -r $(dirname ${output}) fi pushd $project >/dev/null 2>&1 if grep 'bare = true' config >/dev/null 2>&1 ; then if tty -s then branch="$(git branch -a --format '%(refname)' | fzf -1)" else output=$(mktemp -d /tmp/kakup.XXXXXXXX)/fifo mkfifo ${output} st -e bash -c "git branch -a --format '%(refname)' | fzf -1 >${output}" & branch="$(cat ${output})" rm -r $(dirname ${output}) fi echo "$branch" >debug 2>&1 if [[ "$branch" == "refs/remotes/"* ]]; then git branch ${branch#refs/remotes/*/} -t $branch >debug 2>&1 git worktree add ${branch#refs/remotes/*/} ${branch#refs/remotes/*/} >debug 2>&1 branch="${branch#refs/remotes/*/}" elif [[ "$branch" == "refs/heads/"* ]]; then branch="${branch#refs/heads/}" git worktree add $branch $branch >debug 2>&1 fi echo "$project/$branch" else echo "$project" fi popd >/dev/null 2>&1 } case "${@}" in "") client="$(pwd | session-or-client)" ;; .) client="$(search | session-or-client)" ;; *) client="$(echo "${@}" | session-or-client)" ;; esac [[ ! -z "$client" ]] && { if tty -s then tmux attach -t "$client" else st -e tmux attach -t "$client" fi }