From 87df275c6e669cd861f4e566f0d089b8f88393a8 Mon Sep 17 00:00:00 2001 From: Sabato Luca Guadagno Date: Tue, 6 Jul 2021 11:21:21 +0200 Subject: [PATCH] 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 --- scripts/cpu_info.sh | 29 +++++------------------------ scripts/dracula.sh | 12 ++---------- scripts/gpu_usage.sh | 21 ++++++++------------- scripts/ram_info.sh | 3 ++- scripts/utils.sh | 25 +++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 48 deletions(-) create mode 100644 scripts/utils.sh diff --git a/scripts/cpu_info.sh b/scripts/cpu_info.sh index 675b234..b66ee8c 100755 --- a/scripts/cpu_info.sh +++ b/scripts/cpu_info.sh @@ -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 } diff --git a/scripts/dracula.sh b/scripts/dracula.sh index 7729800..8156c1c 100755 --- a/scripts/dracula.sh +++ b/scripts/dracula.sh @@ -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() { diff --git a/scripts/gpu_usage.sh b/scripts/gpu_usage.sh index fe69814..9ffc647 100755 --- a/scripts/gpu_usage.sh +++ b/scripts/gpu_usage.sh @@ -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 diff --git a/scripts/ram_info.sh b/scripts/ram_info.sh index f7ed9b6..625ad61 100755 --- a/scripts/ram_info.sh +++ b/scripts/ram_info.sh @@ -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 } diff --git a/scripts/utils.sh b/scripts/utils.sh new file mode 100644 index 0000000..33a47d0 --- /dev/null +++ b/scripts/utils.sh @@ -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 "" +} +