diff --git a/README.md b/README.md index de78f0f..e559bbb 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,22 @@ Tmux plugin that enables displaying hostname and user of the current pane in you > [!IMPORTANT] > Replaces the `#H` default format variable -## Usage + +Currently working for gcloud and mosh connections + +### Usage + ### Basics + +Here are examples in `.tmux.conf`: + - `#H` (`#{hostname}`) will be the hostname of your current path - `#{hostname_short}` will be the short hostname of your current path (up to the first dot) - `#U` (`#{username}`) will be current user name + ### Remote connection info Plugin can detect pane has remote shell connection in several states: @@ -27,14 +35,27 @@ Besides `#{hostname}` and `#{username}` there are more usefull format variables: - `#{pane_ssh_connected}` will be set to 1 if the currently selected pane has an active connection. (Useful for `#{?#{pane_ssh_connected},ssh,no-ssh}` which will evaluate to `ssh` if there is an active remote session in the currently selected pane and `no-ssh` otherwise.) - `#{pane_ssh_connect}` if an open remote session exists will show the connection info in `"username@hostname:port"` format, otherwise it will be empty. -### Example +### Examples ```tmux set -g status-left " #[bg=blue]#U#[bg=red]@#H#{?#{pane_ssh_port},:#{pane_ssh_port},}#[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`: ```tmux @@ -49,7 +70,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`: 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 diff --git a/scripts/pane_ssh_connected.sh b/scripts/pane_ssh_connected.sh index dce5ef8..bd7629d 100755 --- a/scripts/pane_ssh_connected.sh +++ b/scripts/pane_ssh_connected.sh @@ -1,11 +1,21 @@ #!/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() { - ssh_connected && echo 1 || echo 0 + + if ssh_connected; then + echo "SSH" + elif gcloud_connected; then + echo "GC" + elif mosh_connected; then + echo "MOSH" + else + echo "" + fi + } main diff --git a/scripts/port.sh b/scripts/port.sh index 32ba557..279d591 100755 --- a/scripts/port.sh +++ b/scripts/port.sh @@ -1,11 +1,19 @@ #!/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() { - get_info port + + if ssh_connected; then + get_info "port" + elif gcloud_connected; then + get_info "port" + elif mosh_connected; then + get_info "port" + fi + } main diff --git a/scripts/shared.sh b/scripts/shared.sh index 60d36f0..3d96e2c 100644 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -1,5 +1,27 @@ #!/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 +} + +set_tmux_option() { + 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*//') + get_info() { local host port user local cmd=$(__current_pane_command) @@ -46,6 +68,7 @@ containered() { __containered_cmd "$cmd" } + __ssh_cmd() { [[ $1 =~ (^|^[[:blank:]]*|/?)ssh[[:blank:]] ]] || [[ $1 =~ (^|^[[:blank:]]*|/?)sshpass[[:blank:]] ]] } @@ -54,6 +77,138 @@ __containered_cmd() { [[ $1 =~ (^|^[[:blank:]]*|/?)docker[[:blank:]] ]] || [[ $1 =~ (^|^[[:blank:]]*|/?)podman[[:blank:]] ]] } + +get_ssh_user() { + local ssh_user=$(whoami) + + for ssh_config in $(awk ' + $1 == "Host" { + gsub("\\.", "\\.", $2); + gsub("\\*", ".*", $2); + host = $2; + next; + } + $1 == "User" { + $1 = ""; + sub( /^[[:space:]]*/, "" ); + printf "%s|%s\n", host, $0; + }' .ssh/config); do + local host_regex=${ssh_config%|*} + local host_user=${ssh_config#*|} + if [[ "$1" =~ $host_regex ]]; then + ssh_user=$host_user + break + fi + done + + echo $ssh_user +} + +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 port=$(parse_ssh_port "$cmd") + + 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" + ;; + esac +} + +get_remote_gcloud() { + 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 + + 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() { + 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() { + if ssh_connected; then + 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() { + local cmd=$(tmux display-message -p "#{pane_current_command}") + [ $cmd = "ssh" ] || [ $cmd = "sshpass" ] +======= __current_pane_command() { local ppid=$(tmux display-message -p "#{pane_pid}") local pid command cmd @@ -129,3 +284,13 @@ __get_hostname() { __get_hostname_short() { command -v hostname > /dev/null && hostname --short || echo "${HOSTNAME%%.*}" } + +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-client" ] +}