From 0228fcf19de9857c00a0b6f18eebb181d5d7ce83 Mon Sep 17 00:00:00 2001 From: JG Date: Thu, 27 Feb 2025 16:20:37 -0500 Subject: [PATCH 1/7] add mor options to pane_ssh_connected.sh added gcloud and a holder for mosh --- scripts/pane_ssh_connected.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/scripts/pane_ssh_connected.sh b/scripts/pane_ssh_connected.sh index 1c8cf39..610e737 100755 --- a/scripts/pane_ssh_connected.sh +++ b/scripts/pane_ssh_connected.sh @@ -1,14 +1,18 @@ #!/usr/bin/env bash -CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source $CURRENT_DIR/shared.sh main() { if ssh_connected; then - echo 1 + echo "SSH" + elif gcloud_connected; then + echo "GC" + elif mosh_connected; then + echo "MOSH" else - echo 0 + echo "" fi } From 07c0235aa555c9857de2f5b5063f798dc8d05697 Mon Sep 17 00:00:00 2001 From: JG Date: Thu, 27 Feb 2025 16:24:01 -0500 Subject: [PATCH 2/7] add gcloud to shared.sh rename functions and add functions for detect gcloud connections --- scripts/shared.sh | 118 +++++++++++++++++++++++++++++++--------------- 1 file changed, 81 insertions(+), 37 deletions(-) diff --git a/scripts/shared.sh b/scripts/shared.sh index 063ef21..1449d74 100644 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -1,25 +1,25 @@ #!/usr/bin/env bash get_tmux_option() { - local option=$1 - local default_value=$2 - local option_value=$(tmux show-option -gqv "$option") - if [ -z "$option_value" ]; then - echo "$default_value" - else - echo "$option_value" - fi + local option=$1 + local default_value=$2 + local option_value=$(tmux show-option -gqv "$option") + if [ -z "$option_value" ]; then + echo "$default_value" + else + echo "$option_value" + fi } set_tmux_option() { - local option=$1 - local value=$2 - tmux set-option -gq "$option" "$value" + local option=$1 + local value=$2 + tmux set-option -gq "$option" "$value" } parse_ssh_port() { # If there is a port get it - local port=$(echo $1|grep -Eo '\-p\s*([0-9]+)'|sed 's/-p\s*//') + local port=$(echo $1 | grep -Eo '\-p\s*([0-9]+)' | sed 's/-p\s*//') if [ -z $port ]; then local port=22 @@ -31,10 +31,10 @@ parse_ssh_port() { get_ssh_user() { local ssh_user=$(whoami) - for ssh_config in `awk ' + for ssh_config in $(awk ' $1 == "Host" { - gsub("\\\\.", "\\\\.", $2); - gsub("\\\\*", ".*", $2); + gsub("\\.", "\\.", $2); + gsub("\\*", ".*", $2); host = $2; next; } @@ -42,7 +42,7 @@ get_ssh_user() { $1 = ""; sub( /^[[:space:]]*/, "" ); printf "%s|%s\n", host, $0; - }' .ssh/config`; do + }' .ssh/config); do local host_regex=${ssh_config%|*} local host_user=${ssh_config#*|} if [[ "$1" =~ $host_regex ]]; then @@ -54,50 +54,94 @@ get_ssh_user() { echo $ssh_user } -get_remote_info() { +get_remote_ssh() { local command=$1 # First get the current pane command pid to get the full command with arguments - local cmd=$({ pgrep -flaP `tmux display-message -p "#{pane_pid}"` ; ps -o command -p `tmux display-message -p "#{pane_pid}"` ; } | xargs -I{} echo {} | grep ssh | sed -E 's/^[0-9]*[[:blank:]]*ssh //') + local cmd=$({ + pgrep -flaP $(tmux display-message -p "#{pane_pid}") + ps -o command -p $(tmux display-message -p "#{pane_pid}") + } | xargs -I{} echo {} | grep ssh | sed -E 's/^[0-9]*[[:blank:]]*ssh //') local port=$(parse_ssh_port "$cmd") - local cmd=$(echo $cmd|sed 's/\-p\s*'"$port"'//g') - local user=$(echo $cmd | awk '{print $NF}'|cut -f1 -d@) - local host=$(echo $cmd | awk '{print $NF}'|cut -f2 -d@) + local cmd=$(echo $cmd | sed 's/\-p\s*'"$port"'//g') + local user=$(echo $cmd | awk '{print $NF}' | cut -f9 -d@) + local host=$(echo $cmd | awk '{print $NF}' | cut -f1 -d@) if [ $user == $host ]; then local user=$(get_ssh_user $host) fi case "$1" in - "whoami") - echo $user - ;; - "hostname") - echo $host - ;; - "port") - echo $port - ;; - *) - echo "$user@$host:$port" - ;; + "whoami") + echo $user + ;; + "hostname") + echo $host + ;; + "port") + echo $port + ;; + *) + echo "$user@$host:$port" + ;; esac } +get_remote_gcloud() { + # local command=$1 + local parent=$(pgrep -P $(tmux display-message -p "#{pane_pid}")) + local grandparent=$(pgrep -P $parent) # user + local greatgrandparent=$(pgrep -P $grandparent) #port and host + + local user=$(ps -o command -p $grandparent | awk '{print $33}' | cut -f1 -d@) + local host=$(ps -o command -p $greatgrandparent | awk '{print $7}') + local port=$(ps -o command -p $greatgrandparent | awk '{print $8}') + + case "$1" in + "whoami") + echo $user + ;; + "hostname") + echo $host + ;; + "port") + echo $port + ;; + *) + echo "$user@$host:$port" + ;; + esac +} + +get_remote_mosh() { + echo "$user@$host:$port" +} + get_info() { - # If command is ssh do some magic if ssh_connected; then - echo $(get_remote_info $1) + echo $(get_remote_ssh $1) + elif mosh_connected; then + echo $(get_remote_mosh $1) + elif gcloud_connected; then + echo $(get_remote_gcloud $1) else echo $($1) fi } ssh_connected() { - # Get current pane command local cmd=$(tmux display-message -p "#{pane_current_command}") - [ $cmd = "ssh" ] || [ $cmd = "sshpass" ] } + +gcloud_connected() { + local cmd=$(tmux display-message -p "#{pane_current_command}") + [ $cmd = "python" ] +} + +mosh_connected() { + local cmd=$(tmux display-message -p "#{pane_current_command}") + [ $cmd = "mosh" ] +} From bdac2cb19422f059a8c5e0d9182fbd893859cf1b Mon Sep 17 00:00:00 2001 From: JG Date: Thu, 27 Feb 2025 16:42:26 -0500 Subject: [PATCH 3/7] add examples to README.md --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c4bc5b..6d62dca 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,22 @@ Replaces the `#H` format and adds a `#U` format option. - `#{pane_ssh_port}` if an open ssh session will show the connection port, otherwise it will be empty. - `#{pane_ssh_connected}` will be set to 1 if the currently selected pane has an active ssh connection. (Useful for `#{?#{pane_ssh_connected},ssh,no-ssh}` which will evaluate to `ssh` if there is an active ssh in the currently selected pane and `no-ssh` otherwise.) -Here's the example in `.tmux.conf`: +Here are examples in `.tmux.conf`: ```bash set -g status-right '#[fg=cyan,bold] #U@#H #[default]#[fg=blue]#(tmux display-message -p "#{pane_current_path}" | sed "s#$HOME#~#g") #[fg=red]%H:%M %d-%b-%y#[default]' ``` +```bash +set -ga status-left "#[bg=#{@thm_bg},fg=#{@thm_green}] #{?#{pane_ssh_connected},#[fg=#{@thm_red}]  #{hostname_short} , #{pane_current_command}}" #changes the current process for the remote hostname if connected (and the color) + +set -ga status-left "#[bg=#{@thm_bg},fg=#{@thm_mauve}] #{?#{pane_ssh_connected}, , #{=/-32/...:#{s|$USER|~|:#{b:pane_current_path}}} |}" #shows the local path only if not connected + +set -ga status-right "#[bg=#{@thm_bg},fg=#{@thm_blue}] #{?#{pane_ssh_connected},, #{pane_current_path}} " #shows the current full path only if not connected + +set -ga status-right "#[bg=#{@thm_bg},fg=#{@thm_blue}] #{?#{pane_ssh_connected},#[fg=#{@thm_red}]  #U ,#[fg=#{@thm_blue}]  #U }" #shows the current or remote user, different colors for awareness +``` + ### Installation with [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) (recommended) Add plugin to the list of TPM plugins in `.tmux.conf`: From afa1a29a902afbc0ae87f3c7a35d735aa6f2e6dd Mon Sep 17 00:00:00 2001 From: JG Date: Thu, 27 Feb 2025 16:50:45 -0500 Subject: [PATCH 4/7] Update port.sh for gcloud --- scripts/port.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/port.sh b/scripts/port.sh index ff0885c..ad099b4 100755 --- a/scripts/port.sh +++ b/scripts/port.sh @@ -1,12 +1,16 @@ #!/usr/bin/env bash -CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source $CURRENT_DIR/shared.sh main() { if ssh_connected; then - get_info "port" + get_info "port" + elif gcloud_connected; then + get_info "port" + elif mosh_connected; then + get_info "port" fi } From 33cc2564da623f8e02b27e4b314efcab4d1b7c52 Mon Sep 17 00:00:00 2001 From: JG Date: Tue, 4 Mar 2025 15:07:39 -0500 Subject: [PATCH 5/7] Create ip.sh return ip, for now only on mosh --- scripts/ip.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 scripts/ip.sh diff --git a/scripts/ip.sh b/scripts/ip.sh new file mode 100644 index 0000000..ba17183 --- /dev/null +++ b/scripts/ip.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +source $CURRENT_DIR/shared.sh + +main() { + get_info "ip" +} + +main From 5330a003d0a56936639612cd3b3eaa0fd0958ea5 Mon Sep 17 00:00:00 2001 From: JG Date: Tue, 4 Mar 2025 15:11:24 -0500 Subject: [PATCH 6/7] adding mosh functionality shared.sh --- scripts/shared.sh | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/scripts/shared.sh b/scripts/shared.sh index 1449d74..ed975e9 100644 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -90,8 +90,8 @@ get_remote_ssh() { } get_remote_gcloud() { - # local command=$1 - local parent=$(pgrep -P $(tmux display-message -p "#{pane_pid}")) + local pane_pid=$(tmux display-message -p "#{pane_pid}") + local parent=$(pgrep -P $pane_pid) local grandparent=$(pgrep -P $parent) # user local greatgrandparent=$(pgrep -P $grandparent) #port and host @@ -116,7 +116,31 @@ get_remote_gcloud() { } get_remote_mosh() { - echo "$user@$host:$port" + local pane_pid=$(tmux display-message -p "#{pane_pid}") + local parent=$(pgrep -P $pane_pid) + + local user=$(ps -o command -p $parent | awk '{print $3}' | cut -f1 -d@) + local host=$(ps -o command -p $parent | awk '{print $3}' | cut -f2 -d@) + local port=$(ps -o command -p $parent | awk '{print $6}') + local ip=$(ps -o command -p $parent | awk '{print $5}') + + case "$1" in + "whoami") + echo $user + ;; + "hostname") + echo $host + ;; + "port") + echo $port + ;; + "ip") + echo $ip + ;; + *) + echo "$user@$host:$port" + ;; + esac } get_info() { @@ -143,5 +167,5 @@ gcloud_connected() { mosh_connected() { local cmd=$(tmux display-message -p "#{pane_current_command}") - [ $cmd = "mosh" ] + [ $cmd = "mosh-client" ] } From e407a5aa46f6bab90829f0be6a57c80ec7aa0e13 Mon Sep 17 00:00:00 2001 From: JG Date: Tue, 4 Mar 2025 15:13:25 -0500 Subject: [PATCH 7/7] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d62dca..91ed08f 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ Tmux plugin that enables displaying hostname and user of the current pane in you Replaces the `#H` format and adds a `#U` format option. +Currently working for gcloud and mosh connections + ### Usage - `#H` will be the hostname of your current path. If there is an ssh session opened, the ssh hostname will show instead of the local one. @@ -45,7 +47,7 @@ Hit `prefix + I` to fetch the plugin and source it. Clone the repo: - $ git clone https://github.com/soyuka/tmux-current-pane-hostname ~/clone/path + $ git clone https://github.com/jacostag/tmux-current-pane-hostname ~/clone/path Add this line to the bottom of `.tmux.conf`: