#!/usr/bin/bash # boxit -- a filter putting a text in a box # +-----+ ┌─────┐ # | box | and now │ box │ # +-----+ └─────┘ # puts box around input text with tab conversion as TODO # input is piped into stdin or from concantenating named files # # boxit -title titlewords ... adds a title box now # 2010-04-05 12:26:17 bokr # # 2016-11-29 06:47:32 bokr # now uses utf8 boxchars by default unless first non-help opt is -a # Copyright (C) 2019 Bengt Richter # License LGPLv3+: GNU LGPL 3 or later . # This is free software: you are free to change and redistribute it. # There is NO WARRANTY, to the extent permitted by law. case "$1" in -h*|--h*) shift; echo "\ Usage: boxit [-a | -r ] [ -c command ] [ -h* | --h*] [ -title title_string ] -c command will box the output of command with command as the title. -a will use ascii characters, otherwise utf8 box drawing chars, -r for round corners Otherwise boxit puts box around input text piped into stdin, with optional matching title box at top, like this." \ |boxit $1 -title "Usage"; exit; esac if [ "$1" == '-a' ];then asciiopt='-a' dashes='----------------------------------------' shift; hl='-'; vl='|'; ul='+'; ur='+'; ll='+'; lr='+'; tr='+'; tl='+'; td='+'; tu='+'; tx='+'; else asciiopt= dashes='────────────────────────────────────────' hl='─'; vl='│'; ul='┌'; ur='┐'; ll='└'; lr='┘'; tr='├'; tl='┤'; td='┬'; tu='┴'; tx='┼'; if [ "$1" == '-r' ];then ul='╭'; ur='╮'; ll='╰'; lr='╯'; ## uchr {9581..9582} 10 {9584..9583} 10 shift fi fi if [ "$1" == '-title' ];then shift;title="$*";fi function stripvtesc () { cat $1|sed -e 's:[\x07\r]::g' -e 's:.\x08::g' -e 's:\x1B\[[^a-zA-Z]*[a-zA-Z]\x0f*::g' -e 's:\xe2\x80\x98:":g' -e 's:\xe2\x80\x99:":g' } function stripped_size () { ss="$(echo "$1"|stripvtesc)" echo -n ${#ss} } ## -c command if [ "$1" == '-c' ];then shift; title="$@" stdboth="$($@ 2>&1)" echo "$stdboth" |boxit $asciiopt -title "$title" exit; fi oldIFS="$IFS"; IFS='' i=0; txt[0]=''; mxwidth=$(stripped_size "$title") while read -r line; do if [ "$i" == "0" -a -z "$line" ];then continue;fi # dump leading blank lines stripped_nc="$(stripped_size "$line")" #|stripvtesc)" (( ${stripped_nc} > $mxwidth ? mxwidth=${stripped_nc} : $mxwidth )) txt[$i]="$line" ((i++)) done ((i--)) # to last line rx='[ \t]+' while true;do if [ "$i" -le 0 ];then break;fi if [ -z "$(echo "${txt[$i]}"|tr -d $' \t')" ];then unset txt[$i] ((i--)) else break fi done spaces=' ' while (( ${#dashes} < $mxwidth )); do dashes="$dashes$dashes" # double spaces="$spaces$spaces" # double done mx3=$mxwidth if [ "$title" != "" ]; then nsp=$(( $mxwidth-$(stripped_size "${title}" ))) # center it halfsp=$(( $nsp / 2 )) sp1="${spaces:0:$(( $nsp / 2 ))}"; sp2="${spaces:0:$(($nsp - $halfsp))}" echo "$ul$hl${dashes:0:$mx3}$hl$ur" echo "$vl ${sp1}$title${sp2} $vl" echo "$tr$hl${dashes:0:$mx3}$hl$tl" else echo "$ul$hl${dashes:0:$mx3}$hl$ur" fi for itm in "${txt[@]}"; do sszitm=$(stripped_size "${itm}") sptomax=$(( $mxwidth-$sszitm )) sp="${spaces:0:$sptomax}" echo "$vl $itm${sp} $vl" done echo "$ll$hl${dashes:0:$mx3}$hl$lr" IFS="$oldIFS"