diff --git a/bin/ssh-menu b/bin/ssh-menu index 0823a54..973f906 100755 --- a/bin/ssh-menu +++ b/bin/ssh-menu @@ -4,72 +4,50 @@ function _ssh_menu_preview { file=$1; shift host=$1; shift - 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 \ - )" \ - ) - - user=$(jq -r '.user' <<<$j) - port=$(jq -r '.port' <<<$j) - hostname=$(jq -r '.hostname' <<<$j) - jumps=$(jq -r '.jumps' <<<$j) - - - echo "$(cat <&1 >/dev/null; then - host $hostname + # resolve hostname for reachability check + hostname=$(awk -v h="$host" ' + $0 == "Host " h { found=1; next } + found && /^[^ \t]/ { exit } + found && /HostName/ { print $2 } + ' "$file") + + if command -v host &>/dev/null; then + host "${hostname:-$host}" echo "" fi - ping -c 1 -W 1 $hostname &>/dev/null \ - && echo "Host is reachable!" \ - || echo "Host is not reachable!" + ping -c 1 -W 1 "${hostname:-$host}" &>/dev/null \ + && echo "reachable" \ + || echo "not reachable" } export -f _ssh_menu_preview -function ssh-menu { - selected="$(rg -N --no-heading '^Host .*' ~/.ssh \ +function _ssh_menu_select { + rg -N --no-heading '^Host .*' ~/.ssh \ | sed 's/Host \(.*\)/\1/' | sort \ - | SHELL=$(which bash) fzf --cycle \ - --bind 'tab:toggle-up,btab:toggle-down' \ + | SHELL=$(which bash) fzf \ --delimiter ':' \ --with-nth 2 \ --preview "_ssh_menu_preview {1} {2}" \ --preview-label "ssh config info" \ - --header "Navigate with ARROW KEYS or TAB/S-TAB. Select with ENTER." \ - --border "double" \ --border-label "ssh-menu" \ - --ansi \ - --highlight-line \ | cut -d ":" -f2 - )" - - if [[ -z $selected ]]; then - return - fi - - ssh "$selected" } -ssh-menu +case "${1:-}" in + -s) _ssh_menu_select ;; + *) + selected="$(_ssh_menu_select)" + [[ -n $selected ]] && ssh "$selected" + ;; +esac diff --git a/tmux/scripts/tmux-new-project b/tmux/scripts/tmux-new-project index 1b1dabc..759d4cf 100755 --- a/tmux/scripts/tmux-new-project +++ b/tmux/scripts/tmux-new-project @@ -23,7 +23,7 @@ echo "# $project_name" >>"$project/README.md" gitignore_template=$( find "$GGROOT"/github.com/github/gitignore/*.gitignore -exec basename {} \; \ | sed 's/\(.*\)\.gitignore/\1/' \ - | fzf --cycle --bind 'tab:toggle-up,btab:toggle-down' + | fzf ) if [[ -n $gitignore_template ]]; then @@ -36,7 +36,7 @@ fi license_template=$( find "$GGROOT"/git.janezic.dev/janezicmatej/license/*.license -exec basename {} \; \ | sed 's/\(.*\)\.license/\1/' \ - | fzf --cycle --bind 'tab:toggle-up,btab:toggle-down' + | fzf ) if [[ -n $license_template ]]; then diff --git a/tmux/scripts/tmux-sessionizer b/tmux/scripts/tmux-sessionizer index a2787d3..a0c4c96 100755 --- a/tmux/scripts/tmux-sessionizer +++ b/tmux/scripts/tmux-sessionizer @@ -4,13 +4,8 @@ if [[ $# -eq 1 ]]; then selected=$1 else selected=$(ggman ls \ - | fzf --cycle \ - --bind 'tab:toggle-up,btab:toggle-down' \ - --header "Navigate with ARROW KEYS or TAB/S-TAB. Select with ENTER." \ - --border "double" \ + | fzf \ --border-label "tmux-sessionizer" \ - --ansi \ - --highlight-line \ ) fi diff --git a/tmux/scripts/tmux-ssher b/tmux/scripts/tmux-ssher index 102892a..07e5ec1 100755 --- a/tmux/scripts/tmux-ssher +++ b/tmux/scripts/tmux-ssher @@ -1,76 +1,24 @@ #!/usr/bin/env bash -function _preview { - file=$1; shift - host=$1; shift +if [[ $# -eq 1 ]]; then + selected=$1 +else + selected="$(~/.config/bin/ssh-menu -s)" +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)" \ - ) +if [[ -z $selected ]]; then + exit 0 +fi - user=$(jq -r '.user' <<<"$j") - port=$(jq -r '.port' <<<"$j") - hostname=$(jq -r '.hostname' <<<"$j") - jumps=$(jq -r '.jumps' <<<"$j") +selected_name="ssh_$selected" +if [[ -z $TMUX ]] && ! tmux list-sessions 2>/dev/null; then + tmux new-session -s "$selected_name" ssh "$selected" + exit 0 +fi - echo "$(cat </dev/null; then + tmux new-session -ds "$selected_name" ssh "$selected" +fi -command: - ssh $user@$hostname$([[ $port != "null" ]] && echo -n " -p $port")$([[ $jumps != "null" ]] && echo -n " -J $jumps") -EOF)" - - echo "" - - host "$hostname" - - echo "" - - ping -c 1 -W 1 "$hostname" &>/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" - - if [[ -z $TMUX ]] && ! tmux list-sessions 2>/dev/null; then - tmux new-session -s "$selected_name" ssh "$selected" - exit 0 - fi - - if ! tmux has-session -t="$selected_name" 2>/dev/null; then - tmux new-session -ds "$selected_name" ssh "$selected" - fi - - tmux switch-client -t "$selected_name" -} - -main "$@" +tmux switch-client -t "$selected_name" diff --git a/zsh/.zshenv b/zsh/.zshenv index 5460e20..d510fd0 100644 --- a/zsh/.zshenv +++ b/zsh/.zshenv @@ -22,3 +22,6 @@ export NAME="Matej Janežič" # starship nest config into a folder, default is ~/.config/starship.toml export STARSHIP_CONFIG="$XDG_CONFIG_HOME/starship/config.toml" + +# fzf +export FZF_DEFAULT_OPTS="--cycle --bind 'tab:toggle-up,btab:toggle-down' --pointer '>' --color '16' --border 'double' --ansi --highlight-line --header 'Navigate with ARROW KEYS or TAB/S-TAB. Select with ENTER.'"