#!/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" } function myprint_quiet { #true or false local quiet=$1 local text="$2" if [ "$quiet" != true ]; then myprint "$text" fi } # print message and exit function error { myprint "${RED}$@" 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] ]]); } 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 { local color="$1" local character="$2" if [ -z "$color" ]; then color="${GRAY}" fi if [ -z "$character" ]; then character="-"; fi local term_width=$(tput cols) local line_length=$((term_width - 1)) printf "${color}" char_multiply "$character" $line_length 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 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" }