From 29738fe7788d3a6ef12fb6037ae52f513d8ee7c4 Mon Sep 17 00:00:00 2001 From: Philipp Schmitt Date: Sat, 30 May 2020 17:40:23 +0200 Subject: [PATCH 1/4] Add load average support --- README.md | 6 ++++++ cpu.tmux | 8 ++++++++ scripts/load.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100755 scripts/load.sh diff --git a/README.md b/README.md index 6c35a54..fd24c07 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,10 @@ This is done by introducing 8 new format strings that can be added to - `#{cpu_percentage}` - will show CPU percentage (averaged across cores) - `#{cpu_bg_color}` - will change the background color based on the CPU percentage - `#{cpu_fg_color}` - will change the foreground color based on the CPU percentage + - `#{load}` - will display your current load averages (1, 5 and 15 minutes) + - `#{load1}` - will display the load average of the last minute + - `#{load5}` - will display the load average of the last 5 minutes + - `#{load15}` - will display the load average of the last 15 minutes - `#{ram_icon}` - will display a RAM status icon - `#{ram_percentage}` - will show RAM percentage (averaged across cores) - `#{ram_bg_color}` - will change the background color based on the RAM percentage @@ -111,6 +115,8 @@ Here are all available options with their default values: @cpu_high_bg_color "#[bg=red]" # background color when cpu is high @cpu_percentage_format "%3.1f%%" # printf format to use to display percentage + +@load_per_cpu_core "false" # if set to "true" the load averages will be divided by the number of CPU cores ``` Same options are valid with `@gpu` diff --git a/cpu.tmux b/cpu.tmux index ef8e41a..b084eaf 100755 --- a/cpu.tmux +++ b/cpu.tmux @@ -13,6 +13,10 @@ cpu_interpolation=( "\#{gpu_icon}" "\#{gpu_bg_color}" "\#{gpu_fg_color}" + "\#{load}" + "\#{load1}" + "\#{load5}" + "\#{load15}" "\#{ram_percentage}" "\#{ram_icon}" "\#{ram_bg_color}" @@ -31,6 +35,10 @@ cpu_commands=( "#($CURRENT_DIR/scripts/gpu_icon.sh)" "#($CURRENT_DIR/scripts/gpu_bg_color.sh)" "#($CURRENT_DIR/scripts/gpu_fg_color.sh)" + "#($CURRENT_DIR/scripts/load.sh)" + "#($CURRENT_DIR/scripts/load.sh 1)" + "#($CURRENT_DIR/scripts/load.sh 5)" + "#($CURRENT_DIR/scripts/load.sh 15)" "#($CURRENT_DIR/scripts/ram_percentage.sh)" "#($CURRENT_DIR/scripts/ram_icon.sh)" "#($CURRENT_DIR/scripts/ram_bg_color.sh)" diff --git a/scripts/load.sh b/scripts/load.sh new file mode 100755 index 0000000..396b4a3 --- /dev/null +++ b/scripts/load.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/helpers.sh" + +get_number_of_cores(){ + if is_osx + then + sysctl -n hw.ncpu + else + nproc + fi +} + + +print_load() { + local num_cores=1 + local output + + case "$(get_tmux_option "@load_per_cpu_core")" in + true|1|yes) + num_cores=$(get_number_of_cores) + ;; + esac + + output=$(uptime | awk -v num_cores="$num_cores" '{ + sub(/,$/, "", $(NF-2)); + sub(/,$/, "", $(NF-1)); + sub(/,$/, "", $NF); + printf "%.2f %.2f %.2f", $(NF-2)/num_cores, $(NF-1)/num_cores, $NF/num_cores + }') + + case "$1" in + 1) + output="$(awk '{ print $1 }' <<< "$output")" + ;; + 5) + output="$(awk '{ print $2 }' <<< "$output")" + ;; + 15) + output="$(awk '{ print $3 }' <<< "$output")" + ;; + esac + + echo -n "$output" +} + +main() { + print_load "$@" +} + +main "$@" From b8ac9cac6bf7a05425d9b9b29e04362e9ae71ca4 Mon Sep 17 00:00:00 2001 From: Philipp Schmitt Date: Tue, 28 Jul 2020 19:06:35 +0200 Subject: [PATCH 2/4] Replace commas with dots in uptime output to avoid ending up with truncated floats --- scripts/load.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/load.sh b/scripts/load.sh index 396b4a3..a0653ed 100755 --- a/scripts/load.sh +++ b/scripts/load.sh @@ -28,6 +28,9 @@ print_load() { sub(/,$/, "", $(NF-2)); sub(/,$/, "", $(NF-1)); sub(/,$/, "", $NF); + sub(/,/, ".", $(NF-2)); + sub(/,/, ".", $(NF-1)); + sub(/,/, ".", $(NF)); printf "%.2f %.2f %.2f", $(NF-2)/num_cores, $(NF-1)/num_cores, $NF/num_cores }') From f15e7853f46d9a2595472f7c3760c492b4a56daf Mon Sep 17 00:00:00 2001 From: Philipp Schmitt Date: Tue, 20 Oct 2020 09:14:41 +0200 Subject: [PATCH 3/4] Fix comma replacement --- scripts/load.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/scripts/load.sh b/scripts/load.sh index a0653ed..14264fc 100755 --- a/scripts/load.sh +++ b/scripts/load.sh @@ -28,9 +28,6 @@ print_load() { sub(/,$/, "", $(NF-2)); sub(/,$/, "", $(NF-1)); sub(/,$/, "", $NF); - sub(/,/, ".", $(NF-2)); - sub(/,/, ".", $(NF-1)); - sub(/,/, ".", $(NF)); printf "%.2f %.2f %.2f", $(NF-2)/num_cores, $(NF-1)/num_cores, $NF/num_cores }') @@ -44,9 +41,14 @@ print_load() { 15) output="$(awk '{ print $3 }' <<< "$output")" ;; + *) + echo "Invalid input." >&2 + return 2 + ;; esac - echo -n "$output" + # Replace commas with dots + echo -n "${output//,/.}" } main() { From 3d52c5fd425a5d08ebb6fae3ca85bc41e76f562b Mon Sep 17 00:00:00 2001 From: Philipp Schmitt Date: Tue, 20 Oct 2020 09:17:26 +0200 Subject: [PATCH 4/4] Fix #{load} display --- scripts/load.sh | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/load.sh b/scripts/load.sh index 14264fc..ea0bd13 100755 --- a/scripts/load.sh +++ b/scripts/load.sh @@ -41,10 +41,6 @@ print_load() { 15) output="$(awk '{ print $3 }' <<< "$output")" ;; - *) - echo "Invalid input." >&2 - return 2 - ;; esac # Replace commas with dots