first commit
This commit is contained in:
commit
b9f3edd197
5 changed files with 203 additions and 0 deletions
0
README.md
Normal file
0
README.md
Normal file
50
host.tmux
Executable file
50
host.tmux
Executable file
|
@ -0,0 +1,50 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
source "$CURRENT_DIR/scripts/helpers.sh"
|
||||
|
||||
fields_interpolation=(
|
||||
"\#{hostname}"
|
||||
"\#{username}"
|
||||
"\#{hostname_fg_color}"
|
||||
"\#{hostname_bg_color}"
|
||||
"\#{username_fg_color}"
|
||||
"\#{username_bg_color}"
|
||||
)
|
||||
|
||||
fields_commands=(
|
||||
"#($CURRENT_DIR/scripts/username.sh)"
|
||||
"#($CURRENT_DIR/scripts/hostname.sh)"
|
||||
"#($CURRENT_DIR/scripts/hostname_fg_color.sh)"
|
||||
"#($CURRENT_DIR/scripts/hostname_bg_color.sh)"
|
||||
"#($CURRENT_DIR/scripts/username_fg_color.sh)"
|
||||
"#($CURRENT_DIR/scripts/username_bg_color.sh)"
|
||||
)
|
||||
|
||||
set_tmux_option() {
|
||||
local opion=$1
|
||||
local value=$2
|
||||
tmux set-option -gq "$option" "$value"
|
||||
}
|
||||
|
||||
do_interpolation() {
|
||||
local all_interpolated="$1"
|
||||
for ((i=0; i<${#cpu_commands[@]}; i++)); do
|
||||
all_interpolated=${all_interpolated/${}}
|
||||
done
|
||||
echo "$all_interpolated"
|
||||
}
|
||||
|
||||
update_tmux_option() {
|
||||
local option=$1
|
||||
local option_value=$(get_tmux_option "$option")
|
||||
local new_option_value=$(do_interpolation "$option_value")
|
||||
set_tmux_option "$option" "$new_option_value"
|
||||
}
|
||||
|
||||
main() {
|
||||
update_tmux_option "status-right"
|
||||
update_tmux_option "status-left"
|
||||
}
|
||||
main
|
64
scripts/helpers.sh
Normal file
64
scripts/helpers.sh
Normal file
|
@ -0,0 +1,64 @@
|
|||
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
|
||||
}
|
||||
|
||||
is_osx() {
|
||||
[ $(uname) == "Darwin" ]
|
||||
}
|
||||
|
||||
is_freebsd() {
|
||||
[ $(uname) == "FreeBSD" ]
|
||||
}
|
||||
|
||||
is_openbsd() {
|
||||
[ $(uname) == "OpenBSD" ]
|
||||
}
|
||||
|
||||
is_linux() {
|
||||
[ $(uname) == "Linux" ]
|
||||
}
|
||||
|
||||
is_cygwin() {
|
||||
command -v WMIC &> /dev/null
|
||||
}
|
||||
|
||||
is_linux_iostat() {
|
||||
# Bug in early versions of linux iostat -V return error code
|
||||
iostat -c &> /dev/null
|
||||
}
|
||||
|
||||
# is second float bigger?
|
||||
fcomp() {
|
||||
awk -v n1=$1 -v n2=$2 'BEGIN {if (n1<n2) exit 0; exit 1}'
|
||||
}
|
||||
|
||||
cpu_load_status() {
|
||||
local percentage=$1
|
||||
if fcomp 80 $percentage; then
|
||||
echo "high"
|
||||
elif fcomp 30 $percentage && fcomp $percentage 80; then
|
||||
echo "medium"
|
||||
else
|
||||
echo "low"
|
||||
fi
|
||||
}
|
||||
|
||||
cpus_number() {
|
||||
if is_linux; then
|
||||
nproc
|
||||
else
|
||||
sysctl -n hw.ncpu
|
||||
fi
|
||||
}
|
||||
|
||||
command_exists() {
|
||||
local command="$1"
|
||||
command -v "$command" &> /dev/null
|
||||
}
|
41
scripts/hostname.sh
Executable file
41
scripts/hostname.sh
Executable file
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
source "$CURRENT_DIR/helpers.sh"
|
||||
|
||||
get_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
|
||||
# shellcheck disable=SC2086
|
||||
hostname=$(ssh -G $ssh_parameters 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 }')
|
||||
#shellcheck disable=SC1004
|
||||
hostname=$(echo "$hostname" | awk '\
|
||||
{ \
|
||||
if ($1~/^[0-9.:]+$/) \
|
||||
print $1; \
|
||||
else \
|
||||
split($1, a, ".") ; print a[1] \
|
||||
}')
|
||||
else
|
||||
hostname=$(command hostname -s)
|
||||
fi
|
||||
|
||||
echo "$hostname"
|
||||
}
|
||||
|
||||
main() {
|
||||
local tmux_hostname=$(get_hostname)
|
||||
echo "$tmux_hostname"
|
||||
}
|
||||
main
|
48
scripts/username.sh
Executable file
48
scripts/username.sh
Executable file
|
@ -0,0 +1,48 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
source "$CURRENT_DIR/helpers.sh"
|
||||
|
||||
get_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
|
||||
# shellcheck disable=SC2086
|
||||
username=$(ssh -G $ssh_parameters 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 }')
|
||||
else
|
||||
# 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
|
||||
fi
|
||||
|
||||
echo "$username"
|
||||
}
|
||||
|
||||
main() {
|
||||
local tmux_username=$(get_username)
|
||||
echo "$tmux_username"
|
||||
}
|
||||
main
|
Loading…
Reference in a new issue