Blame Scripts/Bash/Cli/Functions/cli_printMessage.sh

4c79b5
#!/bin/bash
4c79b5
#
4c79b5
# cli_printMessage.sh -- This function outputs information in
db0971
# predifined formats to standard error. This function is the standard
4c79b5
# way to output information inside centos-art.sh script.
4c79b5
#
843bac
# Copyright (C) 2009-2011 Alain Reguera Delgado
4c79b5
# 
7cd8e9
# This program is free software; you can redistribute it and/or
7cd8e9
# modify it under the terms of the GNU General Public License as
7cd8e9
# published by the Free Software Foundation; either version 2 of the
7cd8e9
# License, or (at your option) any later version.
4c79b5
# 
4c79b5
# This program is distributed in the hope that it will be useful, but
4c79b5
# WITHOUT ANY WARRANTY; without even the implied warranty of
4c79b5
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c79b5
# General Public License for more details.
4c79b5
#
4c79b5
# You should have received a copy of the GNU General Public License
4c79b5
# along with this program; if not, write to the Free Software
4c79b5
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
4c79b5
# USA.
4c79b5
# 
4c79b5
# ----------------------------------------------------------------------
418249
# $Id$
4c79b5
# ----------------------------------------------------------------------
4c79b5
4c79b5
function cli_printMessage {
4c79b5
49237e
    # Verify `--quiet' option.
49237e
    if [[ "$FLAG_QUIET" == 'true' ]];then
49237e
        return
49237e
    fi
49237e
bc9c90
    local MESSAGES="$1"
4c79b5
    local FORMAT="$2"
bc9c90
    local MESSAGE=''
bc9c90
bc9c90
    # Let processing more than one message.  There is no need to
bc9c90
    # create loops outside this function to process more than one
bc9c90
    # message. Instead, pass the list of messages to this function.
bc9c90
    # This may save some memory when the `--quiet' option is used.
bc9c90
    for MESSAGE in "$MESSAGES";do
bc9c90
bc9c90
        # Reduce paths inside output messages. The main purpose for
bc9c90
        # this is to free horizontal space in output messages.
bc9c90
        MESSAGE=$(echo "$MESSAGE" \
bc9c90
            | sed -r "s!${HOME}/artwork/(trunk|branches|tags)/!\1/!g")
bc9c90
bc9c90
        # Remove blank spaces from lines' begining.
bc9c90
        MESSAGE=$(echo "$MESSAGE" | sed -r 's!^[[:space:]]+!!')
bc9c90
bc9c90
        # Define message formats.
bc9c90
        case $FORMAT in
bc9c90
bc9c90
            'AsUpdatingLine' )
bc9c90
                cli_printMessage "`gettext "Updating"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsDeletingLine' )
bc9c90
                cli_printMessage "`gettext "Deleting"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsCheckingLine' )
bc9c90
                cli_printMessage "`gettext "Checking"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsCreatingLine' )
bc9c90
                cli_printMessage "`gettext "Creating"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsReadingLine' )
bc9c90
                cli_printMessage "`gettext "Reading"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsSavedAsLine' )
bc9c90
                cli_printMessage "`gettext "Saved as"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsLinkToLine' )
bc9c90
                cli_printMessage "`gettext "Linked to"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsMovedToLine' )
bc9c90
                cli_printMessage "`gettext "Moved to"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsTranslationLine' )
bc9c90
                cli_printMessage "`gettext "Translation"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsDesignLine' )
bc9c90
                cli_printMessage "`gettext "Design"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsConfigurationLine' )
bc9c90
                cli_printMessage "`gettext "Configuration"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsPaletteLine' )
bc9c90
                cli_printMessage "`gettext "Palette"`: $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsResponseLine' )
bc9c90
                cli_printMessage "--> $MESSAGE"
bc9c90
                ;;
bc9c90
bc9c90
            'AsRequestLine' )
bc9c90
                cli_printMessage "${MESSAGE}: " 'AsNoTrailingNewLine'
bc9c90
                ;;
bc9c90
bc9c90
            'AsErrorLine' )
bc9c90
                # This option is used to print error messsages.
bc9c90
                echo "${CLI_PROGRAM}: ${MESSAGE}" > /dev/stderr
bc9c90
                ;;
bc9c90
bc9c90
            'AsToKnowMoreLine' )
bc9c90
                # This option receives the output of bash's caller
bc9c90
                # built-in as message value and produces the documentation
bc9c90
                # entry from it.
bc9c90
                MESSAGE=$(dirname "$(echo $MESSAGE | cut -d ' ' -f2-)")
bc9c90
                cli_printMessage '-' 'AsSeparatorLine'
bc9c90
                cli_printMessage "`gettext "To know more, run the following command"`:"
bc9c90
                cli_printMessage "centos-art manual --read='$MESSAGE'"
bc9c90
                cli_printMessage '-' 'AsSeparatorLine'
bc9c90
                exit # <-- ATTENTION: Do not remove this line. We use this
bc9c90
                     #                option as convenction to end script
bc9c90
                     #                execution.
bc9c90
                ;;
bc9c90
    
bc9c90
            'AsYesOrNoRequestLine' )
bc9c90
bc9c90
                # Define positive answer.
bc9c90
                local Y="`gettext "yes"`"
bc9c90
bc9c90
                # Define negative answer.
bc9c90
                local N="`gettext "no"`"
bc9c90
bc9c90
                # Define default answer.
bc9c90
                local ANSWER=${FLAG_ANSWER}
bc9c90
bc9c90
                if [[ $ANSWER == 'false' ]];then
bc9c90
bc9c90
                    # Print the question.
bc9c90
                    cli_printMessage "$MESSAGE [${Y}/${N}]: " 'AsNoTrailingNewLine'
bc9c90
bc9c90
                    # Redefine default answer based on user's input.
bc9c90
                    read ANSWER
bc9c90
bc9c90
                fi
bc9c90
bc9c90
                # Verify user's answer. Only positive answer let the
bc9c90
                # script flow to continue. Otherwise, if something
bc9c90
                # different from possitive answer is passed, the
bc9c90
                # script terminates its execution immediatly.
bc9c90
                if [[ ! ${ANSWER} =~ "^${Y}" ]];then
bc9c90
                    exit
bc9c90
                fi
bc9c90
                ;;
bc9c90
bc9c90
            'AsSeparatorLine' )
bc9c90
bc9c90
                # Define separator width.
bc9c90
                local MAX=70
bc9c90
bc9c90
                # Draw separator.
bc9c90
                until [[ $MAX -eq 0 ]];do
bc9c90
                    printf "${MESSAGE}" > /dev/stderr
bc9c90
                    MAX=$(($MAX - 1))
bc9c90
                done
bc9c90
bc9c90
                # Output newline to end separator.
bc9c90
                echo "" > /dev/stderr
bc9c90
                ;;
bc9c90
bc9c90
            'AsNoTrailingNewLine' )
bc9c90
                printf "$MESSAGE" > /dev/stderr
bc9c90
                ;;
bc9c90
bc9c90
            'AsRegularLine' | * )
bc9c90
                echo "$MESSAGE" \
bc9c90
                    | awk -f ${CLI_BASEDIR}/Styles/output_forTwoColumns.awk \
bc9c90
                    > /dev/stderr
bc9c90
                ;;
bc9c90
bc9c90
        esac
bc9c90
bc9c90
    done
4c79b5
4c79b5
}