Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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`:

Expand Down
11 changes: 11 additions & 0 deletions scripts/ip.sh
Original file line number Diff line number Diff line change
@@ -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
14 changes: 12 additions & 2 deletions scripts/pane_ssh_connected.sh
Original file line number Diff line number Diff line change
@@ -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
12 changes: 10 additions & 2 deletions scripts/port.sh
Original file line number Diff line number Diff line change
@@ -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
165 changes: 165 additions & 0 deletions scripts/shared.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
#!/usr/bin/env bash


get_tmux_option() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated, see scripts/utils/tmux.sh

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)
Expand Down Expand Up @@ -46,6 +68,7 @@ containered() {
__containered_cmd "$cmd"
}


__ssh_cmd() {
[[ $1 =~ (^|^[[:blank:]]*|/?)ssh[[:blank:]] ]] || [[ $1 =~ (^|^[[:blank:]]*|/?)sshpass[[:blank:]] ]]
}
Expand All @@ -54,6 +77,138 @@ __containered_cmd() {
[[ $1 =~ (^|^[[:blank:]]*|/?)docker[[:blank:]] ]] || [[ $1 =~ (^|^[[:blank:]]*|/?)podman[[:blank:]] ]]
}


get_ssh_user() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plugin doesn't parse ssh/config file anymore

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" ]
=======
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invalid conflicts resolving

__current_pane_command() {
local ppid=$(tmux display-message -p "#{pane_pid}")
local pid command cmd
Expand Down Expand Up @@ -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" ]
}