From 3029934567b0d09c68dce0ce69892940ae5b481e Mon Sep 17 00:00:00 2001 From: kangarooo Date: Mon, 15 Mar 2021 17:09:08 +0200 Subject: [PATCH 1/5] Added SWAP percentage --- README.md | 7 +++++++ cpu.tmux | 8 ++++++++ scripts/swap_bg_color.sh | 37 +++++++++++++++++++++++++++++++++++ scripts/swap_fg_color.sh | 37 +++++++++++++++++++++++++++++++++++ scripts/swap_icon.sh | 39 +++++++++++++++++++++++++++++++++++++ scripts/swap_percentage.sh | 40 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 168 insertions(+) create mode 100644 scripts/swap_bg_color.sh create mode 100644 scripts/swap_fg_color.sh create mode 100644 scripts/swap_icon.sh create mode 100644 scripts/swap_percentage.sh diff --git a/README.md b/README.md index 51f627c..2c229ec 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,13 @@ GPU equivalents also exist: - `#{gpu_temp_bg_color}` - will change the background color based on the GPU temperature - `#{gpu_temp_fg_color}` - will change the foreground color based on the GPU temperature +SWAP percentage: + +- `#{swap_icon}` - will display a SWAP status icon +- `#{swap_percentage}` - will show SWAP percentage (averaged across cores) +- `#{swap_bg_color}` - will change the background color based on the SWAP percentage +- `#{swap_fg_color}` - will change the foreground color based on the SWAP percentage + ## Examples CPU usage lower than 30%:
diff --git a/cpu.tmux b/cpu.tmux index 576c206..619e86d 100755 --- a/cpu.tmux +++ b/cpu.tmux @@ -29,6 +29,10 @@ cpu_interpolation=( "\#{gpu_temp_icon}" "\#{gpu_temp_bg_color}" "\#{gpu_temp_fg_color}" + "\#{swap_percentage}" + "\#{swap_percentage_icon}" + "\#{swap_percentage_bg_color}" + "\#{swap_percentage_fg_color}" ) cpu_commands=( "#($CURRENT_DIR/scripts/cpu_percentage.sh)" @@ -55,6 +59,10 @@ cpu_commands=( "#($CURRENT_DIR/scripts/gpu_temp_icon.sh)" "#($CURRENT_DIR/scripts/gpu_temp_bg_color.sh)" "#($CURRENT_DIR/scripts/gpu_temp_fg_color.sh)" + "#($CURRENT_DIR/scripts/swap_percentage.sh)" + "#($CURRENT_DIR/scripts/swap_icon.sh)" + "#($CURRENT_DIR/scripts/swap_bg_color.sh)" + "#($CURRENT_DIR/scripts/swap_fg_color.sh)" ) set_tmux_option() { diff --git a/scripts/swap_bg_color.sh b/scripts/swap_bg_color.sh new file mode 100644 index 0000000..31bc200 --- /dev/null +++ b/scripts/swap_bg_color.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/helpers.sh" + +swap_low_bg_color="" +swap_medium_bg_color="" +swap_high_bg_color="" + +swap_low_default_bg_color="#[bg=green]" +swap_medium_default_bg_color="#[bg=yellow]" +swap_high_default_bg_color="#[bg=red]" + +get_bg_color_settings() { + swap_low_bg_color=$(get_tmux_option "@swap_low_bg_color" "$swap_low_default_bg_color") + swap_medium_bg_color=$(get_tmux_option "@swap_medium_bg_color" "$swap_medium_default_bg_color") + swap_high_bg_color=$(get_tmux_option "@swap_high_bg_color" "$swap_high_default_bg_color") +} + +print_bg_color() { + local swap_percentage=$($CURRENT_DIR/swap_percentage.sh | sed -e 's/%//') + local load_status=$(load_status $swap_percentage) + if [ $load_status == "low" ]; then + echo "$swap_low_bg_color" + elif [ $load_status == "medium" ]; then + echo "$swap_medium_bg_color" + elif [ $load_status == "high" ]; then + echo "$swap_high_bg_color" + fi +} + +main() { + get_bg_color_settings + print_bg_color +} +main diff --git a/scripts/swap_fg_color.sh b/scripts/swap_fg_color.sh new file mode 100644 index 0000000..3e74b83 --- /dev/null +++ b/scripts/swap_fg_color.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/helpers.sh" + +swap_low_fg_color="" +swap_medium_fg_color="" +swap_high_fg_color="" + +swap_low_default_fg_color="#[fg=green]" +swap_medium_default_fg_color="#[fg=yellow]" +swap_high_default_fg_color="#[fg=red]" + +get_fg_color_settings() { + swap_low_fg_color=$(get_tmux_option "@swap_low_fg_color" "$swap_low_default_fg_color") + swap_medium_fg_color=$(get_tmux_option "@swap_medium_fg_color" "$swap_medium_default_fg_color") + swap_high_fg_color=$(get_tmux_option "@swap_high_fg_color" "$swap_high_default_fg_color") +} + +print_fg_color() { + local swap_percentage=$($CURRENT_DIR/swap_percentage.sh | sed -e 's/%//') + local load_status=$(load_status $swap_percentage) + if [ $load_status == "low" ]; then + echo "$swap_low_fg_color" + elif [ $load_status == "medium" ]; then + echo "$swap_medium_fg_color" + elif [ $load_status == "high" ]; then + echo "$swap_high_fg_color" + fi +} + +main() { + get_fg_color_settings + print_fg_color +} +main diff --git a/scripts/swap_icon.sh b/scripts/swap_icon.sh new file mode 100644 index 0000000..f04a7d7 --- /dev/null +++ b/scripts/swap_icon.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/helpers.sh" + +# script global variables +swap_low_icon="" +swap_medium_icon="" +swap_high_icon="" + +swap_low_default_icon="=" +swap_medium_default_icon="≡" +swap_high_default_icon="≣" + +# icons are set as script global variables +get_icon_settings() { + swap_low_icon=$(get_tmux_option "@swap_low_icon" "$swap_low_default_icon") + swap_medium_icon=$(get_tmux_option "@swap_medium_icon" "$swap_medium_default_icon") + swap_high_icon=$(get_tmux_option "@swap_high_icon" "$swap_high_default_icon") +} + +print_icon() { + local swap_percentage=$($CURRENT_DIR/swap_percentage.sh | sed -e 's/%//') + local load_status=$(load_status $swap_percentage) + if [ $load_status == "low" ]; then + echo "$swap_low_icon" + elif [ $load_status == "medium" ]; then + echo "$swap_medium_icon" + elif [ $load_status == "high" ]; then + echo "$swap_high_icon" + fi +} + +main() { + get_icon_settings + print_icon "$1" +} +main diff --git a/scripts/swap_percentage.sh b/scripts/swap_percentage.sh new file mode 100644 index 0000000..54c1f76 --- /dev/null +++ b/scripts/swap_percentage.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/helpers.sh" + +swap_percentage_format="%3.1f%%" + +print_swap_percentage() { + swap_percentage_format=$(get_tmux_option "@swap_percentage_format" "$swap_percentage_format") + + if command_exists "iostat"; then + + if is_linux_iostat; then + free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}' + elif is_osx; then + free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}' + elif is_freebsd || is_openbsd; then + free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}' + else + echo "Unknown iostat version please create an issue" + fi + elif command_exists "sar"; then + free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}' + else + if is_cygwin; then + usage="$(cached_eval WMIC swap get LoadPercentage | grep -Eo '^[0-9]+')" + printf "$swap_percentage_format" "$usage" + else + load=`cached_eval ps -aux | awk '{print $3}' | tail -n+2 | awk '{s+=$1} END {print s}'` + swaps=$(swaps_number) + echo "$load $swaps" | awk -v format="$swap_percentage_format" '{printf format, $1/$2}' + fi + fi +} + +main() { + print_swap_percentage +} +main From b5c41ad9b5a9dc237169b96bfa94e239c0d602e5 Mon Sep 17 00:00:00 2001 From: kangarooo Date: Mon, 15 Mar 2021 19:25:01 +0200 Subject: [PATCH 2/5] Update swap_percentage.sh --- scripts/swap_percentage.sh | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/scripts/swap_percentage.sh b/scripts/swap_percentage.sh index 54c1f76..d0f42ae 100644 --- a/scripts/swap_percentage.sh +++ b/scripts/swap_percentage.sh @@ -9,29 +9,15 @@ swap_percentage_format="%3.1f%%" print_swap_percentage() { swap_percentage_format=$(get_tmux_option "@swap_percentage_format" "$swap_percentage_format") - if command_exists "iostat"; then - - if is_linux_iostat; then - free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}' - elif is_osx; then - free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}' - elif is_freebsd || is_openbsd; then - free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}' - else - echo "Unknown iostat version please create an issue" - fi - elif command_exists "sar"; then + if command_exists "free"; then free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}' + elif command_exists "cuda-smi"; then + loads=$(cached_eval cuda-smi | sed -nr 's/.*\s([0-9.]+) of ([0-9.]+) MB.*/\1 \2/p' | awk '{print $2-$1" "$2}') else - if is_cygwin; then - usage="$(cached_eval WMIC swap get LoadPercentage | grep -Eo '^[0-9]+')" - printf "$swap_percentage_format" "$usage" - else - load=`cached_eval ps -aux | awk '{print $3}' | tail -n+2 | awk '{s+=$1} END {print s}'` - swaps=$(swaps_number) - echo "$load $swaps" | awk -v format="$swap_percentage_format" '{printf format, $1/$2}' - fi + echo "No GPU" + return fi + echo "$loads" | awk -v format="$swap_percentage_format" '{used+=$1; tot+=$2} END {printf format, 100*$1/$2}' } main() { From 6e833708b41560069eb4feae7e11bf54e10e9e6e Mon Sep 17 00:00:00 2001 From: kangarooo Date: Mon, 15 Mar 2021 19:26:41 +0200 Subject: [PATCH 3/5] Swap added --- scripts/swap_percentage.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/swap_percentage.sh b/scripts/swap_percentage.sh index d0f42ae..9df63c5 100644 --- a/scripts/swap_percentage.sh +++ b/scripts/swap_percentage.sh @@ -14,7 +14,7 @@ print_swap_percentage() { elif command_exists "cuda-smi"; then loads=$(cached_eval cuda-smi | sed -nr 's/.*\s([0-9.]+) of ([0-9.]+) MB.*/\1 \2/p' | awk '{print $2-$1" "$2}') else - echo "No GPU" + echo "No SWAP" return fi echo "$loads" | awk -v format="$swap_percentage_format" '{used+=$1; tot+=$2} END {printf format, 100*$1/$2}' From b8fc2616ebfd6ca13d09058aab16f203cc18823b Mon Sep 17 00:00:00 2001 From: kangarooo Date: Mon, 15 Mar 2021 19:27:23 +0200 Subject: [PATCH 4/5] Swap added --- scripts/swap_percentage.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/swap_percentage.sh b/scripts/swap_percentage.sh index 9df63c5..f49046b 100644 --- a/scripts/swap_percentage.sh +++ b/scripts/swap_percentage.sh @@ -12,7 +12,7 @@ print_swap_percentage() { if command_exists "free"; then free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}' elif command_exists "cuda-smi"; then - loads=$(cached_eval cuda-smi | sed -nr 's/.*\s([0-9.]+) of ([0-9.]+) MB.*/\1 \2/p' | awk '{print $2-$1" "$2}') + free -t | awk 'NR == 3 {printf("Current Swap Utilization is : %.2f%"), $3/$2*100}' else echo "No SWAP" return From 83d8f9d488eead33c78b528b56feebd741a042e5 Mon Sep 17 00:00:00 2001 From: kangarooo Date: Mon, 15 Mar 2021 19:30:11 +0200 Subject: [PATCH 5/5] Added swap option and ram --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c229ec..448dcc8 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Example: ```shell # in .tmux.conf -set -g status-right '#{cpu_bg_color} CPU: #{cpu_icon} #{cpu_percentage} | %a %h-%d %H:%M ' +set -g status-right '#{cpu_bg_color} CPU: #{cpu_icon} #{cpu_percentage} | #{ram_bg_color} RAM: #{ram_icon} #{ram_percentage} | #{swap_bg_color} SWAP: #{swap_icon} #{swap_percentage} | %a %h-%d %H:%M ' ``` ### Supported Options