mirror of
1
0
Fork 0

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
This commit is contained in:
Gregory Pakosz 2017-12-29 22:11:09 +01:00
parent ffb1c54769
commit c326130209
2 changed files with 89 additions and 59 deletions

View File

@ -453,60 +453,87 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration'
# set -g '@battery_percentage' "$battery_percentage" # 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() { # _username() {
# tty=${1:-$(tmux display -p '#{pane_tty}')} # tty=${1:-$(tmux display -p '#{pane_tty}')}
# ssh_only=$2 # ssh_only=$2
# # shellcheck disable=SC2039 #
# if [ x"$OSTYPE" = x"cygwin" ]; then # tty_info=$(_tty_info "$tty")
# pid=$(ps -a | awk -v tty="${tty##/dev/}" '$5 == tty && /ssh/ && !/vagrant ssh/ && !/autossh/ && !/-W/ { print $1 }') # command=$(printf '%s' "$tty_info" | cut -d' ' -f3-)
# [ -n "$pid" ] && ssh_parameters=$(tr '\0' ' ' < "/proc/$pid/cmdline" | sed 's/^ssh //') #
# else # ssh_or_mosh_args=$(_ssh_or_mosh_args "$command")
# ssh_parameters=$(ps -t "$tty" -o command= | awk '/ssh/ && !/vagrant ssh/ && !/autossh/ && !/-W/ { $1=""; print $0; exit }') # if [ -n "$ssh_or_mosh_args" ]; then
# fi
# if [ -n "$ssh_parameters" ]; then
# # shellcheck disable=SC2086 # # 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 # # 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 # else
# if ! _is_enabled "$ssh_only"; then # if ! _is_enabled "$ssh_only"; then
# # shellcheck disable=SC2039 # username=$(printf '%s' "$tty_info" | cut -d' ' -f2)
# 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
# fi # fi
# fi # fi
# #
# echo "$username" # printf '%s' "$username"
# } # }
# #
# _hostname() { # _hostname() {
# tty=${1:-$(tmux display -p '#{pane_tty}')} # tty=${1:-$(tmux display -p '#{pane_tty}')}
# ssh_only=$2 # ssh_only=$2
# # shellcheck disable=SC2039 #
# if [ x"$OSTYPE" = x"cygwin" ]; then # tty_info=$(_tty_info "$tty")
# pid=$(ps -a | awk -v tty="${tty##/dev/}" '$5 == tty && /ssh/ && !/vagrant ssh/ && !/autossh/ && !/-W/ { print $1 }') # command=$(printf '%s' "$tty_info" | cut -d' ' -f3-)
# [ -n "$pid" ] && ssh_parameters=$(tr '\0' ' ' < "/proc/$pid/cmdline" | sed 's/^ssh //') #
# else # ssh_or_mosh_args=$(_ssh_or_mosh_args "$command")
# ssh_parameters=$(ps -t "$tty" -o command= | awk '/ssh/ && !/vagrant ssh/ && !/autossh/ && !/-W/ { $1=""; print $0; exit }') # if [ -n "$ssh_or_mosh_args" ]; then
# fi
# if [ -n "$ssh_parameters" ]; then
# # shellcheck disable=SC2086 # # 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 # # 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 # #shellcheck disable=SC1004
# hostname=$(echo "$hostname" | awk '\ # hostname=$(echo "$hostname" | awk '\
# { \ # { \
@ -521,7 +548,7 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration'
# fi # fi
# fi # fi
# #
# echo "$hostname" # printf '%s' "$hostname"
# } # }
# #
# _root() { # _root() {
@ -581,19 +608,22 @@ run 'cut -c3- ~/.tmux.conf | sh -s _apply_configuration'
# _split_window() { # _split_window() {
# tty=${1:-$(tmux display -p '#{pane_tty}')} # tty=${1:-$(tmux display -p '#{pane_tty}')}
# shift # shift
# # shellcheck disable=SC2039 #
# if [ x"$OSTYPE" = x"cygwin" ]; then # tty_info=$(_tty_info "$tty")
# pid=$(ps -a | sort -d | awk -v tty="${tty##/dev/}" '$5 == tty && /ssh/ && !/-W/ { print $1; exit 0 }') # command=$(printf '%s' "$tty_info" | cut -d' ' -f3-)
# [ -n "$pid" ] && ssh=$(tr '\0' ' ' < "/proc/$pid/cmdline") #
# else # case "$command" in
# ssh=$(ps -t "$tty" -o command= | sort -d | awk '/ssh/ && !/-W/ { print $0; exit 0 }') # *mosh-client*)
# fi # # shellcheck disable=SC2046
# if [ -n "$ssh" ]; then # tmux split-window "$@" mosh $(echo "$command" | sed -E -e 's/.*mosh-client -# (.*)\|.*$/\1/')
# # shellcheck disable=SC2046 # ;;
# tmux split-window "$@" $(echo "$ssh" | sed -e "s/;/\\\\;/g") # *ssh*)
# else # # shellcheck disable=SC2046
# tmux split-window "$@" # tmux split-window "$@" $(echo "$command" | sed -e 's/;/\\\\;/g')
# fi # ;;
# *)
# tmux split-window "$@"
# esac
# } # }
# #
# _apply_overrides() { # _apply_overrides() {

