#!/bin/bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)print-header 3.2.0 12.08.2024 (c)2024 Standby.cloud # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ # # This script can be used free of charge. Use it as is or customize as needed. It is not guaranteed to be # error free and you will not be reimbursed for any damage it may cause. # #@ Reads a file and pretty prints a frame around the content. #@ #@Usage: print-header [options] #@ Options: #@ -h, --help : Displays helptext. #@ -v, --version : Displays the version of the script. #@ -i, --import : Read from a file ( = filename) - if not specified, read from stdin. #@ -c, --color : Green colorcode e.g.: "\033[0;32m". #@ #@Examples: #@ #@ echo "Hello world" | print-header #@ #@ Displays: #@ #@ /-------------\ #@ Hello world #@ \-------------/ # # Exit codes: # 01: Unknown or wrong parameter. # 02: No file # 99: User interrupt. # # See also: # **print-table**(1), **install-scripts**(1) # # Update history: # # V 2.0.0 11.06.2019 New version # V 3.0.0 26.04.2020 Multi OS compatible # V 3.0.1 11.06.2020 Using library # V 3.0.2 20.04.2021 Rewritten using bash instead of tcsh # V 3.1.0 05.06.2023 New copyright # V 3.1.1 10.08.2024 Read from stdin if filename was not specified # V 3.2.0 12.08.2024 New minor version: New option --import # # Find executable bash library and source it lib=`which lib.bash 2>/dev/null | sed 's|^no 'lib.bash' in .*||'` if [ "$lib" != "" ]; then source "$lib" else progdir=`dirname "$0"` if [ -r "${progdir}/lib.bash" ]; then source "${progdir}/lib.bash" else echo "Unexpected error: Unable to locate bash library 'lib.bash'." exit 1 fi fi # Preset filename="" color="" len=0 longestline=0 # Check parameters: Loop until all parameters are used up while [ $# -gt 0 ]; do pname=${1} case "$pname" in -v | --version) shift showversion=true ;; -h | --help) shift showhelp=true ;; -i | --import) shift if [ "$1" != "" ]; then if [ "$filename" = "" ]; then filename="$1" if [ ! -r "$filename" ]; then errstr="Can't read from filename '$filename'." fi else errstr="Option '$pname' used more then once." fi shift else errstr="Please specify a filename after parameter '$pname'." fi ;; -c | --color) shift if [ "$1" != "" ]; then if [ "$color" = "" ]; then color=${1} else errstr="Option '$pname' used more then once." fi shift else errstr="Please specify a color code after parameter '$pname'." fi ;; *) shift paramck=`echo "$pname" | grep '^-'` # Parameters don't begin with '-' if [ "$paramck" != "" ]; then errstr="Unknown option '$pname'." else errstr="Unknown additional parameter: '$pname'." fi esac done # Display help or error message DisplayHelp # Check if filename is empty and file exists if [ "$filename" = "" ]; then if [ ! -t 0 ]; then # We have a stream cat /dev/stdin | tr '\t' ' ' > $scratchfile 2>&1 else exitcode=2 errormsg $exitcode "($progstr) Please specify a file with content to display." exit $exitcode fi else if [ ! -r "$filename" ]; then exitcode=2 errormsg $exitcode "($progstr) File '$filename' does not exists or can't be read." exit $exitcode else # Create a workfile and convert tabs to blanks (otherwise formatting won't work correct) cat "$filename" | tr '\t' ' ' > $scratchfile fi fi function PrintMarkerString() { local len=${1} local stype=`echo "$2" | tolower` local form="" local head="" local tail="" form=`printf "%-${len}s" | tr ' ' '-'` # Right pad string with blanks and replace blanks with dash if [ "$stype" = "begin" ]; then if [ "$color" != "" ]; then head="${color}/-" tail="\\" else head="/-" tail="\\" fi else if [ "$color" != "" ]; then head="\\-" tail="/${normal}" else head="\\-" tail="/" fi fi # Print new string printf "${head}${form}${tail}" printf "\n" } ### Main while read line; do len=`echo "$line" | wc -m` # let len-- if [ $len -gt $longestline ]; then longestline=$len fi done < $scratchfile # Create begin string PrintMarkerString "$longestline" "begin" while read line; do printf " %s\n" "$line" done < $scratchfile # Create begin string PrintMarkerString "$longestline" "end" # Cleanup and exit Cleanup exit $exitcode