Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created this Function , which generate below output on terminal , but this function seems complex, So I posted here for any improvement or for same alternate solution. enter image description here

#!/bin/bash
function box_out() {
        input_char=$(echo "$@" | wc -c)
        line=$(for i in `seq 0 $input_char`; do printf "-"; done)
        # tput This should be the best option. what tput does is it will read the terminal info and render the correctly escaped ANSI code for you. code like \033[31m will break the readline library in some of the terminals.
        tput bold
        line="$(tput setaf 3)${line}"
        space=${line//-/ }
        echo " ${line}"
        printf '|' ; echo -n "$space" ; printf "%s\n" '|';
        printf '| ' ;tput setaf 4; echo -n "$@"; tput setaf 3 ; printf "%s\n" ' |';
        printf '|' ; echo -n "$space" ; printf "%s\n" '|';
        echo " ${line}"
        tput sgr 0
}

box_out $@
share|improve this question
up vote 9 down vote accepted

As your shebang and syntax indicates unportable bash, I prefer it this way:

function box_out()
{
  local s="$*"
  tput setaf 3
  echo " -${s//?/-}-
| ${s//?/ } |
| $(tput setaf 4)$s$(tput setaf 3) |
| ${s//?/ } |
 -${s//?/-}-"
  tput sgr 0
}

Of course, you can optimize it if you wish.

Update as requested in comment, to handle multiline text too.

function box_out()
{
  local s=("$@") b w
  for l in "${s[@]}"; do
    ((w<${#l})) && { b="$l"; w="${#l}"; }
  done
  tput setaf 3
  echo " -${b//?/-}-
| ${b//?/ } |"
  for l in "${s[@]}"; do
    printf '| %s%*s%s |\n' "$(tput setaf 4)" "-$w" "$l" "$(tput setaf 3)"
  done
  echo "| ${b//?/ } |
 -${b//?/-}-"
  tput sgr 0
}

Call it with multiple parameters, like box_out 'first line' 'more line' 'even more line'.

share|improve this answer
    
+1 I like the replace text with border. :) – peterph Mar 30 '13 at 18:20
1  
Awesome.............. You are Master .... – Rahul Patil Mar 31 '13 at 8:29
    
How would I apply this function to multiple lines, or a single line with \n type CRs? – TryTryAgain Jun 2 at 17:48
    
@manatwork that's marvelous, BUT... it's not working for expanding variables...and then if I try any quoting to resolve, it prints on one line again. Thank you so much for the quick response and brilliant solution! – TryTryAgain Jun 2 at 19:17
1  
Variable expansion should occur before the box_out function being executed. Double quotes and escapes should still work as usual: pastebin.com/ekmUKUkn By the way, you also use bash, right? – manatwork Jun 3 at 7:20

So, my solution is not quite the same as yours, but strictly speaking it prints a box around the text, and the implementation is a bit simpler so I thought I'd share.

banner() {
    msg="# $* #"
    edge=$(echo "$msg" | sed 's/./#/g')
    echo "$edge"
    echo "$msg"
    echo "$edge"
}

And here it is in action:

$ banner "hi"
######
# hi #
######
$ banner "hi there"
############
# hi there #
############

Just plain text, no fancy ansi colors or anything.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.