From 87f091ecac9f22d90874da67166fb3c039848e34 Mon Sep 17 00:00:00 2001 From: Marco Lehmann Date: Thu, 25 Apr 2024 15:37:43 +0200 Subject: [PATCH 1/6] add and sync scripts with Dracula theme --- scripts/attached_clients.sh | 35 +++++++ scripts/continuum.sh | 161 ++++++++++++++++++++++++++++ scripts/cpu_info.sh | 14 ++- scripts/cwd.sh | 25 +++++ scripts/fossil.sh | 178 +++++++++++++++++++++++++++++++ scripts/git.sh | 67 ++++++++---- scripts/gpu_power.sh | 49 +++++++++ scripts/gpu_ram_info.sh | 48 +++++++++ scripts/gpu_usage.sh | 2 +- scripts/hg.sh | 163 +++++++++++++++++++++++++++++ scripts/kubernetes_context.sh | 24 ++++- scripts/monokai.sh | 192 ++++++++++++++++++++++++---------- scripts/mpc.sh | 25 +++++ scripts/network.sh | 4 +- scripts/network_bandwidth.sh | 152 ++++++++++++++++++++------- scripts/network_ping.sh | 6 +- scripts/network_vpn.sh | 36 +++++++ scripts/playerctl.sh | 25 +++++ scripts/ram_info.sh | 66 +++++++----- scripts/spotify-tui.sh | 3 +- scripts/ssh_session.sh | 122 +++++++++++++++++++++ scripts/synchronize_panes.sh | 26 +++++ scripts/terraform.sh | 30 ++++++ scripts/tmux_ram_info.sh | 120 +++++++++++++++++++++ scripts/utils.sh | 14 ++- scripts/weather.sh | 2 +- scripts/weather_wrapper.sh | 31 ++++++ 27 files changed, 1462 insertions(+), 158 deletions(-) create mode 100644 scripts/attached_clients.sh create mode 100644 scripts/continuum.sh create mode 100644 scripts/cwd.sh create mode 100644 scripts/fossil.sh create mode 100644 scripts/gpu_power.sh create mode 100644 scripts/gpu_ram_info.sh create mode 100644 scripts/hg.sh create mode 100644 scripts/mpc.sh create mode 100644 scripts/network_vpn.sh create mode 100644 scripts/playerctl.sh create mode 100644 scripts/ssh_session.sh create mode 100644 scripts/synchronize_panes.sh create mode 100644 scripts/terraform.sh create mode 100644 scripts/tmux_ram_info.sh create mode 100644 scripts/weather_wrapper.sh diff --git a/scripts/attached_clients.sh b/scripts/attached_clients.sh new file mode 100644 index 00000000..ed7d6141 --- /dev/null +++ b/scripts/attached_clients.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +# configuration +# @monokai-clients-minimum 1 +# @monokai-clients-singular client +# @monokai-clients-plural clients + +current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source $current_dir/utils.sh + +count_clients() { + pane=$(tmux list-panes -F "#{session_name}" | head -n 1) + tmux list-clients -t $pane | wc -l | tr -d ' ' +} + +main() { + # storing the refresh rate in the variable RATE, default is 5 + RATE=$(get_tmux_option "@monokai-refresh-rate" 5) + clients_count=$(count_clients) + clients_minimum=$(get_tmux_option "@monokai-clients-minimum" 1) + if (( $clients_count >= $clients_minimum )); then + if (( $clients_count > 1 )); then + clients_label=$(get_tmux_option "@monokai-clients-plural" "clients") + else + clients_label=$(get_tmux_option "@monokai-clients-singular" "client") + fi + echo "$clients_count $clients_label" + fi + sleep $RATE +} + +# run main driver +main diff --git a/scripts/continuum.sh b/scripts/continuum.sh new file mode 100644 index 00000000..841b9922 --- /dev/null +++ b/scripts/continuum.sh @@ -0,0 +1,161 @@ +#!/usr/bin/env bash +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +# configuration +# @monokai-continuum-mode default (countdown|time|alert|interval) +# @monokai-continuum-time-threshold 15 + +alert_mode="@monokai-continuum-mode" +time_threshold="@monokai-continuum-time-threshold" +warn_threshold=360 +first_save="@monokai-continuum-first-save" + +# tmux-resurrect and tmux-continuum options +if [ -d "$HOME/.tmux/resurrect" ]; then + default_resurrect_dir="$HOME/.tmux/resurrect" +else + default_resurrect_dir="${XDG_DATA_HOME:-$HOME/.local/share}"/tmux/resurrect +fi +resurrect_dir_option="@resurrect-dir" +last_auto_save_option="@continuum-save-last-timestamp" +auto_save_interval_option="@continuum-save-interval" +auto_save_interval_default="15" + +current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source $current_dir/utils.sh + +current_timestamp() { + echo "$(date +%s)" +} + +file_mtime() { + if [ ! -f "$1" ]; then + echo -1 + return + fi + case $(uname -s) in + Linux|Darwin) + date -r "$1" +%s + ;; + + FreeBSD) + stat -f %m "$1" + ;; + + CYGWIN*|MINGW32*|MSYS*|MINGW*) + # TODO - windows compatability + ;; + esac +} + +timestamp_date() { + case $(uname -s) in + Linux) + date -d "@$1" "$2" + ;; + + Darwin|FreeBSD) + date -r "$1" "$2" + ;; + + CYGWIN*|MINGW32*|MSYS*|MINGW*) + # TODO - windows compatability + ;; + esac +} + +set_tmux_option() { + local option="$1" + local value="$2" + tmux set-option -gq "$option" "$value" +} + +# tmux-resurrect dir +resurrect_dir() { + if [ -z "$_RESURRECT_DIR" ]; then + local path="$(get_tmux_option "$resurrect_dir_option" "$default_resurrect_dir")" + # expands tilde, $HOME and $HOSTNAME if used in @resurrect-dir + echo "$path" | sed "s,\$HOME,$HOME,g; s,\$HOSTNAME,$(hostname),g; s,\~,$HOME,g" + else + echo "$_RESURRECT_DIR" + fi +} +_RESURRECT_DIR="$(resurrect_dir)" + +last_resurrect_file() { + echo "$(resurrect_dir)/last" +} + +last_saved_timestamp() { + local last_saved_timestamp="$(get_tmux_option "$last_auto_save_option" "")" + local first_save_timestamp="$(get_tmux_option "$first_save" "")" + # continuum sets the last save timestamp to the current time on first load if auto_save_option is not set + # so we can outrace it and detect that last_uato_save_option is empty and the timestamp is a dummy save + if [ -z "$first_save_timestamp" ]; then + last_saved_timestamp="$(file_mtime "$(last_resurrect_file)")" || last_saved_timestamp=-1 + set_tmux_option "$first_save" "$last_saved_timestamp" + elif [ "$first_save_timestamp" != "done" ]; then + last_saved_timestamp="$(file_mtime "$(last_resurrect_file)")" || last_saved_timestamp=-1 + if [ "$last_saved_timestamp" -gt "$first_save_timestamp" ]; then + set_tmux_option "$first_save" "done" + else + last_saved_timestamp="$first_save_timestamp" + fi + fi + echo "$last_saved_timestamp" +} + +print_status() { + local mode="$(get_tmux_option "$alert_mode" "countdown")" + local info_threshold="$(get_tmux_option "$time_threshold" "15")" + local save_int="$(get_tmux_option "$auto_save_interval_option" "$auto_save_interval_default")" + local interval_seconds="$((save_int * 60))" + local status="" + local last_timestamp="$(last_saved_timestamp)" + local time_delta="$(($(current_timestamp) - last_timestamp))" + local time_delta_minutes="$((time_delta / 60))" + + if [[ $save_int -gt 0 ]]; then + if [[ "$time_delta" -gt $((interval_seconds + warn_threshold)) ]]; then + if [[ "$last_timestamp" == -1 ]]; then + status="no save" + else + status="last save: $(timestamp_date "$last_timestamp" '+%F %T')" + fi + if [[ "$mode" == "countdown" ]]; then + # continuum timestamp may be different than file timestamp on first load + local last_continuum_timestamp="$(get_tmux_option "$last_auto_save_option" "")" + time_delta="$(($(current_timestamp) - last_continuum_timestamp))" + time_delta_minutes="$((time_delta / 60))" + + status="$status; T$(printf '%+d' "$((time_delta_minutes - save_int))")min" + fi + elif [[ "$time_delta" -le "$info_threshold" ]]; then + status="saved" + else + case "$mode" in + countdown) + status="T$(printf '%+d' "$((time_delta_minutes - save_int))")min"; + ;; + + time) + status="$time_delta_minutes"; + ;; + + alert) + status="" + ;; + + interval) + status="$save_int" + ;; + esac + fi + else + status="off" + fi + + echo "$status" +} +print_status diff --git a/scripts/cpu_info.sh b/scripts/cpu_info.sh index 3ce77350..059f368d 100755 --- a/scripts/cpu_info.sh +++ b/scripts/cpu_info.sh @@ -21,6 +21,14 @@ get_percent() normalize_percent_len $percent ;; + OpenBSD) + cpuvalue=$(ps -A -o %cpu | awk -F. '{s+=$1} END {print s}') + cpucores=$(sysctl -n hw.ncpuonline) + cpuusage=$(( cpuvalue / cpucores )) + percent="$cpuusage%" + normalize_percent_len $percent + ;; + CYGWIN*|MINGW32*|MSYS*|MINGW*) # TODO - windows compatability ;; @@ -29,7 +37,7 @@ get_percent() get_load() { case $(uname -s) in - Linux | Darwin) + Linux | Darwin | OpenBSD) loadavg=$(uptime | awk -F'[a-z]:' '{ print $2}' | sed 's/,//g') echo $loadavg ;; @@ -44,10 +52,10 @@ main() { # storing the refresh rate in the variable RATE, default is 5 RATE=$(get_tmux_option "@monokai-refresh-rate" 5) cpu_load=$(get_tmux_option "@monokai-cpu-display-load" false) + cpu_label=$(get_tmux_option "@monokai-cpu-usage-label" "CPU") if [ "$cpu_load" = true ]; then - echo "$(get_load)" + echo "$cpu_label $(get_load)" else - cpu_label=$(get_tmux_option "@monokai-cpu-usage-label" "CPU") cpu_percent=$(get_percent) echo "$cpu_label $cpu_percent" fi diff --git a/scripts/cwd.sh b/scripts/cwd.sh new file mode 100644 index 00000000..5da448c8 --- /dev/null +++ b/scripts/cwd.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +# return current working directory of tmux pane +getPaneDir() { + nextone="false" + ret="" + for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); do + [ "$i" == "1" ] && nextone="true" && continue + [ "$i" == "0" ] && nextone="false" + [ "$nextone" == "true" ] && ret+="$i " + done + echo "${ret%?}" +} + +main() { + path=$(getPaneDir) + + # change '/home/user' to '~' + cwd="${path/"$HOME"/'~'}" + + echo "$cwd" +} + +#run main driver program +main diff --git a/scripts/fossil.sh b/scripts/fossil.sh new file mode 100644 index 00000000..813b3b75 --- /dev/null +++ b/scripts/fossil.sh @@ -0,0 +1,178 @@ +#!/usr/bin/env bash + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $current_dir/utils.sh + +IFS=' ' read -r -a hide_status <<< $(get_tmux_option "@monokai-fossil-disable-status" "false") +IFS=' ' read -r -a current_symbol <<< $(get_tmux_option "@monokai-fossil-show-current-symbol" "✓") +IFS=' ' read -r -a diff_symbol <<< $(get_tmux_option "@monokai-fossil-show-diff-symbol" "!") +IFS=' ' read -r -a no_repo_message <<< $(get_tmux_option "@monokai-fossil-no-repo-message" "") +IFS=' ' read -r -a no_untracked_files <<< $(get_tmux_option "@monokai-fossil-no-untracked-files" "false") +IFS=' ' read -r -a show_remote_status <<< $(get_tmux_option "@monokai-fossil-show-remote-status" "false") + +# Get added, modified, updated and deleted files from git status +getChanges() +{ + declare -i added=0; + declare -i modified=0; + declare -i updated=0; + declare -i deleted=0; + +for i in $(cd $path; fossil changes --differ|cut -f1 -d' ') + + do + case $i in + 'EXTRA') + added+=1 + ;; + 'EDITED') + modified+=1 + ;; + 'U') + updated+=1 + ;; + 'DELETED') + deleted+=1 + ;; + + esac + done + + output="" + [ $added -gt 0 ] && output+="${added}A" + [ $modified -gt 0 ] && output+=" ${modified}M" + [ $updated -gt 0 ] && output+=" ${updated}U" + [ $deleted -gt 0 ] && output+=" ${deleted}D" + + echo $output +} + + +# getting the #{pane_current_path} from monokai.sh is no longer possible +getPaneDir() +{ + nextone="false" + for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); + do + if [ "$nextone" == "true" ]; then + echo $i + return + fi + if [ "$i" == "1" ]; then + nextone="true" + fi + done +} + + +# check if the current or diff symbol is empty to remove ugly padding +checkEmptySymbol() +{ + symbol=$1 + if [ "$symbol" == "" ]; then + echo "true" + else + echo "false" + fi +} + +# check to see if the current repo is not up to date with HEAD +checkForChanges() +{ + if [ "$(checkForFossilDir)" == "true" ]; then + if [ "$(cd $path; fossil changes --differ)" != "" ]; then + echo "true" + else + echo "false" + fi + else + echo "false" + fi +} + +# check if a git repo exists in the directory +checkForFossilDir() +{ + if [ -f ${path}/.fslckout ]; then + echo "true" + else + echo "false" + fi +} + +# return branch name if there is one +getBranch() +{ + if [ $(checkForFossilDir) == "true" ]; then + echo $(cd $path; fossil branch current) + else + echo $no_repo_message + fi +} + +getRemoteInfo() +{ + base=$(cd $path; fossil branch current) + remote=$(echo "$base" | cut -d" " -f1) + out="" + + if [ -n "$remote" ]; then + out="...$remote" + ahead=$(echo "$base" | grep -E -o 'ahead[ [:digit:]]+' | cut -d" " -f2) + behind=$(echo "$base" | grep -E -o 'behind[ [:digit:]]+' | cut -d" " -f2) + + [ -n "$ahead" ] && out+=" +$ahead" + [ -n "$behind" ] && out+=" -$behind" + fi + + echo "$out" +} + +# return the final message for the status bar +getMessage() +{ + if [ $(checkForFossilDir) == "true" ]; then + branch="$(getBranch)" + output="" + + if [ $(checkForChanges) == "true" ]; then + + changes="$(getChanges)" + + if [ "${hide_status}" == "false" ]; then + if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then + output=$(echo "${changes} $branch") + else + output=$(echo "$diff_symbol ${changes} $branch") + fi + else + if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then + output=$(echo "$branch") + else + output=$(echo "$diff_symbol $branch") + fi + fi + + else + if [ $(checkEmptySymbol $current_symbol) == "true" ]; then + output=$(echo "$branch") + else + output=$(echo "$current_symbol $branch") + fi + fi + + [ "$show_remote_status" == "true" ] && output+=$(getRemoteInfo) + echo "$output" + else + echo $no_repo_message + fi +} + +main() +{ + path=$(getPaneDir) + getMessage +} + +#run main driver program +main diff --git a/scripts/git.sh b/scripts/git.sh index ff09a6af..6dad8fbd 100755 --- a/scripts/git.sh +++ b/scripts/git.sh @@ -8,6 +8,7 @@ IFS=' ' read -r -a current_symbol <<< $(get_tmux_option "@monokai-git-show-curre IFS=' ' read -r -a diff_symbol <<< $(get_tmux_option "@monokai-git-show-diff-symbol" "!") IFS=' ' read -r -a no_repo_message <<< $(get_tmux_option "@monokai-git-no-repo-message" "") IFS=' ' read -r -a no_untracked_files <<< $(get_tmux_option "@monokai-git-no-untracked-files" "false") +IFS=' ' read -r -a show_remote_status <<< $(get_tmux_option "@monokai-git-show-remote-status" "false") # Get added, modified, updated and deleted files from git status getChanges() @@ -20,15 +21,15 @@ getChanges() for i in $(git -C $path --no-optional-locks status -s) do - case $i in + case $i in 'A') - added+=1 + added+=1 ;; 'M') modified+=1 ;; 'U') - updated+=1 + updated+=1 ;; 'D') deleted+=1 @@ -42,8 +43,8 @@ for i in $(git -C $path --no-optional-locks status -s) [ $modified -gt 0 ] && output+=" ${modified}M" [ $updated -gt 0 ] && output+=" ${updated}U" [ $deleted -gt 0 ] && output+=" ${deleted}D" - - echo $output + + echo $output } @@ -56,7 +57,7 @@ getPaneDir() if [ "$nextone" == "true" ]; then echo $i return - fi + fi if [ "$i" == "1" ]; then nextone="true" fi @@ -67,7 +68,7 @@ getPaneDir() # check if the current or diff symbol is empty to remove ugly padding checkEmptySymbol() { - symbol=$1 + symbol=$1 if [ "$symbol" == "" ]; then echo "true" else @@ -88,7 +89,7 @@ checkForChanges() else echo "false" fi -} +} # check if a git repo exists in the directory checkForGitDir() @@ -102,7 +103,7 @@ checkForGitDir() # return branch name if there is one getBranch() -{ +{ if [ $(checkForGitDir) == "true" ]; then echo $(git -C $path rev-parse --abbrev-ref HEAD) else @@ -110,47 +111,69 @@ getBranch() fi } +getRemoteInfo() +{ + base=$(git -C $path for-each-ref --format='%(upstream:short) %(upstream:track)' "$(git -C $path symbolic-ref -q HEAD)") + remote=$(echo "$base" | cut -d" " -f1) + out="" + + if [ -n "$remote" ]; then + out="...$remote" + ahead=$(echo "$base" | grep -E -o 'ahead[ [:digit:]]+' | cut -d" " -f2) + behind=$(echo "$base" | grep -E -o 'behind[ [:digit:]]+' | cut -d" " -f2) + + [ -n "$ahead" ] && out+=" +$ahead" + [ -n "$behind" ] && out+=" -$behind" + fi + + echo "$out" +} + # return the final message for the status bar getMessage() { if [ $(checkForGitDir) == "true" ]; then branch="$(getBranch)" - - if [ $(checkForChanges) == "true" ]; then - - changes="$(getChanges)" - + output="" + + if [ $(checkForChanges) == "true" ]; then + + changes="$(getChanges)" + if [ "${hide_status}" == "false" ]; then if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then - echo "${changes} $branch" + output=$(echo "${changes} $branch") else - echo "$diff_symbol ${changes} $branch" + output=$(echo "$diff_symbol ${changes} $branch") fi else if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then - echo "$branch" + output=$(echo "$branch") else - echo "$diff_symbol $branch" + output=$(echo "$diff_symbol $branch") fi fi else if [ $(checkEmptySymbol $current_symbol) == "true" ]; then - echo "$branch" + output=$(echo "$branch") else - echo "$current_symbol $branch" + output=$(echo "$current_symbol $branch") fi fi + + [ "$show_remote_status" == "true" ] && output+=$(getRemoteInfo) + echo "$output" else echo $no_repo_message fi } main() -{ +{ path=$(getPaneDir) getMessage } #run main driver program -main +main diff --git a/scripts/gpu_power.sh b/scripts/gpu_power.sh new file mode 100644 index 00000000..8fc0f9db --- /dev/null +++ b/scripts/gpu_power.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $current_dir/utils.sh + +get_platform() +{ + case $(uname -s) in + Linux) + gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}') + echo $gpu + ;; + + Darwin) + # TODO - Darwin/Mac compatability + ;; + + CYGWIN*|MINGW32*|MSYS*|MINGW*) + # TODO - windows compatability + ;; + esac +} + +get_gpu() +{ + gpu=$(get_platform) + if [[ "$gpu" == NVIDIA ]]; then + usage=$(nvidia-smi --query-gpu=power.draw,power.limit --format=csv,noheader,nounits | awk '{ draw += $0; max +=$2 } END { printf("%dW/%dW\n", draw, max) }') + + else + usage='unknown' + fi + normalize_percent_len $usage +} + +main() +{ + # storing the refresh rate in the variable RATE, default is 5 + RATE=$(get_tmux_option "@monokai-refresh-rate" 5) + gpu_label=$(get_tmux_option "@monokai-gpu-usage-label" "GPU") + gpu_usage=$(get_gpu) + echo "$gpu_label $gpu_usage" + sleep $RATE +} + +# run the main driver +main diff --git a/scripts/gpu_ram_info.sh b/scripts/gpu_ram_info.sh new file mode 100644 index 00000000..e426381c --- /dev/null +++ b/scripts/gpu_ram_info.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $current_dir/utils.sh + +get_platform() +{ + case $(uname -s) in + Linux) + gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}') + echo $gpu + ;; + + Darwin) + # TODO - Darwin/Mac compatability + ;; + + CYGWIN*|MINGW32*|MSYS*|MINGW*) + # TODO - windows compatability + ;; + esac +} + +get_gpu() +{ + gpu=$(get_platform) + if [[ "$gpu" == NVIDIA ]]; then + usage=$(nvidia-smi --query-gpu=memory.used,memory.total --format=csv,noheader,nounits | awk '{ used += $0; total +=$2 } END { printf("%dGB/%dGB\n", used / 1024, total / 1024) }') + else + usage='unknown' + fi + normalize_percent_len $usage +} + +main() +{ + # storing the refresh rate in the variable RATE, default is 5 + RATE=$(get_tmux_option "@monokai-refresh-rate" 5) + gpu_label=$(get_tmux_option "@monokai-gpu-usage-label" "VRAM") + gpu_usage=$(get_gpu) + echo "$gpu_label $gpu_usage" + sleep $RATE +} + +# run the main driver +main diff --git a/scripts/gpu_usage.sh b/scripts/gpu_usage.sh index 8d33bb5c..afb83554 100755 --- a/scripts/gpu_usage.sh +++ b/scripts/gpu_usage.sh @@ -27,7 +27,7 @@ get_gpu() { gpu=$(get_platform) if [[ "$gpu" == NVIDIA ]]; then - usage=$(nvidia-smi | grep '%' | awk '{ sum += $13 } END { printf("%d%%\n", sum / NR) }') + usage=$(nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits | awk '{ sum += $0 } END { printf("%d%%\n", sum / NR) }') else usage='unknown' fi diff --git a/scripts/hg.sh b/scripts/hg.sh new file mode 100644 index 00000000..7c2953e8 --- /dev/null +++ b/scripts/hg.sh @@ -0,0 +1,163 @@ +#!/usr/bin/env bash + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $current_dir/utils.sh + +IFS=' ' read -r -a hide_status <<< $(get_tmux_option "@monokai-hg-disable-status" "false") +IFS=' ' read -r -a current_symbol <<< $(get_tmux_option "@monokai-hg-show-current-symbol" "✓") +IFS=' ' read -r -a diff_symbol <<< $(get_tmux_option "@monokai-hg-show-diff-symbol" "!") +IFS=' ' read -r -a no_repo_message <<< $(get_tmux_option "@monokai-hg-no-repo-message" "") +IFS=' ' read -r -a no_untracked_files <<< $(get_tmux_option "@monokai-hg-no-untracked-files" "false") + +# Get added, modified, and removed files from hg status +getChanges() +{ + declare -i added=0; + declare -i deleted=0; + declare -i modified=0; + declare -i removed=0; + declare -i untracked=0; + +for i in $(hg -R $path status -admru) + do + case $i in + 'A') + added+=1 + ;; + '!') + deleted+=1 + ;; + 'M') + modified+=1 + ;; + 'R') + removed+=1 + ;; + '?') + untracked+=1 + ;; + + esac + done + + output="" + [ $added -gt 0 ] && output+="${added}A" + [ $modified -gt 0 ] && output+=" ${modified}M" + [ $deleted -gt 0 ] && output+=" ${deleted}D" + [ $removed -gt 0 ] && output+=" ${removed}R" + [ $no_untracked_files == "false" -a $untracked -gt 0 ] && output+=" ${untracked}?" + + echo $output +} + + +# getting the #{pane_current_path} from monokai.sh is no longer possible +getPaneDir() +{ + nextone="false" + for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); + do + if [ "$nextone" == "true" ]; then + echo $i + return + fi + if [ "$i" == "1" ]; then + nextone="true" + fi + done +} + + +# check if the current or diff symbol is empty to remove ugly padding +checkEmptySymbol() +{ + symbol=$1 + if [ "$symbol" == "" ]; then + echo "true" + else + echo "false" + fi +} + +# check to see if the current repo is not up to date with HEAD +checkForChanges() +{ + [ $no_untracked_files == "false" ] && no_untracked="-u" || no_untracked="" + if [ "$(checkForHgDir)" == "true" ]; then + if [ "$(hg -R $path status -admr $no_untracked)" != "" ]; then + echo "true" + else + echo "false" + fi + else + echo "false" + fi +} + +# check if a hg repo exists in the directory +checkForHgDir() +{ + if [ "$(hg -R $path branch)" != "" ]; then + echo "true" + else + echo "false" + fi +} + +# return branch name if there is one +getBranch() +{ + if [ $(checkForHgDir) == "true" ]; then + echo $(hg -R $path branch) + else + echo $no_repo_message + fi +} + +# return the final message for the status bar +getMessage() +{ + if [ $(checkForHgDir) == "true" ]; then + branch="$(getBranch)" + output="" + + if [ $(checkForChanges) == "true" ]; then + + changes="$(getChanges)" + + if [ "${hide_status}" == "false" ]; then + if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then + output=$(echo "${changes} $branch") + else + output=$(echo "$diff_symbol ${changes} $branch") + fi + else + if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then + output=$(echo "$branch") + else + output=$(echo "$diff_symbol $branch") + fi + fi + + else + if [ $(checkEmptySymbol $current_symbol) == "true" ]; then + output=$(echo "$branch") + else + output=$(echo "$current_symbol $branch") + fi + fi + + echo "$output" + else + echo $no_repo_message + fi +} + +main() +{ + path=$(getPaneDir) + getMessage +} + +#run main driver program +main diff --git a/scripts/kubernetes_context.sh b/scripts/kubernetes_context.sh index ba01f485..11be25f3 100755 --- a/scripts/kubernetes_context.sh +++ b/scripts/kubernetes_context.sh @@ -2,7 +2,10 @@ # setting the locale, some users have issues with different locales, this forces the correct one export LC_ALL=en_US.UTF-8 -label=$1 +hide_arn_from_cluster=$1 +extract_account=$2 +hide_user=$3 +label=$4 current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source $current_dir/utils.sh @@ -12,11 +15,30 @@ current_user=$(kubectl config view --minify --output 'jsonpath={.contexts[?(@.na current_cluster=$(kubectl config view --minify --output 'jsonpath={.contexts[?(@.name=="'$current_context'")].context.cluster}'; echo) current_namespace=$(kubectl config view --minify --output 'jsonpath={.contexts[?(@.name=="'$current_context'")].context.namespace}'; echo) +current_account_id="" +if [[ "$current_cluster" =~ ^arn:aws:eks:[a-z0-9\-]*:[0-9]*:cluster/[a-z0-9\-]*$ ]]; then + if [ "$extract_account" = "true" ]; then + current_account_id=$(echo "$current_cluster" | cut -d':' -f5) + fi + if [ "$hide_arn_from_cluster" = "true" ]; then + current_cluster=${current_cluster##*/} + fi +fi + +if [ "$hide_user" = "true" ]; then + current_user="" +fi + main() { # storing the refresh rate in the variable RATE, default is 5 RATE=$(get_tmux_option "@monokai-refresh-rate" 5) OUTPUT_STRING="" + if [ ! -z "$current_account_id" ] + then + OUTPUT_STRING="${current_account_id}/" + fi + if [ ! -z "$current_user" ] then OUTPUT_STRING="${current_user}@" diff --git a/scripts/monokai.sh b/scripts/monokai.sh index c3c5e58d..19dd70f8 100755 --- a/scripts/monokai.sh +++ b/scripts/monokai.sh @@ -7,27 +7,34 @@ source $current_dir/utils.sh main() { - datafile=/tmp/.monokai-tmux-data - # set configuration option variables + show_kubernetes_context_label=$(get_tmux_option "@monokai-kubernetes-context-label" "") + eks_hide_arn=$(get_tmux_option "@monokai-kubernetes-eks-hide-arn" false) + eks_extract_account=$(get_tmux_option "@monokai-kubernetes-eks-extract-account" false) + hide_kubernetes_user=$(get_tmux_option "@monokai-kubernetes-hide-user" false) + terraform_label=$(get_tmux_option "@monokai-terraform-label" "") show_fahrenheit=$(get_tmux_option "@monokai-show-fahrenheit" true) show_location=$(get_tmux_option "@monokai-show-location" true) fixed_location=$(get_tmux_option "@monokai-fixed-location") - show_powerline=$(get_tmux_option "@monokai-show-powerline" true) + show_powerline=$(get_tmux_option "@monokai-show-powerline" false) show_flags=$(get_tmux_option "@monokai-show-flags" false) - show_left_icon=$(get_tmux_option "@monokai-show-left-icon" session) - show_left_icon_padding=$(get_tmux_option "@monokai-left-icon-padding" 0) + show_left_icon=$(get_tmux_option "@monokai-show-left-icon" smiley) + show_left_icon_padding=$(get_tmux_option "@monokai-left-icon-padding" 1) show_military=$(get_tmux_option "@monokai-military-time" false) + timezone=$(get_tmux_option "@monokai-set-timezone" "") show_timezone=$(get_tmux_option "@monokai-show-timezone" true) show_left_sep=$(get_tmux_option "@monokai-show-left-sep" ) show_right_sep=$(get_tmux_option "@monokai-show-right-sep" ) show_border_contrast=$(get_tmux_option "@monokai-border-contrast" false) show_day_month=$(get_tmux_option "@monokai-day-month" false) show_refresh=$(get_tmux_option "@monokai-refresh-rate" 5) - show_kubernetes_context_label=$(get_tmux_option "@monokai-kubernetes-context-label" "") - IFS=' ' read -r -a plugins <<< $(get_tmux_option "@monokai-plugins" "network-ping cpu-usage ram-usage") + show_synchronize_panes_label=$(get_tmux_option "@monokai-synchronize-panes-label" "Sync") + time_format=$(get_tmux_option "@monokai-time-format" "") + show_ssh_session_port=$(get_tmux_option "@monokai-show-ssh-session-port" false) + IFS=' ' read -r -a plugins <<< $(get_tmux_option "@monokai-plugins" "battery network weather") + show_empty_plugins=$(get_tmux_option "@monokai-show-empty-plugins" true) - # Monokai Pro Color Pallette + # Monokai Pro Color Pallette white='#fcfcfa' black='#2d2a2e' gray='#727072' @@ -46,6 +53,10 @@ main() left_icon="#S";; window) left_icon="#W";; + hostname) + left_icon="#H";; + shortname) + left_icon="#h";; *) left_icon=$show_left_icon;; esac @@ -69,12 +80,14 @@ main() fi # Set timezone unless hidden by configuration - case $show_timezone in - false) - timezone="";; - true) - timezone="#(date +%Z)";; - esac + if [[ -z "$timezone" ]]; then + case $show_timezone in + false) + timezone="";; + true) + timezone="#(date +%Z)";; + esac + fi case $show_flags in false) @@ -126,86 +139,155 @@ main() for plugin in "${plugins[@]}"; do - if [ $plugin = "git" ]; then + if case $plugin in custom:*) true;; *) false;; esac; then + script=${plugin#"custom:"} + if [[ -x "${current_dir}/${script}" ]]; then + IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-custom-plugin-colors" "blue black") + script="#($current_dir/${script})" + else + colors[0]="red" + colors[1]="black" + script="${script} not found!" + fi + + elif [ $plugin = "cwd" ]; then + IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-cwd-colors" "dark_gray white") + tmux set-option -g status-right-length 250 + script="#($current_dir/cwd.sh)" + + elif [ $plugin = "fossil" ]; then + IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-fossil-colors" "green black") + tmux set-option -g status-right-length 250 + script="#($current_dir/fossil.sh)" + + elif [ $plugin = "git" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-git-colors" "green black") - script="#($current_dir/git.sh)" - fi + tmux set-option -g status-right-length 250 + script="#($current_dir/git.sh)" - if [ $plugin = "battery" ]; then + elif [ $plugin = "hg" ]; then + IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-hg-colors" "green black") + tmux set-option -g status-right-length 250 + script="#($current_dir/hg.sh)" + + elif [ $plugin = "battery" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-battery-colors" "red black") script="#($current_dir/battery.sh)" - fi - if [ $plugin = "gpu-usage" ]; then + elif [ $plugin = "gpu-usage" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-gpu-usage-colors" "red black") script="#($current_dir/gpu_usage.sh)" - fi - if [ $plugin = "cpu-usage" ]; then + elif [ $plugin = "gpu-ram-usage" ]; then + IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-gpu-ram-usage-colors" "blue black") + script="#($current_dir/gpu_ram_info.sh)" + + elif [ $plugin = "gpu-power-draw" ]; then + IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-gpu-power-draw-colors" "green black") + script="#($current_dir/gpu_power.sh)" + + elif [ $plugin = "cpu-usage" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-cpu-usage-colors" "magenta black") script="#($current_dir/cpu_info.sh)" - fi - if [ $plugin = "ram-usage" ]; then + elif [ $plugin = "ram-usage" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-ram-usage-colors" "yellow black") script="#($current_dir/ram_info.sh)" - fi - if [ $plugin = "network" ]; then + elif [ $plugin = "tmux-ram-usage" ]; then + IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-tmux-ram-usage-colors" "yellow black") + script="#($current_dir/tmux_ram_info.sh)" + + elif [ $plugin = "network" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-network-colors" "blue black") script="#($current_dir/network.sh)" - fi - if [ $plugin = "network-bandwidth" ]; then + elif [ $plugin = "network-bandwidth" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-network-bandwidth-colors" "blue black") tmux set-option -g status-right-length 250 script="#($current_dir/network_bandwidth.sh)" - fi - if [ $plugin = "network-ping" ]; then - IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-network-ping-colors" "gray white") + elif [ $plugin = "network-ping" ]; then + IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-network-ping-colors" "blue black") script="#($current_dir/network_ping.sh)" - fi - if [ $plugin = "spotify-tui" ]; then + elif [ $plugin = "network-vpn" ]; then + IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-network-vpn-colors" "blue black") + script="#($current_dir/network_vpn.sh)" + + elif [ $plugin = "attached-clients" ]; then + IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-attached-clients-colors" "blue black") + script="#($current_dir/attached_clients.sh)" + + elif [ $plugin = "mpc" ]; then + IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-mpc-colors" "green black") + script="#($current_dir/mpc.sh)" + + elif [ $plugin = "spotify-tui" ]; then IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-spotify-tui-colors" "green black") script="#($current_dir/spotify-tui.sh)" - fi - if [ $plugin = "kubernetes-context" ]; then + elif [ $plugin = "playerctl" ]; then + IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-playerctl-colors" "green black") + script="#($current_dir/playerctl.sh)" + + elif [ $plugin = "kubernetes-context" ]; then IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-kubernetes-context-colors" "blue black") - script="#($current_dir/kubernetes_context.sh $show_kubernetes_context_label)" - fi + script="#($current_dir/kubernetes_context.sh $eks_hide_arn $eks_extract_account $hide_kubernetes_user $show_kubernetes_context_label)" + + elif [ $plugin = "terraform" ]; then + IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-terraform-colors" "blue black") + script="#($current_dir/terraform.sh $terraform_label)" - if [ $plugin = "weather" ]; then - # wait unit $datafile exists just to avoid errors - # this should almost never need to wait unless something unexpected occurs - while [ ! -f $datafile ]; do - sleep 0.01 - done + elif [ $plugin = "continuum" ]; then + IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-continuum-colors" "blue black") + script="#($current_dir/continuum.sh)" + elif [ $plugin = "weather" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-weather-colors" "magenta black") - script="#(cat $datafile)" - fi + script="#($current_dir/weather_wrapper.sh $show_fahrenheit $show_location '$fixed_location')" - if [ $plugin = "time" ]; then + elif [ $plugin = "time" ]; then IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-time-colors" "green white") - if $show_day_month && $show_military ; then # military time and dd/mm - script="%a %d/%m %R ${timezone} " - elif $show_military; then # only military time - script="%a %m/%d %R ${timezone} " - elif $show_day_month; then # only dd/mm - script="%a %d/%m %I:%M %p ${timezone} " + if [ -n "$time_format" ]; then + script=${time_format} else - script="%a %m/%d %I:%M %p ${timezone} " + if $show_day_month && $show_military ; then # military time and dd/mm + script="%a %d/%m %R ${timezone} " + elif $show_military; then # only military time + script="%a %m/%d %R ${timezone} " + elif $show_day_month; then # only dd/mm + script="%a %d/%m %I:%M %p ${timezone} " + else + script="%a %m/%d %I:%M %p ${timezone} " + fi fi + + elif [ $plugin = "synchronize-panes" ]; then + IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-synchronize-panes-colors" "blue black") + script="#($current_dir/synchronize_panes.sh $show_synchronize_panes_label)" + + elif [ $plugin = "ssh-session" ]; then + IFS=' ' read -r -a colors <<< $(get_tmux_option "@monokai-ssh-session-colors" "green black") + script="#($current_dir/ssh_session.sh $show_ssh_session_port)" + + else + continue fi if $show_powerline; then - tmux set-option -ga status-right "#[fg=${!colors[0]},bg=${powerbg},nobold,nounderscore,noitalics]${right_sep}#[fg=${!colors[1]},bg=${!colors[0]}] $script " + if $show_empty_plugins; then + tmux set-option -ga status-right "#[fg=${!colors[0]},bg=${powerbg},nobold,nounderscore,noitalics]${right_sep}#[fg=${!colors[1]},bg=${!colors[0]}] $script " + else + tmux set-option -ga status-right "#{?#{==:$script,},,#[fg=${!colors[0]},nobold,nounderscore,noitalics]${right_sep}#[fg=${!colors[1]},bg=${!colors[0]}] $script }" + fi powerbg=${!colors[0]} else - tmux set-option -ga status-right "#[fg=${!colors[1]},bg=${!colors[0]}] $script " + if $show_empty_plugins; then + tmux set-option -ga status-right "#[fg=${!colors[1]},bg=${!colors[0]}] $script " + else + tmux set-option -ga status-right "#{?#{==:$script,},,#[fg=${!colors[1]},bg=${!colors[0]}] $script }" + fi fi done diff --git a/scripts/mpc.sh b/scripts/mpc.sh new file mode 100644 index 00000000..c3e71406 --- /dev/null +++ b/scripts/mpc.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $current_dir/utils.sh + +main() +{ + # storing the refresh rate in the variable RATE, default is 5 + RATE=$(get_tmux_option "@monokai-refresh-rate" 5) + + if ! command -v mpc &> /dev/null + then + exit 1 + fi + + FORMAT=$(get_tmux_option "@monokai-mpc-format" "%title% - %artist%") + mpc_playback=$(mpc current -f "${FORMAT}") + echo ${mpc_playback} + +} + +# run the main driver +main diff --git a/scripts/network.sh b/scripts/network.sh index d2aa88b7..883212ef 100755 --- a/scripts/network.sh +++ b/scripts/network.sh @@ -18,8 +18,8 @@ get_ssid() ;; Darwin) - if /System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | grep -E ' SSID' | cut -d ':' -f 2 | sed 's/ ^*//g' &> /dev/null; then - echo "$(/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | grep -E ' SSID' | cut -d ':' -f 2)" | sed 's/ ^*//g' + if networksetup -getairportnetwork en0 | cut -d ':' -f 2 | sed 's/^[[:blank:]]*//g' &> /dev/null; then + echo "$(networksetup -getairportnetwork en0 | cut -d ':' -f 2)" | sed 's/^[[:blank:]]*//g' else echo 'Ethernet' fi diff --git a/scripts/network_bandwidth.sh b/scripts/network_bandwidth.sh index 61ee460e..3001de74 100755 --- a/scripts/network_bandwidth.sh +++ b/scripts/network_bandwidth.sh @@ -1,51 +1,123 @@ #!/usr/bin/env bash -INTERVAL="1" # update interval in seconds +# INTERVAL is equal to 1s because we want to express the bandwidth in sec +readonly INTERVAL=1 -network_name=$(tmux show-option -gqv "@monokai-network-bandwidth") +# UPLOAD and DOWNLOAD index +readonly UPLOAD=0 +readonly DOWNLOAD=1 -main() { - while true - do - output_download="" - output_upload="" - output_download_unit="" - output_upload_unit="" - - initial_download=$(cat /sys/class/net/$network_name/statistics/rx_bytes) - initial_upload=$(cat /sys/class/net/$network_name/statistics/tx_bytes) - - sleep $INTERVAL - - final_download=$(cat /sys/class/net/$network_name/statistics/rx_bytes) - final_upload=$(cat /sys/class/net/$network_name/statistics/tx_bytes) - - total_download_bps=$(expr $final_download - $initial_download) - total_upload_bps=$(expr $final_upload - $initial_upload) - - if [ $total_download_bps -gt 1073741824 ]; then - output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}') - output_download_unit="gB/s" - elif [ $total_download_bps -gt 1048576 ]; then - output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}') - output_download_unit="mB/s" - else - output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/$2}') - output_download_unit="kB/s" +# SIZE index are the multiple of the unit byte and value the internationally recommended unit symbol in sec +readonly SIZE=( + [1]='B/s' + [1024]='kB/s' + [1048576]='MB/s' + [1073741824]='GB/s' +) + +# interface_get try to automaticaly get the used interface if network_name is empty +interface_get() { + name="$(tmux show-option -gqv "@monokai-network-bandwidth")" + + if [[ -z $name ]]; then + case "$(uname -s)" in + Linux) + if type ip >/dev/null; then + name="$(ip -o route get 192.168.0.0 | awk '{print $5}')" + fi + ;; + Darwin) + if type route >/dev/null; then + name="$(route -n get 192.168.0.0 2>/dev/null | awk '/interface: / {print $2}')" + fi + ;; + esac + fi + + echo "$name" +} + +# interface_bytes give an interface name and return both tx/rx Bytes, separated by whitespace (upload first) +interface_bytes() { + case "$(uname -s)" in + Linux) + upload=$(cat "/sys/class/net/$1/statistics/tx_bytes") + download=$(cat "/sys/class/net/$1/statistics/rx_bytes") + + echo "$upload $download" + ;; + Darwin) + # column 7 is Ibytes (in bytes, rx, download) and column 10 is Obytes (out bytes, tx, upload) + netstat -nbI "$1" | tail -n1 | awk '{print $10 " " $7}' + ;; + esac +} + +# get_bandwidth return the number of bytes exchanged for tx and rx +get_bandwidth() { + local upload=0 + local download=0 + + IFS=' ' read -r upload download <<< "$(interface_bytes "$1")" + + # wait for interval to calculate the difference + sleep "$INTERVAL" + + IFS=' ' read -r new_upload new_download <<< "$(interface_bytes "$1")" + + upload=$(( $new_upload - $upload )) + download=$(( $new_download - $download )) + + # set to 0 by default + echo "${upload:-0} ${download:-0}" +} + +# bandwidth_to_unit convert bytes into its highest unit and add unit symbol in sec +bandwidth_to_unit() { + local size=1 + for i in "${!SIZE[@]}"; do + if (($1 < i)); then + break fi - if [ $total_upload_bps -gt 1073741824 ]; then - output_upload=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}') - output_upload_unit="gB/s" - elif [ $total_upload_bps -gt 1048576 ]; then - output_upload=$(echo "$total_upload_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}') - output_upload_unit="mB/s" - else - output_upload=$(echo "$total_upload_bps 1024" | awk '{printf "%.2f \n", $1/$2}') - output_upload_unit="kB/s" + size="$i" + done + + local result="0.00" + if (($1 != 0)); then + result="$(awk -v a="$1" -v b="$size" 'BEGIN { printf "%.2f\n", a / b }' /dev/null + then + exit 1 + fi + + FORMAT=$(get_tmux_option "@monokai-playerctl-format" "Now playing: {{ artist }} - {{ album }} - {{ title }}") + playerctl_playback=$(playerctl metadata --format "${FORMAT}") + echo ${playerctl_playback} + +} + +# run the main driver +main diff --git a/scripts/ram_info.sh b/scripts/ram_info.sh index e1b36d80..bb51b93e 100755 --- a/scripts/ram_info.sh +++ b/scripts/ram_info.sh @@ -5,35 +5,29 @@ export LC_ALL=en_US.UTF-8 current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source $current_dir/utils.sh -get_percent() +get_ratio() { case $(uname -s) in Linux) - total_mem_gb=$(free -g | awk '/^Mem/ {print $2}') - used_mem=$(free -g | awk '/^Mem/ {print $3}') - total_mem=$(free -h | awk '/^Mem/ {print $2}') - if (( $total_mem_gb == 0)); then - memory_usage=$(free -m | awk '/^Mem/ {print $3}') - total_mem_mb=$(free -m | awk '/^Mem/ {print $2}') - echo $memory_usage\M\B/$total_mem_mb\M\B - elif (( $used_mem == 0 )); then - memory_usage=$(free -m | awk '/^Mem/ {print $3}') - echo $memory_usage\M\B/$total_mem_gb\G\B - else - memory_usage=$(free -g | awk '/^Mem/ {print $3}') - echo $memory_usage\G\B/$total_mem_gb\G\B - fi + usage="$(free -h | awk 'NR==2 {print $3}')" + total="$(free -h | awk 'NR==2 {print $2}')" + formated="${usage}/${total}" + + echo "${formated//i/B}" ;; Darwin) # Get used memory blocks with vm_stat, multiply by page size to get size in bytes, then convert to MiB used_mem=$(vm_stat | grep ' active\|wired ' | sed 's/[^0-9]//g' | paste -sd ' ' - | awk -v pagesize=$(pagesize) '{printf "%d\n", ($1+$2) * pagesize / 1048576}') - total_mem=$(system_profiler SPHardwareDataType | grep "Memory:" | awk '{print $2 $3}') - if (( $used_mem < 1024 )); then - echo $used_mem\M\B/$total_mem + # System Profiler performs an activation lock check, which can result in + # time outs or a lagged response. (~10 seconds) + # total_mem=$(system_profiler SPHardwareDataType | grep "Memory:" | awk '{print $2 $3}') + total_mem=$(sysctl -n hw.memsize | awk '{print $0/1024/1024/1024 " GB"}') + if ((used_mem < 1024 )); then + echo "${used_mem}MB/$total_mem" else - memory=$(($used_mem/1024)) - echo $memory\G\B/$total_mem + memory=$((used_mem/1024)) + echo "${memory}GB/$total_mem" fi ;; @@ -48,11 +42,32 @@ get_percent() total_mem=$(($(sysctl -n hw.physmem) / 1024 / 1024)) used_mem=$((total_mem - free_mem)) echo $used_mem + if ((used_mem < 1024 )); then + echo "${used_mem}MB/$total_mem" + else + memory=$((used_mem/1024)) + echo "${memory}GB/$total_mem" + fi + ;; + + OpenBSD) + # vmstat -s | grep "pages managed" | sed -ne 's/^ *\([0-9]*\).*$/\1/p' + # Looked at the code from neofetch + hw_pagesize="$(pagesize)" + used_mem=$(( ( +$(vmstat -s | grep "pages active$" | sed -ne 's/^ *\([0-9]*\).*$/\1/p') + +$(vmstat -s | grep "pages inactive$" | sed -ne 's/^ *\([0-9]*\).*$/\1/p') + +$(vmstat -s | grep "pages wired$" | sed -ne 's/^ *\([0-9]*\).*$/\1/p') + +$(vmstat -s | grep "pages zeroed$" | sed -ne 's/^ *\([0-9]*\).*$/\1/p') + +0) * hw_pagesize / 1024 / 1024 )) + total_mem=$(($(sysctl -n hw.physmem) / 1024 / 1024)) + #used_mem=$((total_mem - free_mem)) + total_mem=$(($total_mem/1024)) if (( $used_mem < 1024 )); then - echo $used_mem\M\B/$total_mem + echo $used_mem\M\B/$total_mem\G\B else memory=$(($used_mem/1024)) - echo $memory\G\B/$total_mem + echo $memory\G\B/$total_mem\G\B fi ;; @@ -64,12 +79,9 @@ get_percent() main() { - # storing the refresh rate in the variable RATE, default is 5 - RATE=$(get_tmux_option "@monokai-refresh-rate" 5) ram_label=$(get_tmux_option "@monokai-ram-usage-label" "RAM") - ram_percent=$(get_percent) - echo "$ram_label $ram_percent" - sleep $RATE + ram_ratio=$(get_ratio) + echo "$ram_label $ram_ratio" } #run main driver diff --git a/scripts/spotify-tui.sh b/scripts/spotify-tui.sh index 6363c860..75223c66 100755 --- a/scripts/spotify-tui.sh +++ b/scripts/spotify-tui.sh @@ -15,7 +15,8 @@ main() exit 1 fi - spotify_playback=$(spt playback) + FORMAT=$(get_tmux_option "@monokai-spotify-tui-format" "%f %s %t - %a") + spotify_playback=$(spt playback -f "${FORMAT}") echo ${spotify_playback} } diff --git a/scripts/ssh_session.sh b/scripts/ssh_session.sh new file mode 100644 index 00000000..92daba54 --- /dev/null +++ b/scripts/ssh_session.sh @@ -0,0 +1,122 @@ +#!/usr/bin/env bash + +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +show_ssh_session_port=$1 + +parse_ssh_port() { + # Get port from connection + local port=$(echo $1|grep -Eo '\-p\s*([0-9]+)'|sed 's/-p\s*//') + + if [ -z $port ]; then + local port=22 + fi + + echo $port +} + +parse_ssh_config() { + 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; + }' $1`; do + local host_regex=${ssh_config%|*} + local host_user=${ssh_config#*|} + if [ "$2" == "$host_regex" ]; then + ssh_user_found=$host_user + break + fi + done + + echo $ssh_user_found +} + +get_ssh_user() { + # Search SSH User in user local file if available + if [ -f ~/.ssh/config ]; then + ssh_user=$(parse_ssh_config ~/.ssh/config $1) + fi + + # If SSH User not found, search in global config file + if [ -z $ssh_user ]; then + ssh_user=$(parse_ssh_config /etc/ssh/ssh_config $1) + fi + + #If SSH User not found in any config file, return current user + if [ -z $ssh_user ]; then + ssh_user=$(whoami) + fi + + echo $ssh_user +} + +get_remote_info() { + 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 -f1 -d@) + local host=$(echo $cmd | awk '{print $NF}'|cut -f2 -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_info() { + # If command is ssh get info from remote + if $(ssh_connected); then + echo $(get_remote_info $1) + else + echo $($1) + fi +} + +ssh_connected() { + # Get current pane command + local cmd=$(tmux display-message -p "#{pane_current_command}") + + [ $cmd = "ssh" ] || [ $cmd = "sshpass" ] +} + +main() { + hostname=$(get_info hostname) + user=$(get_info whoami) + + # Only show port info if ssh session connected (no localhost) and option enabled + if $(ssh_connected) && [ "$show_ssh_session_port" == "true" ] ; then + port=$(get_info port) + echo $user@$hostname:$port + else + echo $user@$hostname + fi +} + +main diff --git a/scripts/synchronize_panes.sh b/scripts/synchronize_panes.sh new file mode 100644 index 00000000..d9a7ef83 --- /dev/null +++ b/scripts/synchronize_panes.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +label=$1 + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $current_dir/utils.sh + +get_synchronize_panes_status() { + current_synchronize_panes_status=$(get_tmux_window_option "synchronize-panes" "off") + echo $current_synchronize_panes_status +} + +main() +{ + # storing the refresh rate in the variable RATE, default is 5 + RATE=$(get_tmux_option "@monokai-refresh-rate" 5) + synchronize_panes_label=$label + synchronize_panes_status=$(get_synchronize_panes_status) + echo "$synchronize_panes_label $synchronize_panes_status" + sleep $RATE +} + +# run main driver +main diff --git a/scripts/terraform.sh b/scripts/terraform.sh new file mode 100644 index 00000000..7f8a2916 --- /dev/null +++ b/scripts/terraform.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +label=$1 + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $current_dir/utils.sh + +main() { + # storing the refresh rate in the variable RATE, default is 5 + RATE=$(get_tmux_option "@monokai-refresh-rate" 5) + OUTPUT_STRING="N/A" + terraform_dir="$(tmux display-message -p '#{pane_current_path}')/.terraform" + if [ -d $terraform_dir ]; then + current_workspace=$(terraform workspace show 2>/dev/null) + OUTPUT_STRING="${current_workspace}" + fi + if [ "$label" = "" ] + then + echo "⚙️ ${OUTPUT_STRING}" + else + echo "⚙️ ${label} ${OUTPUT_STRING}" + fi + + sleep $RATE +} + +# run the main driver +main diff --git a/scripts/tmux_ram_info.sh b/scripts/tmux_ram_info.sh new file mode 100644 index 00000000..ea0c72a5 --- /dev/null +++ b/scripts/tmux_ram_info.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source "$current_dir/utils.sh" + +get_cpids_linux() { + local ppid="$1" + local cpids + local cpid + echo "$ppid" + cpids="$(pgrep -P "$ppid")" + for cpid in $cpids; do + get_cpids_linux "$cpid" + done +} + +get_cpids_unix() { + local ppid="$1" + local cpids + local cpid + echo "$ppid" + cpids="$(pgrep -aP "$ppid")" + for cpid in $cpids; do + get_cpids_unix "$cpid" + done +} + +kb_to_mb() { + if [ $# == 0 ]; then + read -r num + else + num="$1" + fi + bc <<< "scale=3;$num/1024" +} + +kb_to_gb() { + if [ $# == 0 ]; then + read -r num + else + num="$1" + fi + bc <<< "scale=6;$num/1048576" +} + +round() { + if [ $# == 1 ]; then + read -r num + scale="$1" + elif [ $# == 2 ]; then + num="$1" + scale="$2" + fi + printf "%.${scale}f" "${num}" +} + +get_tmux_ram_usage() +{ + local pid + local pids + local total_mem_kb=0 + local total_mem_mb=0 + local total_mem_gb=0 + pid="$(tmux display-message -pF '#{pid}')" + case $(uname -s) in + Linux) + if command -v pstree > /dev/null; then + pids="$(pstree -p "$pid" | tr -d '\n' | sed -rn -e 's/[^()]*\(([0-9]+)\)[^()]*/\1,/g' -e 's/,$//p')" + else + pids="$(get_cpids_linux "$pid" | tr '\n' ',')" + fi + total_mem_kb="$(ps -o rss= -p "$pids" | paste -sd+ | bc)" + ;; + + Darwin) + if command -v pstree > /dev/null; then + pids="$(pstree "$pid" | sed -En 's/[^0-9]+([0-9]+) .*/\1/p' | tr '\n' ',')" + else + pids="$(get_cpids_unix "$pid" | tr '\n' ',')" + fi + total_mem_kb="$(ps -o rss= -p "$pids" | paste -sd+ - | bc)" + ;; + + FreeBSD) + # TODO check FreeBSD compatibility + if command -v pstree > /dev/null; then + pids="$(pstree "$pid" | sed -En 's/[^0-9]+([0-9]+) .*/\1/p' | tr '\n' ',')" + else + pids="$(get_cpids_unix "$pid" | tr '\n' ',')" + fi + total_mem_kb="$(ps -o rss= -p "$pids" | paste -sd+ - | bc)" + ;; + + CYGWIN*|MINGW32*|MSYS*|MINGW*) + # TODO - windows compatability + ;; + esac + total_mem_mb=$(kb_to_mb "$total_mem_kb" | round 0) + total_mem_gb=$(kb_to_gb "$total_mem_kb" | round 0) + + if (( total_mem_gb > 0)); then + echo "${total_mem_gb}GB" + elif (( total_mem_mb > 0 )); then + echo "${total_mem_mb}MB" + else + echo "${total_mem_kb}kB" + fi +} + +main() +{ + ram_label=$(get_tmux_option "@monokai-tmux-ram-usage-label" "MEM") + ram_usage=$(get_tmux_ram_usage) + echo "$ram_label $ram_usage" +} + +#run main driver +main diff --git a/scripts/utils.sh b/scripts/utils.sh index 8ce42462..8f22e894 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -11,10 +11,21 @@ get_tmux_option() { fi } +get_tmux_window_option() { + local option=$1 + local default_value=$2 + local option_value=$(tmux show-window-options -v "$option") + if [ -z "$option_value" ]; then + echo $default_value + else + echo $option_value + fi +} + # normalize the percentage string to always have a length of 5 normalize_percent_len() { # the max length that the percent can reach, which happens for a two digit number with a decimal house: "99.9%" - max_len=3 + max_len=5 percent_len=${#1} let diff_len=$max_len-$percent_len # if the diff_len is even, left will have 1 more space than right @@ -22,4 +33,3 @@ normalize_percent_len() { let right_spaces=($diff_len)/2 printf "%${left_spaces}s%s%${right_spaces}s\n" "" $1 "" } - diff --git a/scripts/weather.sh b/scripts/weather.sh index 6debde58..e8bbd8f0 100755 --- a/scripts/weather.sh +++ b/scripts/weather.sh @@ -23,7 +23,7 @@ fetch_weather_information() { display_weather=$1 # it gets the weather condition textual name (%C), and the temperature (%t) - curl -sL wttr.in/$fixedlocation\?format="%C+%t$display_weather" + curl -sL wttr.in/${fixedlocation// /%20}\?format="%C+%t$display_weather" } #get weather display diff --git a/scripts/weather_wrapper.sh b/scripts/weather_wrapper.sh new file mode 100644 index 00000000..3d22fa5b --- /dev/null +++ b/scripts/weather_wrapper.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# setting the locale, some users have issues with different locales, this forces the correct one +export LC_ALL=en_US.UTF-8 + +#wrapper script for running weather on interval + +fahrenheit=$1 +location=$2 +fixedlocation=$3 + +DATAFILE=/tmp/.monokai-tmux-data +LAST_EXEC_FILE="/tmp/.monokai-tmux-weather-last-exec" +RUN_EACH=1200 +TIME_NOW=$(date +%s) +TIME_LAST=$(cat "${LAST_EXEC_FILE}" 2>/dev/null || echo "0") + +main() +{ + current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + + if [ "$(expr ${TIME_LAST} + ${RUN_EACH})" -lt "${TIME_NOW}" ]; then + # Run weather script here + $current_dir/weather.sh $fahrenheit $location "$fixedlocation" > "${DATAFILE}" + echo "${TIME_NOW}" > "${LAST_EXEC_FILE}" + fi + + cat "${DATAFILE}" +} + +#run main driver function +main From 1715a6aa38512ddb51891d43ef469a74f80ae8cb Mon Sep 17 00:00:00 2001 From: Marco Lehmann Date: Thu, 25 Apr 2024 16:37:58 +0200 Subject: [PATCH 2/6] revert to previous defaults for powerline and left icon --- scripts/monokai.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/monokai.sh b/scripts/monokai.sh index 19dd70f8..ed38b80f 100755 --- a/scripts/monokai.sh +++ b/scripts/monokai.sh @@ -16,9 +16,9 @@ main() show_fahrenheit=$(get_tmux_option "@monokai-show-fahrenheit" true) show_location=$(get_tmux_option "@monokai-show-location" true) fixed_location=$(get_tmux_option "@monokai-fixed-location") - show_powerline=$(get_tmux_option "@monokai-show-powerline" false) + show_powerline=$(get_tmux_option "@monokai-show-powerline" true) show_flags=$(get_tmux_option "@monokai-show-flags" false) - show_left_icon=$(get_tmux_option "@monokai-show-left-icon" smiley) + show_left_icon=$(get_tmux_option "@monokai-show-left-icon" session) show_left_icon_padding=$(get_tmux_option "@monokai-left-icon-padding" 1) show_military=$(get_tmux_option "@monokai-military-time" false) timezone=$(get_tmux_option "@monokai-set-timezone" "") From 37e62d6fcfdbc0667dccd021bd85ba29db1710ef Mon Sep 17 00:00:00 2001 From: Marco Lehmann Date: Thu, 25 Apr 2024 16:38:39 +0200 Subject: [PATCH 3/6] revert to previous defaults for left icon padding --- scripts/monokai.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/monokai.sh b/scripts/monokai.sh index ed38b80f..2d2c775d 100755 --- a/scripts/monokai.sh +++ b/scripts/monokai.sh @@ -19,7 +19,7 @@ main() show_powerline=$(get_tmux_option "@monokai-show-powerline" true) show_flags=$(get_tmux_option "@monokai-show-flags" false) show_left_icon=$(get_tmux_option "@monokai-show-left-icon" session) - show_left_icon_padding=$(get_tmux_option "@monokai-left-icon-padding" 1) + show_left_icon_padding=$(get_tmux_option "@monokai-left-icon-padding" 0) show_military=$(get_tmux_option "@monokai-military-time" false) timezone=$(get_tmux_option "@monokai-set-timezone" "") show_timezone=$(get_tmux_option "@monokai-show-timezone" true) From 1b4ea7ecd7a416d0d0b073f244a55256c95bc1f3 Mon Sep 17 00:00:00 2001 From: Marco Lehmann Date: Thu, 25 Apr 2024 17:13:18 +0200 Subject: [PATCH 4/6] ensure fixed width for network bandwidth to avoid jumps --- scripts/network_bandwidth.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/network_bandwidth.sh b/scripts/network_bandwidth.sh index 3001de74..8915f2f9 100755 --- a/scripts/network_bandwidth.sh +++ b/scripts/network_bandwidth.sh @@ -112,7 +112,7 @@ main() { IFS=" " read -ra bandwidth <<<"$(get_bandwidth "$network_name")" if [[ $show_interface == "true" ]]; then echo -n "[$network_name] "; fi - echo "↓ $(bandwidth_to_unit "${bandwidth[$DOWNLOAD]}") • ↑ $(bandwidth_to_unit "${bandwidth[$UPLOAD]}")" + printf "↓ %6s %-4s • ↑ %6s %-4s\n" $(bandwidth_to_unit "${bandwidth[$DOWNLOAD]}") $(bandwidth_to_unit "${bandwidth[$UPLOAD]}") ((counter = counter - 1)) sleep "$interval_update" From 49785370c76a2b728f1644342d9987ef30a2a0e4 Mon Sep 17 00:00:00 2001 From: Marco Lehmann Date: Thu, 25 Apr 2024 20:42:33 +0200 Subject: [PATCH 5/6] revert to previous defaults for list of plugins --- scripts/monokai.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/monokai.sh b/scripts/monokai.sh index 2d2c775d..333f1866 100755 --- a/scripts/monokai.sh +++ b/scripts/monokai.sh @@ -31,7 +31,7 @@ main() show_synchronize_panes_label=$(get_tmux_option "@monokai-synchronize-panes-label" "Sync") time_format=$(get_tmux_option "@monokai-time-format" "") show_ssh_session_port=$(get_tmux_option "@monokai-show-ssh-session-port" false) - IFS=' ' read -r -a plugins <<< $(get_tmux_option "@monokai-plugins" "battery network weather") + IFS=' ' read -r -a plugins <<< $(get_tmux_option "@monokai-plugins" "network-ping cpu-usage ram-usage") show_empty_plugins=$(get_tmux_option "@monokai-show-empty-plugins" true) # Monokai Pro Color Pallette From c40ac5bacdfb70548b0f14a5dec583763fad466b Mon Sep 17 00:00:00 2001 From: Max Petretta Date: Mon, 27 May 2024 19:41:31 -0400 Subject: [PATCH 6/6] adjust colors --- scripts/monokai.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/monokai.sh b/scripts/monokai.sh index 333f1866..7aa69dcf 100755 --- a/scripts/monokai.sh +++ b/scripts/monokai.sh @@ -128,10 +128,10 @@ main() # Status left if $show_powerline; then - tmux set-option -g status-left "#[fg=${green},bg=${black}]#{?client_prefix,#[fg=${yellow}],}#[bg=${green},fg=${black},bold]#{?client_prefix,#[bg=${yellow}],} ${left_icon} #[fg=${green},bg=${gray}]#{?client_prefix,#[fg=${yellow}],}${left_sep}" + tmux set-option -g status-left "#[fg=${green},bg=${black}]#{?client_prefix,#[fg=${magenta}],}#[bg=${green},fg=${black},bold]#{?client_prefix,#[bg=${magenta}],} ${left_icon} #[fg=${green},bg=${gray}]#{?client_prefix,#[fg=${magenta}],}${left_sep}" powerbg=${gray} else - tmux set-option -g status-left "#[bg=${green},fg=${black},bold]#{?client_prefix,#[bg=${yellow}],} ${left_icon} " + tmux set-option -g status-left "#[bg=${green},fg=${black},bold]#{?client_prefix,#[bg=${magenta}],} ${left_icon} " fi # Status right @@ -208,7 +208,7 @@ main() script="#($current_dir/network_bandwidth.sh)" elif [ $plugin = "network-ping" ]; then - IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-network-ping-colors" "blue black") + IFS=' ' read -r -a colors <<<$(get_tmux_option "@monokai-network-ping-colors" "gray white") script="#($current_dir/network_ping.sh)" elif [ $plugin = "network-vpn" ]; then