Blame Scripts/Bash/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" \
3b6828
        | sed -r 's!/home/centos/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.
db0971
            cli_printMessage "${CLINAME}: ${MESSAGE}"
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
db0971
            if [[ $FLAG_ANSWER == 'false' ]];then
843bac
db0971
                # Print the question.
db0971
                cli_printMessage "$MESSAGE [${Y}/${N}]: " 'AsNoTrailingNewLine'
db0971
db0971
                # Wait for user's answer to be entered.
db0971
                cli_printMessage "FLAG_ANSWER" 'AsReadLine'
db0971
db0971
                # Verify user's answer. Only positive answer let the
db0971
                # script flow to continue. Otherwise, if something
db0971
                # different from possitive answer is passed, the
db0971
                # script terminates its execution immediatly.
db0971
                if [[ ! ${FLAG_ANSWER} =~ "^${Y}" ]];then
db0971
                    exit
db0971
                fi
843bac
4c79b5
            fi
4c79b5
            ;;
a44f77
db0971
        'AsReadLine' )
db0971
            if [[ $FLAG_QUIET == 'false' ]];then
db0971
                # Use variable indirect expantion to redefine the
db0971
                # value of the variable of name passed as message.
db0971
                read ${!MESSAGE}
db0971
            fi
a44f77
            ;;
4c79b5
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
db0971
                    printf "${MESSAGE}"
db0971
                    MAX=$(($MAX - 1))
db0971
                done
db0971
db0971
                # Output newline to end separator.
db0971
                echo ""
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" \
db0971
                    | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk \
db0971
                    > /dev/stderr
843bac
            fi
4c79b5
            ;;
4c79b5
4c79b5
    esac
4c79b5
4c79b5
}