83 lines
1.9 KiB
Bash
83 lines
1.9 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
BLACK='\033[0;30m'
|
|
GRAY='\033[0;37m'
|
|
WHITE='\033[0;97m'
|
|
RED='\033[0;91m'
|
|
GREEN='\033[0;92m'
|
|
YELLOW='\033[0;93m'
|
|
BLUE='\033[0;94m'
|
|
PURPLE='\033[0;95m'
|
|
CYAN='\033[0;96m'
|
|
|
|
# prints text without special characters
|
|
# use it to return string values from functions
|
|
function safeprint {
|
|
printf "%s" "$@"
|
|
}
|
|
|
|
# prints text with special characters and resets color
|
|
function myprint {
|
|
printf "${GRAY}$@${GRAY}\n"
|
|
}
|
|
|
|
# print message and exit
|
|
function error {
|
|
myprint "$@"
|
|
exit 1
|
|
}
|
|
|
|
# asks a question with two options and returns 0 or 1
|
|
# usage: `if ask_yn "do something?"`
|
|
function ask_yn {
|
|
local answ=""
|
|
myprint "$@ [y/n]"
|
|
read -r answ
|
|
return $([[ "$answ" = [Yy] ]]);
|
|
}
|
|
|
|
# asks a question and prints answer
|
|
function ask {
|
|
local answ=""
|
|
myprint "$@: "
|
|
read -r answ
|
|
safeprint "$answ"
|
|
}
|
|
|
|
# prints horizontal line occupying whole terminal row
|
|
# https://en.wikipedia.org/wiki/Box-drawing_characters
|
|
function print_hline {
|
|
local color="$1"
|
|
local character="$2"
|
|
if [ -z "$color" ]; then
|
|
color="${GRAY}"
|
|
fi
|
|
if [ -z "$character" ]; then
|
|
character="-";
|
|
fi
|
|
printf "${color}%.s${character}" $(seq 2 $(tput cols))
|
|
printf "${GRAY}\n"
|
|
}
|
|
|
|
# prints horizontal line occupying whole terminal row with a given label
|
|
function print_header {
|
|
local color="$1"
|
|
local character="$2"
|
|
local label="$3"
|
|
if [ -z "$color" ]; then
|
|
color="${GRAY}"
|
|
fi
|
|
if [ -z "$character" ]; then
|
|
character="-";
|
|
fi
|
|
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)
|
|
printf "${GRAY}\n"
|
|
}
|