#!/usr/bin/env bash # # Author: Georg Voell - georg.voell@standby.cloud # Version: @(#)print-header 3.3.1 16.03.2026 (c)2026 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. # # 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 # V 3.3.0 19.01.2026 Revised with support of Claude Code # V 3.3.1 16.03.2026 Optimized: replaced backticks with $(); [ ] with [[ ]]; # echo|grep with [[ == -* ]]; echo|wc -m with ${#line} (no subshell); # UUOC removed; printf form with bash built-in string repeat # # Find executable bash library and source it lib=$(command -v lib.bash 2>/dev/null) if [[ -n "$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 function PrintMarkerString() { local len="${1}" local stype stype=$(ToLower "$2") local head="" local tail="" # Build dash string using bash built-in printf (no tr subshell) local form printf -v form '%*s' "$len" '' form="${form// /-}" if [[ "$stype" == "begin" ]]; then if [[ -n "$color" ]]; then head="${color}/-" tail="\\" else head="/-" tail="\\" fi else if [[ -n "$color" ]]; then head="\\-" tail="/${normal}" else head="\\-" tail="/" fi fi printf "%s%s%s\n" "$head" "$form" "$tail" } # 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 [[ -n "$1" ]]; then if [[ -z "$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 [[ -n "$1" ]]; then if [[ -z "$color" ]]; then color="$(printf '%b' "${1}")" # expand \033 etc. into real escape bytes else errstr="Option '$pname' used more then once." fi shift else errstr="Please specify a color code after parameter '$pname'." fi ;; *) shift if [[ "$pname" == -* ]]; 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 [[ -z "$filename" ]]; then if [[ ! -t 0 ]]; then # We have a stream - avoid UUOC: redirect stdin directly into tr 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) # Avoid UUOC: redirect file directly into tr tr '\t' ' ' < "$filename" > "$scratchfile" fi fi ### Main # Find longest line length using bash built-in ${#line} (no echo|wc subshell per line) while IFS= read -r line; do len=${#line} if (( len > longestline )); then longestline=$len fi done < "$scratchfile" # Create begin string PrintMarkerString "$longestline" "begin" while IFS= read -r line; do printf " %s\n" "$line" done < "$scratchfile" # Create end string PrintMarkerString "$longestline" "end" # Cleanup and exit Cleanup exit $exitcode