Blame Scripts/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
c62219
    local MESSAGE="$1"
4c79b5
    local FORMAT="$2"
bc9c90
c62219
    # Reduce paths inside output messages. The main purpose for
c62219
    # this is to free horizontal space in output messages.
c62219
    MESSAGE=$(echo "$MESSAGE" \
c62219
        | sed -r "s!${HOME}/artwork/(trunk|branches|tags)/!\1/!g")
c62219
c62219
    # Remove blank spaces from lines' begining.
c62219
    MESSAGE=$(echo "$MESSAGE" | sed -r 's!^[[:space:]]+!!')
c62219
c62219
    # Define message formats.
c62219
    case $FORMAT in
c62219
c62219
    'AsUpdatingLine' )
c62219
        cli_printMessage "`gettext "Updating"`: $MESSAGE"
c62219
        ;;
c62219
903248
    'AsTunningLine' )
903248
        cli_printMessage "`gettext "Tunning"`: $MESSAGE"
903248
        ;;
903248
c62219
    'AsDeletingLine' )
c62219
        cli_printMessage "`gettext "Deleting"`: $MESSAGE"
c62219
        ;;
c62219
c62219
    'AsCheckingLine' )
c62219
        cli_printMessage "`gettext "Checking"`: $MESSAGE"
c62219
        ;;
c62219
c62219
    'AsCreatingLine' )
c62219
        cli_printMessage "`gettext "Creating"`: $MESSAGE"
c62219
        ;;
c62219
c62219
    'AsReadingLine' )
c62219
        cli_printMessage "`gettext "Reading"`: $MESSAGE"
c62219
        ;;
c62219
c62219
    'AsSavedAsLine' )
c62219
        cli_printMessage "`gettext "Saved as"`: $MESSAGE"
c62219
        ;;
c62219
            
c62219
    'AsLinkToLine' )
c62219
        cli_printMessage "`gettext "Linked to"`: $MESSAGE"
c62219
        ;;
c62219
        
c62219
    'AsMovedToLine' )
c62219
        cli_printMessage "`gettext "Moved to"`: $MESSAGE"
c62219
        ;;
c62219
c62219
    'AsTranslationLine' )
c62219
        cli_printMessage "`gettext "Translation"`: $MESSAGE"
c62219
        ;;
c62219
c62219
    'AsDesignLine' )
c62219
        cli_printMessage "`gettext "Design"`: $MESSAGE"
c62219
        ;;
c62219
c62219
    'AsConfigurationLine' )
c62219
        cli_printMessage "`gettext "Configuration"`: $MESSAGE"
c62219
        ;;
c62219
c62219
    'AsPaletteLine' )
c62219
        cli_printMessage "`gettext "Palette"`: $MESSAGE"
c62219
        ;;
c62219
c62219
    'AsResponseLine' )
c62219
        cli_printMessage "--> $MESSAGE"
c62219
        ;;
c62219
c62219
    'AsRequestLine' )
c62219
        cli_printMessage "${MESSAGE}: " 'AsNoTrailingNewLine'
c62219
        ;;
c62219
c62219
    'AsErrorLine' )
c62219
        # This option is used to print error messsages.
c62219
        echo "${CLI_PROGRAM}: ${MESSAGE}" > /dev/stderr
c62219
        ;;
c62219
c62219
    'AsToKnowMoreLine' )
c62219
        # This option receives the output of bash's caller built-in as
c62219
        # message value and produces the documentation entry from it.
9de177
        MESSAGE="trunk/Scripts/Functions/$MESSAGE"
c62219
        cli_printMessage '-' 'AsSeparatorLine'
c62219
        cli_printMessage "`gettext "To know more, run the following command"`:"
8d0fb0
        cli_printMessage "centos-art help $MESSAGE --read"
c62219
        cli_printMessage '-' 'AsSeparatorLine'
c62219
        exit # <-- ATTENTION: Do not remove this line. We use this
c62219
             #                option as convenction to end script
c62219
             #                execution.
c62219
        ;;
c62219
    
c62219
    'AsYesOrNoRequestLine' )
bc9c90
c62219
        # Define positive answer.
c62219
        local Y="`gettext "yes"`"
bc9c90
c62219
        # Define negative answer.
c62219
        local N="`gettext "no"`"
bc9c90
c62219
        # Define default answer.
c62219
        local ANSWER=${FLAG_ANSWER}
bc9c90
c62219
        if [[ $ANSWER == 'false' ]];then
bc9c90
c62219
            # Print the question.
c62219
            cli_printMessage "$MESSAGE [${Y}/${N}]: " 'AsNoTrailingNewLine'
bc9c90
c62219
            # Redefine default answer based on user's input.
c62219
            read ANSWER
bc9c90
c62219
        fi
bc9c90
c62219
        # Verify user's answer. Only positive answer let the script
c62219
        # flow to continue. Otherwise, if something different from
c62219
        # possitive answer is passed, the script terminates its
c62219
        # execution immediatly.
c62219
        if [[ ! ${ANSWER} =~ "^${Y}" ]];then
c62219
            exit
c62219
        fi
c62219
        ;;
bc9c90
c62219
    'AsSeparatorLine' )
bc9c90
c62219
        # Define separator width.
c62219
        local MAX=70
bc9c90
c62219
        # Draw separator.
c62219
        until [[ $MAX -eq 0 ]];do
c62219
            printf "${MESSAGE}" > /dev/stderr
c62219
            MAX=$(($MAX - 1))
c62219
        done
bc9c90
c62219
        # Output newline to end separator.
c62219
        echo "" > /dev/stderr
c62219
        ;;
bc9c90
c62219
    'AsNoTrailingNewLine' )
c62219
        printf "$MESSAGE" > /dev/stderr
c62219
        ;;
bc9c90
c62219
    'AsRegularLine' | * )
c62219
        echo "$MESSAGE" \
36c149
            | awk 'BEGIN { FS=": " }
36c149
                    { 
36c149
                    if ( $0 ~ /^-+$/ ) 
36c149
                        print $0
36c149
                    else
36c149
                        printf "%-15s\t%s\n", $1, $2 
36c149
                    }
36c149
                    END {}' > /dev/stderr
c62219
        ;;
bc9c90
c62219
    esac
4c79b5
4c79b5
}