Merry Xmas (special present inside)

A Merry Xmas to all of you.

And, as a special present to vbe (he knows why) a little exercise:

#! /bin/ksh

pPrintSnow ()
{
typeset -i iLen=$1
while (( iLen )) ; do
     if ! (( RANDOM % 31 )) ; then
          printf "%1s" "."
     else
          printf "%1s" " "
     fi
     (( iLen -= 1 ))
done
return 0
}



pPrintTreeLine ()
{
typeset -i iWidth=$1
typeset -i iTermWidth=$COLUMNS
typeset -i iOffset=$(( (iTermWidth - iWidth)/2 ))

pPrintSnow $iOffset
while [ $iWidth -gt 0 ] ; do
     if !   (( RANDOM % 11 )) ; then
          printf "%s" "$(tput sgr0;tput bold)i$(tput sgr0;tput dim)"
     elif ! (( RANDOM %  7 )) ; then
          printf "%s" "O"
     elif ! (( RANDOM % 13 )) ; then
          printf "%s" "O"
     else
          printf "%s" "*"
     fi
     (( iWidth -= 1 ))
done
pPrintSnow $iOffset
printf "\n"

return 0
}



pPrintTrunk ()
{
typeset -i iTrunkWidth=$(( $1 - 2 ))
typeset -i iTrunkHeight=$2
typeset -i iTermWidth=$COLUMNS
typeset -i iOffset=$(( (iTermWidth - iTrunkWidth)/2 ))
typeset -i iCnt=0

if [ $iTrunkWidth -lt 1 ] ; then
     (( iTrunkWidth = 1 ))
     (( iOffset = (iTermWidth - iTrunkWidth ) / 2 ))
fi

while (( iTrunkHeight )) ; do
     if (( iTrunkHeight == 1 )) ; then
          printf "%${iOffset}s/" " "
     else
          printf "%${iOffset}s " " "
     fi
     (( iCnt = iTrunkWidth ))
     while (( iCnt )) ; do
          printf "%s" "|"
          (( iCnt -= 1 ))
     done
     if (( iTrunkHeight == 1 )) ; then
          printf '\\ \n'
     else
          printf '\n'
     fi
     (( iTrunkHeight -= 1 ))
done
return 0
}



# main ()
typeset -i iHeight=$1
typeset -i iCnt=0

eval $(resize)
tput dim
(( iCnt = 1 ))
while [ $iCnt -le $(( iHeight *2 )) ] ; do
     pPrintTreeLine $iCnt
     (( iCnt += 2 ))
done
pPrintTrunk $(( iHeight / 2 )) $(( iHeight / 5 ))
tput sgr0

exit 0

bakunin

1 Like