View File

@ -92,7 +92,7 @@ Features
- `C-a` acts as secondary prefix, while keeping default `C-b` prefix - `C-a` acts as secondary prefix, while keeping default `C-b` prefix
- visual theme inspired by [Powerline][] - visual theme inspired by [Powerline][]
- [maximize any pane to a new window with `<prefix> +`][maximize-pane] - [maximize any pane to a new window with `<prefix> +`][maximize-pane]
- SSH aware username and hostname status line information - SSH/Mosh aware username and hostname status line information
- mouse mode toggle with `<prefix> m` - mouse mode toggle with `<prefix> m`
- automatic usage of [`reattach-to-user-namespace`][reattach-to-user-namespace] - automatic usage of [`reattach-to-user-namespace`][reattach-to-user-namespace]
if available if available
@ -100,7 +100,7 @@ Features
- uptime status line information - uptime status line information
- optional highlight of focused pane (tmux `>= 2.1`) - optional highlight of focused pane (tmux `>= 2.1`)
- configurable new windows and panes behavior (optionally retain current path) - 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] - copy to OS clipboard (needs [`reattach-to-user-namespace`][reattach-to-user-namespace]
on macOS, `xsel` or `xclip` on Linux) on macOS, `xsel` or `xclip` on Linux)
- [Facebook PathPicker][] integration if available - [Facebook PathPicker][] integration if available
@ -246,9 +246,9 @@ This configuration supports the following builtin variables:
- `#{battery_status}`: is battery charging or discharging? - `#{battery_status}`: is battery charging or discharging?
- `#{battery_vbar}`: vertical battery charge bar - `#{battery_vbar}`: vertical battery charge bar
- `#{circled_session_name}`: circled session number, up to 20 - `#{circled_session_name}`: circled session number, up to 20
- `#{hostname}`: SSH aware hostname information - `#{hostname}`: SSH/Mosh aware hostname information
- `#{hostname_ssh}`: SSH aware hostname information, blank when no SSH - `#{hostname_ssh}`: SSH/Mosh aware hostname information, blank when not
connection detected connected to a remote server through SSH/Mosh
- `#{loadavg}`: load average - `#{loadavg}`: load average
- `#{pairing}`: is session attached to more than one client? - `#{pairing}`: is session attached to more than one client?
- `#{prefix}`: is prefix being depressed? - `#{prefix}`: is prefix being depressed?
@ -258,9 +258,9 @@ This configuration supports the following builtin variables:
- `#{uptime_h}`: uptime hours - `#{uptime_h}`: uptime hours
- `#{uptime_m}`: uptime minutes - `#{uptime_m}`: uptime minutes
- `#{uptime_s}`: uptime seconds - `#{uptime_s}`: uptime seconds
- `#{username}`: SSH aware username information - `#{username}`: SSH/Mosh aware username information
- `#{username_ssh}`: SSH aware username information, blank when no SSH - `#{username_ssh}`: SSH aware username information, blank when not connected
connection detected to a remote server through SSH/Mosh
### Accessing the macOS clipboard from within tmux sessions ### Accessing the macOS clipboard from within tmux sessions