From 446e4d8a6ca93076556baad9cf988451bf1f59f4 Mon Sep 17 00:00:00 2001 From: Abhishek Keshri Date: Tue, 9 Aug 2022 06:28:34 +0530 Subject: [PATCH] Add utils.sh --- scripts/utils.sh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 scripts/utils.sh diff --git a/scripts/utils.sh b/scripts/utils.sh new file mode 100644 index 0000000..a296192 --- /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 "" +} +