2020-06-01 08:10:10 +02:00
|
|
|
#!/usr/bin/env bash
|
2020-10-17 00:38:29 +02:00
|
|
|
# setting the locale, some users have issues with different locales, this forces the correct one
|
|
|
|
export LC_ALL=en_US.UTF-8
|
2020-06-01 08:10:10 +02:00
|
|
|
|
2020-08-10 14:04:51 +02:00
|
|
|
# function for getting the refresh rate
|
2020-08-10 13:57:48 +02:00
|
|
|
get_tmux_option() {
|
|
|
|
local option=$1
|
|
|
|
local default_value=$2
|
|
|
|
local option_value=$(tmux show-option -gqv "$option")
|
|
|
|
if [ -z $option_value ]; then
|
|
|
|
echo $default_value
|
|
|
|
else
|
|
|
|
echo $option_value
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-07-06 10:22:36 +02:00
|
|
|
# normalize the percentage string to always have a length of 5
|
2021-03-27 19:41:07 +01:00
|
|
|
normalize_percent_len() {
|
2021-07-06 10:22:36 +02:00
|
|
|
# the max length that the percent can reach, which happens for a two digit number with a decimal house: "99.9%"
|
|
|
|
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
|
|
|
|
let left_spaces=($diff_len+1)/2
|
|
|
|
let right_spaces=($diff_len)/2
|
|
|
|
printf "%${left_spaces}s%s%${right_spaces}s\n" "" $1 ""
|
2021-03-27 19:41:07 +01:00
|
|
|
}
|
|
|
|
|
2020-06-01 08:10:10 +02:00
|
|
|
get_percent()
|
|
|
|
{
|
2021-07-06 10:22:36 +02:00
|
|
|
case $(uname -s) in
|
|
|
|
Linux)
|
|
|
|
percent=$(LC_NUMERIC=en_US.UTF-8 top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
|
|
|
|
normalize_percent_len $percent
|
|
|
|
;;
|
2020-06-01 08:10:10 +02:00
|
|
|
|
2021-07-06 10:22:36 +02:00
|
|
|
Darwin)
|
|
|
|
cpuvalue=$(ps -A -o %cpu | awk -F. '{s+=$1} END {print s}')
|
|
|
|
cpucores=$(sysctl -n hw.logicalcpu)
|
|
|
|
cpuusage=$(( cpuvalue / cpucores ))
|
|
|
|
percent="$cpuusage%"
|
|
|
|
normalize_percent_len $percent
|
|
|
|
;;
|
|
|
|
|
|
|
|
CYGWIN*|MINGW32*|MSYS*|MINGW*)
|
|
|
|
# TODO - windows compatability
|
|
|
|
;;
|
|
|
|
esac
|
2020-06-01 08:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
{
|
2021-07-06 10:22:36 +02:00
|
|
|
# storing the refresh rate in the variable RATE, default is 5
|
|
|
|
RATE=$(get_tmux_option "@dracula-refresh-rate" 5)
|
|
|
|
cpu_percent=$(get_percent)
|
|
|
|
echo "CPU $cpu_percent"
|
|
|
|
sleep $RATE
|
2020-06-01 08:10:10 +02:00
|
|
|
}
|
2020-06-04 04:58:26 +02:00
|
|
|
|
|
|
|
# run main driver
|
2020-06-01 08:10:10 +02:00
|
|
|
main
|