#!/bin/bash
# Build ASCII "picture" by putting output of figlet and catimg together
# (C) 2020 - F-Hauri.ch
# Licensed by GNU GENERAL PUBLIC LICENSE Version 3
# This script use (require) `catimg` and `figlet` to produce kind of merge
# of both output together.
# If sourced, produce no output but declare mkImageText function.
mkImageText() {
local -a lhs rhs
local -i OPTIND=0 width=${COLUMNS} prcent=50 sep=1 lwidth rwidth godown goup
local OPTARG usage text='' image='' toggle=false figletopt='' endstr
if ! ((width)) ;then
type tput &>/dev/null && width=$(tput cols) ||
width=80
fi
usage=$(cat <<-EOF
Usage:
${0##*/} [-h] [-t] [-w NUM] [-p NUM] [-s NUM] [-f STR]
-h Print this help.
-t Toggle Text on left and Image on right side.
-w NUM Total Width of output (default: $width)
-p NUM Percent of Width to use for Image (default: $prcent)
-s NUM Separation between Image and Text (default: $sep)
-f STR Option for "figlet" command (sample: "-f smslant -r")
Image file name (argument required)
Text string (argument required)
EOF
)
while getopts "hts:f:w:p:" opt;do
case $opt in
h ) echo "$usage" ; exit 0;;
t ) toggle=true ;;
w ) width=$OPTARG ;;
s ) sep=$OPTARG ;;
f ) figletopt=$OPTARG ;;
p ) prcent=$OPTARG ;;
* ) echo >&2 "$0 ERROR: $opt not valid option."
echo "$usage"
exit 1 ;;
esac
done
shift $[OPTIND-1]
image="$1" text="$2"
lwidth=$(( ( width - sep ) * prcent +50 ))
lwidth=${lwidth::-2}
rwidth=$(( width - sep - lwidth ))
if $toggle;then
mapfile lhs < <(figlet -w$lwidth $figletopt "$text")
mapfile rhs < <(catimg -w$(( 2 * rwidth )) -r2 "$image")
endstr=("${rhs[${#rhs[@]}-1]}")
rhs=("${rhs[@]:: ${#rhs[@]}-1}")
goup=$(( ${#lhs[@]} ))
godown=$(( ${#lhs[@]}-${#rhs[@]} ))
else
mapfile lhs < <(catimg -w$(( 2 * lwidth )) -r2 "$image")
mapfile rhs < <(figlet -w$rwidth $figletopt "$text")
goup=$((${#lhs[@]}-1))
godown=$(( ${#lhs[@]}-${#rhs[@]} -1 ))
fi
printf "%s" "${lhs[@]}"
printf "\e[${goup}A"
printf "\e[$((lwidth+sep))C%s" "${rhs[@]}"
((godown)) && printf "\e[${godown}B"
$toggle && printf "%s" "$endstr"
}
# End of definition
# Execute function if not sourced.
[ "$0" = "$BASH_SOURCE" ] && mkImageText "$@"