From 9837828c7262ee923b8d383fb0ddd2053bf6e3a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Sun, 29 Dec 2024 13:42:34 +0100 Subject: [PATCH] feat(tmux): sessionizer and ssher fzf preview and hashed names --- tmux/tmux-sessionizer | 19 +++++++--- tmux/tmux-ssher | 88 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 84 insertions(+), 23 deletions(-) diff --git a/tmux/tmux-sessionizer b/tmux/tmux-sessionizer index d0894a4..153141b 100755 --- a/tmux/tmux-sessionizer +++ b/tmux/tmux-sessionizer @@ -3,18 +3,27 @@ if [[ $# -eq 1 ]]; then selected=$1 else - if ! [[ -f "$GGROOT/compiled" ]]; then - find -L $GGROOT -mindepth 1 -maxdepth 5 -type d -name .git -prune | xargs -n 1 dirname > "$GGROOT/compiled" - fi - selected=$(cat "$GGROOT/compiled" | fzf --cycle --bind 'tab:toggle-up,btab:toggle-down') + selected=$(ggman exec pwd 2>&1 >/dev/null \ + | fzf --cycle \ + --bind 'tab:toggle-up,btab:toggle-down' \ + --header "Navigate with ARROW KEYS or TAB/S-TAB. Select with ENTER." \ + --border "double" \ + --border-label "tmux-sessionizer" \ + --ansi \ + --highlight-line \ + ) fi if [[ -z $selected ]]; then exit 0 fi +path=$selected +while [[ "$path" != "" && ! -e "$path/.sessionizer" ]]; do + path=${path%/*} +done -selected_name=$(basename "$selected" | tr . _) +selected_name="$(cat $path/.sessionizer)_$(basename $selected | tr . _ | head -c 10)_$(echo $selected | shasum -a 256 | head -c 4)" tmux_running=$(pgrep tmux) if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then diff --git a/tmux/tmux-ssher b/tmux/tmux-ssher index 8427fd9..e48dd3b 100755 --- a/tmux/tmux-ssher +++ b/tmux/tmux-ssher @@ -1,25 +1,77 @@ #!/usr/bin/env bash -if [[ $# -eq 1 ]]; then - selected=$1 -else - selected="$(grep 'Host .*' ~/.ssh/config | grep -v '*' | sed 's/Host \(.*\)/\1/' | fzf --cycle --bind 'tab:toggle-up,btab:toggle-down')" -fi +function _preview { + file=$1; shift + host=$1; shift -if [[ -z $selected ]]; then - exit 0 -fi + j=$(cat $file \ + | jc --ssh-conf \ + | jq -r ".[] \ + | select(.host == \"$host\") \ + | .jumps = (if (.proxyjump | type == \"array\" and length > 0) then (.proxyjump | join(\",\")) else \"null\" end)" \ + ) -selected_name="ssh_$selected" -tmux_running=$(pgrep tmux) + user=$(jq -r '.user' <<<$j) + port=$(jq -r '.port' <<<$j) + hostname=$(jq -r '.hostname' <<<$j) + jumps=$(jq -r '.jumps' <<<$j) -if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then - tmux new-session -s $selected_name -c $selected ssh "$selected" - exit 0 -fi -if ! tmux has-session -t=$selected_name 2> /dev/null; then - tmux new-session -ds $selected_name -c $selected ssh "$selected" -fi + echo "$(cat </dev/null \ + && echo "Host is reachable!" \ + || echo "Host is not reachable!" +} + +export -f _preview + +function main() { + if [[ $# -eq 1 ]]; then + selected=$1 + else + selected="$(rg -N --no-heading 'Host .*' ~/.ssh \ + | sed 's/Host \(.*\)/\1/' \ + | SHELL=$(which bash) fzf --cycle \ + --bind 'tab:toggle-up,btab:toggle-down' \ + --delimiter ':' \ + --with-nth 2 \ + --preview "_preview {1} {2}" \ + | cut -d ":" -f2 + )" + fi + + if [[ -z $selected ]]; then + exit 0 + fi + + selected_name="ssh_$selected" + tmux_running=$(pgrep tmux) + + if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then + tmux new-session -s $selected_name -c $selected ssh "$selected" + exit 0 + fi + + if ! tmux has-session -t=$selected_name 2> /dev/null; then + tmux new-session -ds $selected_name -c $selected ssh "$selected" + fi + + tmux switch-client -t $selected_name +} + +main "$@"