| 1 | #!/bin/bash |
| 2 | # Draw-box.sh: Drawing a box using ASCII characters. |
| 3 | |
| 4 | # Script by Stefano Palmeri, with minor editing by document author. |
| 5 | # Minor edits suggested by Jim Angstadt. |
| 6 | # Used in the ABS Guide with permission. |
| 7 | |
| 8 | |
| 9 | ###################################################################### |
| 10 | ### draw_box function doc ### |
| 11 | |
| 12 | # The "draw_box" function lets the user |
| 13 | #+ draw a box in a terminal. |
| 14 | # |
| 15 | # Usage: draw_box ROW COLUMN HEIGHT WIDTH [COLOR] |
| 16 | # ROW and COLUMN represent the position |
| 17 | #+ of the upper left angle of the box you're going to draw. |
| 18 | # ROW and COLUMN must be greater than 0 |
| 19 | #+ and less than current terminal dimension. |
| 20 | # HEIGHT is the number of rows of the box, and must be > 0. |
| 21 | # HEIGHT + ROW must be <= than current terminal height. |
| 22 | # WIDTH is the number of columns of the box and must be > 0. |
| 23 | # WIDTH + COLUMN must be <= than current terminal width. |
| 24 | # |
| 25 | # E.g.: If your terminal dimension is 20x80, |
| 26 | # draw_box 2 3 10 45 is good |
| 27 | # draw_box 2 3 19 45 has bad HEIGHT value (19+2 > 20) |
| 28 | # draw_box 2 3 18 78 has bad WIDTH value (78+3 > 80) |
| 29 | # |
| 30 | # COLOR is the color of the box frame. |
| 31 | # This is the 5th argument and is optional. |
| 32 | # 0=black 1=red 2=green 3=tan 4=blue 5=purple 6=cyan 7=white. |
| 33 | # If you pass the function bad arguments, |
| 34 | #+ it will just exit with code 65, |
| 35 | #+ and no messages will be printed on stderr. |
| 36 | # |
| 37 | # Clear the terminal before you start to draw a box. |
| 38 | # The clear command is not contained within the function. |
| 39 | # This allows the user to draw multiple boxes, even overlapping ones. |
| 40 | |
| 41 | ### end of draw_box function doc ### |
| 42 | ###################################################################### |
| 43 | |
| 44 | draw_box(){ |
| 45 | |
| 46 | #=============# |
| 47 | HORZ="-" |
| 48 | VERT="|" |
| 49 | CORNER_CHAR="+" |
| 50 | |
| 51 | MINARGS=4 |
| 52 | E_BADARGS=65 |
| 53 | #=============# |
| 54 | |
| 55 | |
| 56 | if [ $# -lt "$MINARGS" ]; then # If args are less than 4, exit. |
| 57 | exit $E_BADARGS |
| 58 | fi |
| 59 | |
| 60 | # Looking for non digit chars in arguments. |
| 61 | # Probably it could be done better (exercise for the reader?). |
| 62 | if echo $@ | tr -d [:blank:] | tr -d [:digit:] | grep . &> /dev/null; then |
| 63 | exit $E_BADARGS |
| 64 | fi |
| 65 | |
| 66 | BOX_HEIGHT=`expr $3 - 1` # -1 correction needed because angle char "+" |
| 67 | BOX_WIDTH=`expr $4 - 1` #+ is a part of both box height and width. |
| 68 | T_ROWS=`tput lines` # Define current terminal dimension |
| 69 | T_COLS=`tput cols` #+ in rows and columns. |
| 70 | |
| 71 | if [ $1 -lt 1 ] || [ $1 -gt $T_ROWS ]; then # Start checking if arguments |
| 72 | exit $E_BADARGS #+ are correct. |
| 73 | fi |
| 74 | if [ $2 -lt 1 ] || [ $2 -gt $T_COLS ]; then |
| 75 | exit $E_BADARGS |
| 76 | fi |
| 77 | if [ `expr $1 + $BOX_HEIGHT + 1` -gt $T_ROWS ]; then |
| 78 | exit $E_BADARGS |
| 79 | fi |
| 80 | if [ `expr $2 + $BOX_WIDTH + 1` -gt $T_COLS ]; then |
| 81 | exit $E_BADARGS |
| 82 | fi |
| 83 | if [ $3 -lt 1 ] || [ $4 -lt 1 ]; then |
| 84 | exit $E_BADARGS |
| 85 | fi # End checking arguments. |
| 86 | |
| 87 | plot_char(){ # Function within a function. |
| 88 | echo -e "\E[${1};${2}H"$3 |
| 89 | } |
| 90 | |
| 91 | echo -ne "\E[3${5}m" # Set box frame color, if defined. |
| 92 | |
| 93 | # start drawing the box |
| 94 | |
| 95 | count=1 # Draw vertical lines using |
| 96 | for (( r=$1; count<=$BOX_HEIGHT; r++)); do #+ plot_char function. |
| 97 | plot_char $r $2 $VERT |
| 98 | let count=count+1 |
| 99 | done |
| 100 | |
| 101 | count=1 |
| 102 | c=`expr $2 + $BOX_WIDTH` |
| 103 | for (( r=$1; count<=$BOX_HEIGHT; r++)); do |
| 104 | plot_char $r $c $VERT |
| 105 | let count=count+1 |
| 106 | done |
| 107 | |
| 108 | count=1 # Draw horizontal lines using |
| 109 | for (( c=$2; count<=$BOX_WIDTH; c++)); do #+ plot_char function. |
| 110 | plot_char $1 $c $HORZ |
| 111 | let count=count+1 |
| 112 | done |
| 113 | |
| 114 | count=1 |
| 115 | r=`expr $1 + $BOX_HEIGHT` |
| 116 | for (( c=$2; count<=$BOX_WIDTH; c++)); do |
| 117 | plot_char $r $c $HORZ |
| 118 | let count=count+1 |
| 119 | done |
| 120 | |
| 121 | plot_char $1 $2 $CORNER_CHAR # Draw box angles. |
| 122 | plot_char $1 `expr $2 + $BOX_WIDTH` $CORNER_CHAR |
| 123 | plot_char `expr $1 + $BOX_HEIGHT` $2 $CORNER_CHAR |
| 124 | plot_char `expr $1 + $BOX_HEIGHT` `expr $2 + $BOX_WIDTH` $CORNER_CHAR |
| 125 | |
| 126 | echo -ne "\E[0m" # Restore old colors. |
| 127 | |
| 128 | P_ROWS=`expr $T_ROWS - 1` # Put the prompt at bottom of the terminal. |
| 129 | |
| 130 | echo -e "\E[${P_ROWS};1H" |
| 131 | } |
| 132 | |
| 133 | |
| 134 | # Now, let's try drawing a box. |
| 135 | clear # Clear the terminal. |
| 136 | R=2 # Row |
| 137 | C=3 # Column |
| 138 | H=10 # Height |
| 139 | W=45 # Width |
| 140 | col=1 # Color (red) |
| 141 | draw_box $R $C $H $W $col # Draw the box. |
| 142 | |
| 143 | exit 0 |
| 144 | |
| 145 | # Exercise: |
| 146 | # -------- |
| 147 | # Add the option of printing text within the drawn box. |