get_tmux_option(), normalize_percent_len() in scripts/utils.sh

source scripts/utils.sh in dracula.sh, cpu_info.sh, gpu_usage.sh, ram_info.sh
normalize_percent_len for ram_info
custom labels for cpu-usage, gpu-usage, ram-usage
This commit is contained in:
Sabato Luca Guadagno 2021-07-06 11:21:21 +02:00
parent 151cd3e45f
commit 87df275c6e
5 changed files with 42 additions and 48 deletions

View file

@ -2,29 +2,8 @@
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8
# function for getting the refresh rate
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
}
# normalize the percentage string to always have a length of 5
normalize_percent_len() {
# 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 ""
}
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $current_dir/utils.sh
get_percent()
{
@ -52,8 +31,10 @@ main()
{
# storing the refresh rate in the variable RATE, default is 5
RATE=$(get_tmux_option "@dracula-refresh-rate" 5)
cpu_label=$(get_tmux_option "@dracula-cpu-usage-label" "CPU")
cpu_percent=$(get_percent)
echo "CPU $cpu_percent"
echo "$cpu_label $cpu_percent"
# echo "$current_dir"
sleep $RATE
}

View file

@ -2,16 +2,8 @@
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8
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
}
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $current_dir/utils.sh
main()
{

View file

@ -2,17 +2,8 @@
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8
# function for getting the refresh rate
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
}
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $current_dir/utils.sh
get_platform()
{
@ -31,6 +22,7 @@ get_platform()
;;
esac
}
get_gpu()
{
gpu=$(get_platform)
@ -39,15 +31,18 @@ get_gpu()
else
usage='unknown'
fi
echo $usage
normalize_percent_len $usage
}
main()
{
# storing the refresh rate in the variable RATE, default is 5
RATE=$(get_tmux_option "@dracula-refresh-rate" 5)
gpu_label=$(get_tmux_option "@dracula-gpu-usage-label" "GPU")
gpu_usage=$(get_gpu)
echo "GPU $gpu_usage"
echo "$gpu_label $gpu_usage"
sleep $RATE
}
# run the main driver
main

View file

@ -77,8 +77,9 @@ main()
{
# storing the refresh rate in the variable RATE, default is 5
RATE=$(get_tmux_option "@dracula-refresh-rate" 5)
ram_label=$(get_tmux_option "@dracula-ram-usage-label" "RAM")
ram_percent=$(get_percent)
echo "RAM $ram_percent"
echo "$ram_label $ram_percent"
sleep $RATE
}

25
scripts/utils.sh Normal file
View file

@ -0,0 +1,25 @@
#!/usr/bin/env bash
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
}
# normalize the percentage string to always have a length of 5
normalize_percent_len() {
# 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 ""
}