#!/bin/bash

###############################################################################
#                                                                             #
#   F01 common.lib : A library for common functions.                          #
#   _common4bash.lib : Is an extension for Bash function that cause problem   #
#                      under KSH.                                             #
#                                                                             #
#   +---------------------------------------------------------------------+   #
#   | Copyright (c) 2010-2010 Flyounet                                    |   #
#   +---------------------------------------------------------------------+   #
#   | This program is free software: you can redistribute it and/or       |   #
#   | modify it under the terms of the GNU General Public License as      |   #
#   | published by the Free Software Foundation, either version 3 of the  |   #
#   | License, or (at your option) any later version.                     |   #
#   |                                                                     |   #
#   | This program is distributed in the hope that it will be useful,     |   #
#   | but WITHOUT ANY WARRANTY; without even the implied warranty of      |   #
#   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       |   #
#   | GNU General Public License for more details.                        |   #
#   |                                                                     |   #
#   | You should have received a copy of the GNU General Public License   |   #
#   | along with this program.                                            |   #
#   | If not, see <http://www.gnu.org/licenses/>.                         |   #
#   +---------------------------------------------------------------------+   #
#   | Cette œuvre est distribuée SANS AUCUNE GARANTIE hormis celle d'être |   #
#   | distribuée sous les termes de la License Demerdez-vous ("Demerden   |   #                                  
#   | Sie Sich License") plusommunément appelée DSSL telle que publiée    |   #                                  
#   | par Flyounet : soit la version 1 de cette licence, soit (à votre    |   #                                  
#   | gré) toute version ultérieure.                                      |   #                                  
#   |                                                                     |   #                                  
#   | Vous devriez avoir reçu une copie de la Licence Démerdez-vous avec  |   #                                  
#   | cette œuvre ; si ce n’est pas le cas, consultez :                   |   #                                  
#   | <http://dssl.flyounet.net/licenses/>.                               |   #
#   +---------------------------------------------------------------------+   #
#   | Author: Flyounet  < dev @@ flyounet . net >                         |   #
#   +---------------------------------------------------------------------+   #
#                                                                             #
###############################################################################
#                                                                             #
# v0.01 [14/04/2010] Flyounet  [@Home] :                                      #
#       > Initiale Release (from common.lib)                                  #
#       > Version is linked to the common.lib                                 #
#                                                                             #
#                                                                             #
###############################################################################
#                                                                             #
# Todo :                                                                      #
#                                                                             #
#                                                                             #
###############################################################################
#                                                                             #
# Legende :                                                                   #
#  + --> Indique une nouveaute, un ajout de fonctionnalite.                   #
#  * --> Indique une correction de bogue.                                     #
#  - --> Indique la suppression d'une fonctionnalite/variable.                #
#  > --> Indique une information n'ayant pas forcement de rapport avec le code#
#  < --> Indique une amelioration a apporter au code.                         #
# <- --> Indique une amelioration en cours de developpement/realisation.      #
# OK --> Indique qu'une amelioration a ete effectuee.                         #
#                                                                             #
###############################################################################


# ############################################################################ #
# Variables Initialisation
# ############################################################################ #

# Indicate that the file is loaded
export __BASH_EXTENSION='loaded'

# ############################################################################ #
# Functions
# ############################################################################ #

#F01.23-1.02
#Function : _textBox4Bash, Create a box around the text
#Syntaxe  : _textBox4Bash <string> <pattern> <(left|center|right)>
#Return   : 1=OK
#Notes    : export the text variable in __textBox
#         : All catacters \t, \n, \r and space are removed.
_textBox4Bash() {
	myDebug 5 "_textBox4Bash(${@})"
	_max=0
	if [ ${#} -eq 3 ]; then _align="${3}"; else _align="left"; fi
	eval $(
		p2s "${1}" | while read  _string
		do
			if [ ${#_string} -gt ${_max} ]; then export _max=${#_string}; fi
			p2s "_max=${_max};"
		done
	)
	let _maxRow=${_max}+4
	_padding="${2}"
	__textBox=''
	while read _string
	do
		_subString=""
		case "${_align,,}" in
			center)
				let "_padLeft=(${_max}-${#_string})/2"
				let _padRight=${_max}-${#_string}-${_padLeft}
				textFill ' ' ${_padLeft}
				_subString="${_padding} ${__textFill}${_string}"
				textFill ' ' ${_padRight}
				_subString="${_subString}${__textFill} ${_padding}"
				;;
			left|right|*)
				let _pad=${_max}-${#_string}
				textFill ' ' ${_pad}
				if [ "${_align,,}" = "right" ]
				then
					_subString="${_padding} ${__textFill}${_string} ${_padding}"
				else
					_subString="${_padding} ${_string}${__textFill} ${_padding}"
				fi
				;;
		esac
		__textBox="${__textBox}${_subString}\n"
	done < <( p2s "${1}" )
	textFill "${2}" ${_maxRow}
	__textBox="${__textFill}\n${__textBox}${__textFill}"
	export __textBox
	return 1
}

# +---------------------------------------------------------------------------+ #
# | Last subversion informations : 
# | $Revision: 20 $ : $Date: 2010-04-12 17:43:50 +0200 (lun. 12 avril 2010) $ 
# | $Author: flyounet $
# +---------------------------------------------------------------------------+ #
