From c00544a404b6b8fa8c871a2c44d535b802612f36 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sun, 23 Oct 2022 22:42:05 -0500 Subject: feat(tmux): start implementing tmux-ssh script --- tmux/.local/bin/timestamp.sh | 72 +++++++++++++++++++++++++++++--------------- tmux/.local/bin/tmux-ssh | 44 +++++++++++++++++++-------- 2 files changed, 80 insertions(+), 36 deletions(-) (limited to 'tmux') diff --git a/tmux/.local/bin/timestamp.sh b/tmux/.local/bin/timestamp.sh index 752c1a5..ff496be 100755 --- a/tmux/.local/bin/timestamp.sh +++ b/tmux/.local/bin/timestamp.sh @@ -3,8 +3,8 @@ SCRIPT="$(basename "$0")" -long='git,sessions::,format:,verbose,help' -short='gs::f:vh' +long='type:,git,sessions::,format:,verbose,help' +short='t:gs::f:vh' if ! opts="$(getopt -o $short -l $long -n "$SCRIPT" -- "$@")"; then exit 1 @@ -24,6 +24,7 @@ help() { $SCRIPT [OPTION ...] [PATH ...] OPTIONS: + -t, --type= Specify type of input -g, --git Check last git commit for timestamp -s, --sessions=[PATH] Check nvim sessions directory for existing session files. (DEFAULT="$XDG_DATA_HOME/nvim/sessions") @@ -62,9 +63,11 @@ err_help() { } verbose=0 +type='' git=false sessions=false sessions_dir="$XDG_DATA_HOME/nvim/sessions" +histfile="${HISTFILE:-$XDG_STATE_HOME/zsh/history}" format="{}" while true; do case "$1" in @@ -76,6 +79,15 @@ while true; do verbose=$((verbose + 1)) shift ;; + -t | --type) + case "$2" in + s*) type="ssh" ;; + f*) type="file" ;; + d*) type="directory" ;; + *) type="$2" ;; + esac + shift 2 + ;; -e | --git) git=true shift @@ -109,34 +121,46 @@ if [ "$#" -eq 0 ]; then fi while [ $# -gt 0 ]; do - if [ ! -d "$1" ]; then - say_err "$1" "No such file or directory" - shift - continue - fi - ts_max="0" - if ts=$(stat -c "%Y" "$1"); then - say_verbose "stat '$1': $ts" - if [ "$ts" -gt "$ts_max" ]; then - ts_max=$ts + case "$type" in + ssh) + if ts=$(grep -P "^: \d+:\d;ssh.* $1" "$histfile" | tail -1 | cut -d: -f2 | sed 's/^\s*//'); then + say_verbose "ssh '$1': $ts" + if [ -n "$ts" ] && [ "$ts" -gt "$ts_max" ]; then + ts_max=$ts + fi + fi + ;; + file | directory) + if [ ! -d "$1" ]; then + say_err "$1" "No such file or directory" + shift + continue fi - fi - if $git && ts=$(git -C "$1" --no-pager log -1 --all --format="%at" 2>/dev/null); then - say_verbose "git '$1': $ts" - if [ "$ts" -gt "$ts_max" ]; then - ts_max=$ts + if ts=$(stat -c "%Y" "$1"); then + say_verbose "stat '$1': $ts" + if [ "$ts" -gt "$ts_max" ]; then + ts_max=$ts + fi fi - fi - pattern=$(printf %s\\n "$1" | sed 's|/|.{1,2}|g') - if $sessions && ts=$(fd "$pattern" "$sessions_dir" -1 --type=f --max-depth=1 -x stat -c "%Y" {} 2>/dev/null); then - say_verbose "session '$1': $ts" - if [ "$ts" -gt "$ts_max" ]; then - ts_max=$ts + if $git && ts=$(git -C "$1" --no-pager log -1 --all --format="%at" 2>/dev/null); then + say_verbose "git '$1': $ts" + if [ "$ts" -gt "$ts_max" ]; then + ts_max=$ts + fi fi - fi + + pattern=$(printf %s\\n "$1" | sed 's|/|.{1,2}|g') + if $sessions && ts=$(fd "$pattern" "$sessions_dir" -1 --type=f --max-depth=1 -x stat -c "%Y" {} 2>/dev/null); then + say_verbose "session '$1': $ts" + if [ "$ts" -gt "$ts_max" ]; then + ts_max=$ts + fi + fi + ;; + esac printf %s\\n "$format" | sed "s|{}|$ts_max|g" | sed "s|{1}|$1|g" shift diff --git a/tmux/.local/bin/tmux-ssh b/tmux/.local/bin/tmux-ssh index 2dedc53..0bbdc7e 100755 --- a/tmux/.local/bin/tmux-ssh +++ b/tmux/.local/bin/tmux-ssh @@ -1,18 +1,38 @@ #!/bin/sh -setopt localoptions nonomatch -tmp="$(mktemp)" +#!/bin/sh +# shellcheck disable=2016,2089 + +if [ "$#" -eq 0 ]; then + ATTACHED=$(tmux display -p '#{session_path}') + lines=$(tmux display -p "#{pane_height}") + columns=$(tmux display -p "#{pane_width}") + WIDTH=$([ "$columns" -gt "20" ] && printf %s "20" || printf %s "90%") + HEIGHT=$([ "$lines" -gt "15" ] && printf %s "15" || printf %s "90%") -tail -n +1 ~/.ssh/config ~/.ssh/config.d/* /etc/ssh/ssh_config 2>/dev/null | grep -i '^\s*host ' | - awk '{for (i = 2; i <= NF; i++) print "ssh_config " $i}' | grep -v '[*?%]' >"$tmp" + # shellcheck disable=2046 + set -- $( + grep -P '^[Hh]ost ([^*]+)$' "$HOME/.ssh/config" | grep -Po ' \K(\w+)' | + sed "\|^$HOST\$|d" | sed "\|^${ATTACHED#SSH:}\$|d" | sort -u | + timestamp.sh --type="ssh" --format="{}:{1}" | sort -r | cut -d':' -f2 | + fzf-tmux -p "$WIDTH,$HEIGHT" -- --multi --print-query -d/ --with-nth -1 | + tr -s '\n' ' ' + ) && [ $# -gt 1 ] && shift +fi -grep -oE '^[[a-z0-9.,:-]+' ~/.ssh/known_hosts | tr ',' '\n' | tr -d '[' | awk '{ print "known_hosts " $1 }' >"$tmp" -grep -v '^\s*\(#\|$\)' /etc/hosts | command grep -Fv '0.0.0.0' >"$tmp" -cat "$tmp" exit 0 -while IFS= read -r host; do - hosts="$hosts $host" -done <"$tmp" -rm "$tmp" +while [ $# -gt 0 ]; do + ssh_host="$1" + shift + + name="SSH:$ssh_host" + if ! tmux has-session -t "$name" 2>/dev/null; then + tmux new-session -ds "$name" ssh -t "$ssh_host" 'tmux new -As $USER' + fi +done -printf %s\\n "$hosts" | xargs -I{} -d ' ' printf %s\\n {} +if [ -z "$TMUX" ] && [ -z "$name" ]; then + tmux attach-session -t "$name" +elif tmux has-session -t="$name" 2>/dev/null; then + tmux switch-client -t "$name" +fi -- cgit v1.2.3-70-g09d2