optimized line printing
This commit is contained in:
parent
4b4794e253
commit
f4c12e9b0e
34
myprint.sh
34
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"
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user