Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add load average support #52

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down
8 changes: 8 additions & 0 deletions cpu.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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)"
Expand Down
54 changes: 54 additions & 0 deletions scripts/load.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/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

# Replace commas with dots
echo -n "${output//,/.}"
}

main() {
print_load "$@"
}

main "$@"