Blame Scripts/Bash/centos-art/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
4c79b5
    local MESSAGE="$1"
4c79b5
    local FORMAT="$2"
843bac
4c79b5
    # Reduce paths inside output messages. The main purpose for this
4c79b5
    # is to free horizontal space in output messages.
841480
    MESSAGE=$(echo "$MESSAGE" \
7b46a4
        | sed -r "s!^${HOME}/artwork/(trunk|branches|tags)/!\1/!g")
4c79b5
4c79b5
    # Remove blank spaces from lines' begining.
4c79b5
    MESSAGE=$(echo "$MESSAGE" | sed -r 's!^[[:space:]]+!!')
4c79b5
4c79b5
    # Define message formats.
4c79b5
    case $FORMAT in
4c79b5
4c79b5
        'AsUpdatingLine' )
db0971
            cli_printMessage "`gettext "Updating"`: $MESSAGE"
4c79b5
            ;;
4c79b5
9a2a67
        'AsDeletingLine' )
db0971
            cli_printMessage "`gettext "Deleting"`: $MESSAGE"
4c79b5
            ;;
4c79b5
4c79b5
        'AsCheckingLine' )
db0971
            cli_printMessage "`gettext "Checking"`: $MESSAGE"
4c79b5
            ;;
4c79b5
4c79b5
        'AsCreatingLine' )
db0971
            cli_printMessage "`gettext "Creating"`: $MESSAGE"
4c79b5
            ;;
4c79b5
4c79b5
        'AsSavedAsLine' )
db0971
            cli_printMessage "`gettext "Saved as"`: $MESSAGE"
4c79b5
            ;;
4c79b5
4c79b5
        'AsLinkToLine' )
db0971
            cli_printMessage "`gettext "Linked to"`: $MESSAGE"
6253fc
            ;;
6253fc
6253fc
        'AsMovedToLine' )
db0971
            cli_printMessage "`gettext "Moved to"`: $MESSAGE"
4c79b5
            ;;
4c79b5
4c79b5
        'AsTranslationLine' )
db0971
            cli_printMessage "`gettext "Translation"`: $MESSAGE"
e92b23
            ;;
e92b23
0c0142
        'AsDesignLine' )
db0971
            cli_printMessage "`gettext "Design"`: $MESSAGE"
0c0142
            ;;
0c0142
e92b23
        'AsConfigurationLine' )
db0971
            cli_printMessage "`gettext "Configuration"`: $MESSAGE"
db0971
            cli_printMessage '-' 'AsSeparatorLine'
4c79b5
            ;;
4c79b5
70c11c
        'AsPaletteLine' )
db0971
            cli_printMessage "`gettext "Palette"`: $MESSAGE"
70c11c
            ;;
70c11c
4c79b5
        'AsResponseLine' )
db0971
            cli_printMessage "--> $MESSAGE"
4c79b5
            ;;
4c79b5
4c79b5
        'AsRequestLine' )
db0971
            cli_printMessage "${MESSAGE}: " 'AsNoTrailingNewLine'
4c79b5
            ;;
4c79b5
db0971
        'AsErrorLine' )
db0971
            # This option is used to print error messsages.
7b46a4
            echo "${CLI_PROGRAM}: ${MESSAGE}" > /dev/stderr
db0971
            ;;
6e2747
db0971
        'AsToKnowMoreLine' )
db0971
            # This option receives the output of bash's caller
db0971
            # built-in as message value and produces the documentation
db0971
            # entry from it.
db0971
            MESSAGE=$(cli_getRepoName "$(echo $MESSAGE | cut -d ' ' -f2-)" 'd')
db0971
            cli_printMessage '-' 'AsSeparatorLine'
db0971
            cli_printMessage "`gettext "To know more, run the following command"`:"
db0971
            cli_printMessage "centos-art manual --read='$MESSAGE'"
db0971
            cli_printMessage '-' 'AsSeparatorLine'
db0971
            exit # <-- ATTENTION: Do not remove this line. We use this
db0971
                 #                option as convenction to end script
db0971
                 #                execution.
6e2747
            ;;
6e2747
4c79b5
        'AsYesOrNoRequestLine' )
db0971
cdebd1
            # Define positive answer.
db0971
            local Y="`gettext "yes"`"
cdebd1
cdebd1
            # Define negative answer.
db0971
            local N="`gettext "no"`"
cdebd1
09cf0d
            # Define default answer.
09cf0d
            local ANSWER=${FLAG_ANSWER}
09cf0d
09cf0d
            if [[ $ANSWER == 'false' ]];then
843bac
db0971
                # Print the question.
db0971
                cli_printMessage "$MESSAGE [${Y}/${N}]: " 'AsNoTrailingNewLine'
db0971
09cf0d
                # Redefine default answer based on user's input.
09cf0d
                read ANSWER
db0971
070e44
            fi
843bac
070e44
            # Verify user's answer. Only positive answer let the
070e44
            # script flow to continue. Otherwise, if something
070e44
            # different from possitive answer is passed, the
070e44
            # script terminates its execution immediatly.
09cf0d
            if [[ ! ${ANSWER} =~ "^${Y}" ]];then
070e44
                exit
4c79b5
            fi
4c79b5
            ;;
a44f77
db0971
        'AsSeparatorLine' )
db0971
db0971
            if [[ "$FLAG_QUIET" == 'false' ]];then
db0971
db0971
                # Define separator width.
db0971
                local MAX=70
db0971
db0971
                # Draw separator.
db0971
                until [[ $MAX -eq 0 ]];do
2b5261
                    printf "${MESSAGE}" > /dev/stderr
db0971
                    MAX=$(($MAX - 1))
db0971
                done
db0971
db0971
                # Output newline to end separator.
2b5261
                echo "" > /dev/stderr
db0971
db0971
            fi
6253fc
            ;;
6253fc
db0971
        'AsNoTrailingNewLine' )
db0971
            if [[ "$FLAG_QUIET" == 'false' ]];then
db0971
                printf "$MESSAGE" > /dev/stderr
843bac
            fi
4c79b5
            ;;
4c79b5
db0971
        'AsRegularLine' | * )
db0971
            if [[ "$FLAG_QUIET" == 'false' ]];then
db0971
                echo "$MESSAGE" \
7b46a4
                    | awk -f ${CLI_BASEDIR}/Styles/output_forTwoColumns.awk \
db0971
                    > /dev/stderr
843bac
            fi
4c79b5
            ;;
4c79b5
4c79b5
    esac
4c79b5
4c79b5
}