From 1ecc02e1489aec997f883a706bfb57e18db64d95 Mon Sep 17 00:00:00 2001 From: slange-dev Date: Sat, 4 Jan 2025 21:19:20 +0100 Subject: [PATCH] CPU and RAM percentage usage as 2-digit integer padded outputs [#17}(https://github.com/samoshkin/tmux-plugin-sysstat/pull/17) --- scripts/cpu.sh | 6 +++--- scripts/mem.sh | 34 +++++++++++++++++----------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/scripts/cpu.sh b/scripts/cpu.sh index adc2223..9ef7d6e 100755 --- a/scripts/cpu.sh +++ b/scripts/cpu.sh @@ -34,9 +34,9 @@ get_cpu_color(){ print_cpu_usage() { local cpu_pused=$(get_cpu_usage_or_collect) local cpu_color=$(get_cpu_color "$cpu_pused") - + local cpu_view="$cpu_view_tmpl" - cpu_view="${cpu_view//'#{cpu.pused}'/$(printf "%.1f%%" "$cpu_pused")}" + cpu_view="${cpu_view//'#{cpu.pused}'/$(printf "%2.0f%%" "$cpu_pused")}" cpu_view="${cpu_view//'#{cpu.color}'/$(echo "$cpu_color" | awk '{ print $1 }')}" cpu_view="${cpu_view//'#{cpu.color2}'/$(echo "$cpu_color" | awk '{ print $2 }')}" cpu_view="${cpu_view//'#{cpu.color3}'/$(echo "$cpu_color" | awk '{ print $3 }')}" @@ -60,7 +60,7 @@ start_cpu_collect_if_required() { if [ -f "$collect_cpu_pidfile" ] && ps -p "$(cat "$collect_cpu_pidfile")" > /dev/null 2>&1; then return; fi - + jobs >/dev/null 2>&1 "$CURRENT_DIR/cpu_collect.sh" &>/dev/null & if [ -n "$(jobs -n)" ]; then diff --git a/scripts/mem.sh b/scripts/mem.sh index 6a032bf..f805810 100755 --- a/scripts/mem.sh +++ b/scripts/mem.sh @@ -35,10 +35,10 @@ print_mem() { local mem_usage local scale local size_format - + if is_osx; then mem_usage=$(get_mem_usage_osx) - elif is_linux; then + elif is_linux; then mem_usage=$(get_mem_usage_linux) elif is_freebsd; then mem_usage=$(get_mem_usage_freebsd) @@ -53,16 +53,16 @@ print_mem() { local mem_total=$(echo "$mem_free + $mem_used" | calc) local mem_pused=$(echo "($mem_used / $mem_total) * 100" | calc) local mem_pfree=$(echo "($mem_free / $mem_total) * 100" | calc) - + # Calculate colors for mem and swap local mem_color=$(get_mem_color "$mem_pused") - + local mem_view="$mem_view_tmpl" mem_view="${mem_view//'#{mem.used}'/$(printf "$size_format" "$mem_used" "$size_unit")}" - mem_view="${mem_view//'#{mem.pused}'/$(printf "%.0f%%" "$mem_pused")}" + mem_view="${mem_view//'#{mem.pused}'/$(printf "%2.0f%%" "$mem_pused")}" mem_view="${mem_view//'#{mem.free}'/$(printf "$size_format" "$mem_free" "$size_unit")}" - mem_view="${mem_view//'#{mem.pfree}'/$(printf "%.0f%%" "$mem_pfree")}" - mem_view="${mem_view//'#{mem.total}'/$(printf "$size_format" "$mem_total" "$size_unit")}" + mem_view="${mem_view//'#{mem.pfree}'/$(printf "%2.0f%%" "$mem_pfree")}" + mem_view="${mem_view//'#{mem.total}'/$(printf "$size_format" "$mem_total" "$size_unit")}" mem_view="${mem_view//'#{mem.color}'/$(echo "$mem_color" | awk '{ print $1 }')}" mem_view="${mem_view//'#{mem.color2}'/$(echo "$mem_color" | awk '{ print $2 }')}" mem_view="${mem_view//'#{mem.color3}'/$(echo "$mem_color" | awk '{ print $3 }')}" @@ -76,19 +76,19 @@ print_mem() { # free = free + inactive + speculative + occupied by compressor # see `vm_stat` command get_mem_usage_osx(){ - + local page_size=$(sysctl -nq "vm.pagesize") vm_stat | awk -v page_size=$page_size -F ':' ' BEGIN { free=0; used=0 } - - /Pages active/ || - /Pages wired/ { + + /Pages active/ || + /Pages wired/ { gsub(/^[ \t]+|[ \t]+$/, "", $2); used+=$2; } - /Pages free/ || - /Pages inactive/ || - /Pages speculative/ || - /Pages occupied by compressor/ { + /Pages free/ || + /Pages inactive/ || + /Pages speculative/ || + /Pages occupied by compressor/ { gsub(/^[ \t]+|[ \t]+$/, "", $2); free+=$2; } @@ -103,7 +103,7 @@ get_mem_usage_freebsd(){ # Method #1. Sum up free+buffers+cached, treat it as "available" memory, assuming buff+cache can be reclaimed. Note, that this assumption is not 100% correct, buff+cache most likely cannot be 100% reclaimed, but this is how memory calculation is used to be done on Linux -# Method #2. If "MemAvailable" is provided by system, use it. This is more correct method, because we're not relying on fragile "free+buffer+cache" equation. +# Method #2. If "MemAvailable" is provided by system, use it. This is more correct method, because we're not relying on fragile "free+buffer+cache" equation. # See: Interpreting /proc/meminfo and free output for Red Hat Enterprise Linux 5, 6 and 7 - Red Hat Customer Portal - https://access.redhat.com/solutions/406773 @@ -112,7 +112,7 @@ get_mem_usage_linux(){