From c32613020984907c299e73fae6daf21bdf3d4d3a Mon Sep 17 00:00:00 2001 From: Gregory Pakosz Date: Fri, 29 Dec 2017 22:11:09 +0100 Subject: [PATCH] refactored #{username}, #{username_ssh}, #{hostname} and #{hostname_ssh} - improved and extracted pane current command introspection into the _tty_info() helper - added Mosh support to #{username_ssh} and #{hostname_ssh}. Fixes #127 --- .tmux.conf | 132 ++++++++++++++++++++++++++++++++--------------------- README.md | 16 +++---- 2 files changed, 89 insertions(+), 59 deletions(-) diff --git a/.tmux.conf b/.tmux.conf index b650e6f..503f6b8 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -453,60 +453,87 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration' # set -g '@battery_percentage' "$battery_percentage" # } # +# _tty_info() { +# tty="${1##/dev/}" +# uname -s | grep -q "CYGWIN" && cygwin=true +# +# if [ x"$cygwin" = x"true" ]; then +# ps -af | tail -n +2 | awk -v tty="$tty" ' +# $4 == tty { user[$2] = $1; child[$3] = $2 } +# END { +# for (i in user) +# { +# if (!(i in child)) +# { +# file = "/proc/" i "/cmdline"; getline command < file; close(file) +# gsub(/\0/, " ", command) +# print i, user[i], command +# exit +# } +# } +# } +# ' +# else +# ps -t "$tty" -o user= -o pid= -o ppid= -o command= | awk ' +# { user[$2] = $1; child[$3] = $2; for (i = 4 ; i <= NF; ++i) command[$2] = i > 4 ? command[$2] FS $i : $i } +# END { +# for (i in user) +# { +# if (!(i in child)) +# { +# print i, user[i], command[i] +# exit +# } +# } +# } +# ' +# fi +# } +# +# _ssh_or_mosh_args() { +# args=$(printf '%s' "$1" | awk '/ssh/ && !/vagrant ssh/ && !/autossh/ && !/-W/ { $1=""; print $0; exit }') +# if [ -z "$args" ]; then +# args=$(printf '%s' "$1" | grep 'mosh-client' | sed -E -e 's/.*mosh-client -# (.*)\|.*$/\1/' -e 's/-[^ ]*//g' -e 's/\d:\d//g') +# fi +# +# printf '%s' "$args" +# } +# # _username() { # tty=${1:-$(tmux display -p '#{pane_tty}')} # ssh_only=$2 -# # shellcheck disable=SC2039 -# if [ x"$OSTYPE" = x"cygwin" ]; then -# pid=$(ps -a | awk -v tty="${tty##/dev/}" '$5 == tty && /ssh/ && !/vagrant ssh/ && !/autossh/ && !/-W/ { print $1 }') -# [ -n "$pid" ] && ssh_parameters=$(tr '\0' ' ' < "/proc/$pid/cmdline" | sed 's/^ssh //') -# else -# ssh_parameters=$(ps -t "$tty" -o command= | awk '/ssh/ && !/vagrant ssh/ && !/autossh/ && !/-W/ { $1=""; print $0; exit }') -# fi -# if [ -n "$ssh_parameters" ]; then +# +# tty_info=$(_tty_info "$tty") +# command=$(printf '%s' "$tty_info" | cut -d' ' -f3-) +# +# ssh_or_mosh_args=$(_ssh_or_mosh_args "$command") +# if [ -n "$ssh_or_mosh_args" ]; then # # shellcheck disable=SC2086 -# username=$(ssh -G $ssh_parameters 2>/dev/null | awk 'NR > 2 { exit } ; /^user / { print $2 }') +# username=$(ssh -G $ssh_or_mosh_args 2>/dev/null | awk 'NR > 2 { exit } ; /^user / { print $2 }') # # shellcheck disable=SC2086 -# [ -z "$username" ] && username=$(ssh -T -o ControlPath=none -o ProxyCommand="sh -c 'echo %%username%% %r >&2'" $ssh_parameters 2>&1 | awk '/^%username% / { print $2; exit }') +# [ -z "$username" ] && username=$(ssh -T -o ControlPath=none -o ProxyCommand="sh -c 'echo %%username%% %r >&2'" $ssh_or_mosh_args 2>&1 | awk '/^%username% / { print $2; exit }') # else # if ! _is_enabled "$ssh_only"; then -# # shellcheck disable=SC2039 -# if [ x"$OSTYPE" = x"cygwin" ]; then -# username=$(whoami) -# else -# username=$(ps -t "$tty" -o user= -o pid= -o ppid= -o command= | awk ' -# !/ssh/ { user[$2] = $1; ppid[$3] = 1 } -# END { -# for (i in user) -# if (!(i in ppid)) -# { -# print user[i] -# exit -# } -# } -# ') -# fi +# username=$(printf '%s' "$tty_info" | cut -d' ' -f2) # fi # fi # -# echo "$username" +# printf '%s' "$username" # } # # _hostname() { # tty=${1:-$(tmux display -p '#{pane_tty}')} # ssh_only=$2 -# # shellcheck disable=SC2039 -# if [ x"$OSTYPE" = x"cygwin" ]; then -# pid=$(ps -a | awk -v tty="${tty##/dev/}" '$5 == tty && /ssh/ && !/vagrant ssh/ && !/autossh/ && !/-W/ { print $1 }') -# [ -n "$pid" ] && ssh_parameters=$(tr '\0' ' ' < "/proc/$pid/cmdline" | sed 's/^ssh //') -# else -# ssh_parameters=$(ps -t "$tty" -o command= | awk '/ssh/ && !/vagrant ssh/ && !/autossh/ && !/-W/ { $1=""; print $0; exit }') -# fi -# if [ -n "$ssh_parameters" ]; then +# +# tty_info=$(_tty_info "$tty") +# command=$(printf '%s' "$tty_info" | cut -d' ' -f3-) +# +# ssh_or_mosh_args=$(_ssh_or_mosh_args "$command") +# if [ -n "$ssh_or_mosh_args" ]; then # # shellcheck disable=SC2086 -# hostname=$(ssh -G $ssh_parameters 2>/dev/null | awk 'NR > 2 { exit } ; /^hostname / { print $2 }') +# hostname=$(ssh -G $ssh_or_mosh_args 2>/dev/null | awk 'NR > 2 { exit } ; /^hostname / { print $2 }') # # shellcheck disable=SC2086 -# [ -z "$hostname" ] && hostname=$(ssh -T -o ControlPath=none -o ProxyCommand="sh -c 'echo %%hostname%% %h >&2'" $ssh_parameters 2>&1 | awk '/^%hostname% / { print $2; exit }') +# [ -z "$hostname" ] && hostname=$(ssh -T -o ControlPath=none -o ProxyCommand="sh -c 'echo %%hostname%% %h >&2'" $ssh_or_mosh_args 2>&1 | awk '/^%hostname% / { print $2; exit }') # #shellcheck disable=SC1004 # hostname=$(echo "$hostname" | awk '\ # { \ @@ -521,7 +548,7 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration' # fi # fi # -# echo "$hostname" +# printf '%s' "$hostname" # } # # _root() { @@ -581,19 +608,22 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration' # _split_window() { # tty=${1:-$(tmux display -p '#{pane_tty}')} # shift -# # shellcheck disable=SC2039 -# if [ x"$OSTYPE" = x"cygwin" ]; then -# pid=$(ps -a | sort -d | awk -v tty="${tty##/dev/}" '$5 == tty && /ssh/ && !/-W/ { print $1; exit 0 }') -# [ -n "$pid" ] && ssh=$(tr '\0' ' ' < "/proc/$pid/cmdline") -# else -# ssh=$(ps -t "$tty" -o command= | sort -d | awk '/ssh/ && !/-W/ { print $0; exit 0 }') -# fi -# if [ -n "$ssh" ]; then -# # shellcheck disable=SC2046 -# tmux split-window "$@" $(echo "$ssh" | sed -e "s/;/\\\\;/g") -# else -# tmux split-window "$@" -# fi +# +# tty_info=$(_tty_info "$tty") +# command=$(printf '%s' "$tty_info" | cut -d' ' -f3-) +# +# case "$command" in +# *mosh-client*) +# # shellcheck disable=SC2046 +# tmux split-window "$@" mosh $(echo "$command" | sed -E -e 's/.*mosh-client -# (.*)\|.*$/\1/') +# ;; +# *ssh*) +# # shellcheck disable=SC2046 +# tmux split-window "$@" $(echo "$command" | sed -e 's/;/\\\\;/g') +# ;; +# *) +# tmux split-window "$@" +# esac # } # # _apply_overrides() { diff --git a/README.md b/README.md index a82ea00..deae8d2 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,7 @@ Features - `C-a` acts as secondary prefix, while keeping default `C-b` prefix - visual theme inspired by [Powerline][] - [maximize any pane to a new window with ` +`][maximize-pane] - - SSH aware username and hostname status line information + - SSH/Mosh aware username and hostname status line information - mouse mode toggle with ` m` - automatic usage of [`reattach-to-user-namespace`][reattach-to-user-namespace] if available @@ -100,7 +100,7 @@ Features - uptime status line information - optional highlight of focused pane (tmux `>= 2.1`) - configurable new windows and panes behavior (optionally retain current path) - - SSH aware split pane (reconnects to remote server, experimental) + - SSH/Mosh aware split pane (reconnects to remote server) - copy to OS clipboard (needs [`reattach-to-user-namespace`][reattach-to-user-namespace] on macOS, `xsel` or `xclip` on Linux) - [Facebook PathPicker][] integration if available @@ -246,9 +246,9 @@ This configuration supports the following builtin variables: - `#{battery_status}`: is battery charging or discharging? - `#{battery_vbar}`: vertical battery charge bar - `#{circled_session_name}`: circled session number, up to 20 - - `#{hostname}`: SSH aware hostname information - - `#{hostname_ssh}`: SSH aware hostname information, blank when no SSH - connection detected + - `#{hostname}`: SSH/Mosh aware hostname information + - `#{hostname_ssh}`: SSH/Mosh aware hostname information, blank when not + connected to a remote server through SSH/Mosh - `#{loadavg}`: load average - `#{pairing}`: is session attached to more than one client? - `#{prefix}`: is prefix being depressed? @@ -258,9 +258,9 @@ This configuration supports the following builtin variables: - `#{uptime_h}`: uptime hours - `#{uptime_m}`: uptime minutes - `#{uptime_s}`: uptime seconds - - `#{username}`: SSH aware username information - - `#{username_ssh}`: SSH aware username information, blank when no SSH - connection detected + - `#{username}`: SSH/Mosh aware username information + - `#{username_ssh}`: SSH aware username information, blank when not connected + to a remote server through SSH/Mosh ### Accessing the macOS clipboard from within tmux sessions