From f4c12e9b0ef945caec455e725e464c15964cd250 Mon Sep 17 00:00:00 2001 From: Timerix Date: Fri, 19 Jul 2024 01:43:57 +0300 Subject: [PATCH] optimized line printing --- myprint.sh | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/myprint.sh b/myprint.sh index 2ba9d8a..2c63670 100644 --- a/myprint.sh +++ b/myprint.sh @@ -21,6 +21,14 @@ function myprint { printf "${GRAY}$@${GRAY}\n" } +function myprint_quiet { + local quiet=$1 + local text="$2" + if [ "$quiet" != true ]; then + myprint "$text" + fi +} + # print message and exit function error { myprint "${RED}$@" @@ -36,6 +44,16 @@ function ask_yn { return $([[ "$answ" = [Yy] ]]); } +function char_multiply { + local character="$1" + local length="$2" + i=0 + while [ $i -lt $length ]; do + printf $character + i=$((i+1)) + done +} + # prints horizontal line occupying whole terminal row # https://en.wikipedia.org/wiki/Box-drawing_characters function print_hline { @@ -47,7 +65,10 @@ function print_hline { if [ -z "$character" ]; then character="-"; fi - printf "${color}%.s${character}" $(seq 2 $(tput cols)) + local term_width=$(tput cols) + local line_length=$((term_width - 1)) + printf "${color}" + char_multiply "$character" $line_length printf "${GRAY}\n" } @@ -65,10 +86,11 @@ function print_header { local term_width=$(tput cols) local label_length=${#label} local line_characters_count=$((term_width - label_length - 2)) - local letf_line_length=$(( line_characters_count / 2 )) - local right_line_length=$(( letf_line_length + line_characters_count % 2 )) - printf "${color}%.s${character}" $(seq 1 $letf_line_length) - printf "[${label}]" - printf "${color}%.s${character}" $(seq 2 $right_line_length) + local left_line_length=$(( line_characters_count / 2 )) + local right_line_length=$(( left_line_length - 1 + line_characters_count % 2 )) + printf "${color}" + char_multiply "$character" $left_line_length + printf "[${label}]${color}" + char_multiply "$character" $right_line_length printf "${GRAY}\n" }