Blame Scripts/Functions/cli_printMessage.sh

4c79b5
#!/bin/bash
4c79b5
#
e4f9d3
# cli_printMessage.sh -- This function standardizes the way messages
e4f9d3
# are printed out from centos-art.sh script.
4c79b5
#
3b0984
# Copyright (C) 2009, 2010, 2011 The CentOS Artwork SIG
fa95b1
#
fa95b1
# This program is free software; you can redistribute it and/or modify
fa95b1
# it under the terms of the GNU General Public License as published by
dcd347
# the Free Software Foundation; either version 2 of the License, or (at
dcd347
# your option) any later version.
fa95b1
#
74a058
# This program is distributed in the hope that it will be useful, but
74a058
# 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
dcd347
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
7ac5a5
#
4c79b5
# ----------------------------------------------------------------------
418249
# $Id$
4c79b5
# ----------------------------------------------------------------------
4c79b5
5b396a
function cli_printMessage {
4c79b5
49237e
    # Verify `--quiet' option.
49237e
    if [[ "$FLAG_QUIET" == 'true' ]];then
49237e
        return
49237e
    fi
49237e
e4f9d3
    local MESSAGE="$1"
e4f9d3
    local FORMAT="$2"
2fa569
b2f699
    # Verify message variable, it cannot have an empty value.
e4f9d3
    if [[ $MESSAGE == '' ]];then
b2f699
        cli_printMessage "`gettext "The message cannot be empty."`" --as-error-line
b2f699
    fi
b2f699
7be3ee
    # Reverse the codification performed on characters that may affect
7be3ee
    # parsing options and non-option arguments. This codification is
7be3ee
    # realized before building the ARGUMENTS variable, at
793c4c
    # cli_parseArgumentsReDef, and we need to reverse it back here
7be3ee
    # in order to show the correct character when the message be
7be3ee
    # printed out on the screen.
7be3ee
    MESSAGE=$(echo $MESSAGE | sed -e "s/\\\0x27/'/g")
e4f9d3
7be3ee
    # Reduce paths and leading spaces from output messages. The main
7be3ee
    # purpose for this is to free horizontal space on output messages.
7be3ee
    MESSAGE=$(echo "$MESSAGE" | sed -r \
7be3ee
        -e "s!${HOME}/artwork/(trunk|branches|tags)/!\1/!g" \
7be3ee
        -e 's!^[[:space:]]+!!')
2fa569
e4f9d3
    # Print out messages based on format.
e4f9d3
    case "$FORMAT" in
e4f9d3
e4f9d3
        --as-separator-line )
e4f9d3
e4f9d3
        # Define width of separator line.
e4f9d3
        local MAX=70
e4f9d3
e4f9d3
        # Build the separator line. 
e4f9d3
        MESSAGE=$(\
e4f9d3
            until [[ $MAX -eq 0 ]];do
e4f9d3
                echo -n "$MESSAGE"
e4f9d3
                MAX=$(($MAX - 1))
e4f9d3
            done)
e4f9d3
e4f9d3
        # Draw the separator line.
e4f9d3
        echo "$MESSAGE" > /dev/stderr
e4f9d3
        ;;
e4f9d3
e4f9d3
        --as-banner-line )
e4f9d3
            cli_printMessage '-' --as-separator-line
e4f9d3
            cli_printMessage "$MESSAGE"
e4f9d3
            cli_printMessage '-' --as-separator-line
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-updating-line )
e4f9d3
            cli_printMessage "`gettext "Updating"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-cropping-line )
e4f9d3
            cli_printMessage "`gettext "Cropping from"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-tuningup-line )
e4f9d3
            cli_printMessage "`gettext "Tuning-up"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-checking-line )
e4f9d3
            cli_printMessage "`gettext "Checking"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-creating-line )
e4f9d3
            cli_printMessage "`gettext "Creating"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-deleting-line )
e4f9d3
            cli_printMessage "`gettext "Deleting"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-reading-line )
e4f9d3
            cli_printMessage "`gettext "Reading"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-savedas-line )
e4f9d3
            cli_printMessage "`gettext "Saved as"`: $MESSAGE"
e4f9d3
            ;;
c62219
            
e4f9d3
        --as-linkto-line )
e4f9d3
            cli_printMessage "`gettext "Linked to"`: $MESSAGE"
e4f9d3
            ;;
c62219
        
e4f9d3
        --as-movedto-line )
e4f9d3
            cli_printMessage "`gettext "Moved to"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-translation-line )
e4f9d3
            cli_printMessage "`gettext "Translation"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
17184e
        --as-validating-line )
17184e
            cli_printMessage "`gettext "Validating"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
17184e
e4f9d3
        --as-configuration-line )
e4f9d3
            cli_printMessage "`gettext "Configuration"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-palette-line )
e4f9d3
            cli_printMessage "`gettext "Palette"`: $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-response-line )
e4f9d3
            cli_printMessage "--> $MESSAGE"
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-request-line )
e4f9d3
            cli_printMessage "${MESSAGE}: " --as-notrailingnew-line
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-error-line )
f015d3
            # Define where the error was originated inside the
f015d3
            # centos-art.sh script. Print out the function name and
f015d3
            # line from the caller.
f015d3
            local ORIGIN="${CLI_PROGRAM} ($(caller 1 | gawk '{ print $2 " " $1 }'))"
f015d3
f015d3
            # Build the error message.
f015d3
            cli_printMessage "${ORIGIN}: ${MESSAGE}" --as-stderr-line
e4f9d3
            cli_printMessage "${FUNCDIRNAM}" --as-toknowmore-line
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-toknowmore-line )
e4f9d3
            cli_printMessage '-' --as-separator-line
e4f9d3
            cli_printMessage "`gettext "To know more, run the following command"`:"
e4f9d3
            cli_printMessage "centos-art help --read trunk/Scripts/Functions/$MESSAGE"
e4f9d3
            cli_printMessage '-' --as-separator-line
e4f9d3
            exit # <-- ATTENTION: Do not remove this line. We use this
e4f9d3
                 #                option as convenction to end script
e4f9d3
                 #                execution.
e4f9d3
            ;;
c62219
    
e4f9d3
        --as-yesornorequest-line )
e4f9d3
            # Define positive answer.
e4f9d3
            local Y="`gettext "yes"`"
e4f9d3
e4f9d3
            # Define negative answer.
e4f9d3
            local N="`gettext "no"`"
e4f9d3
e4f9d3
            # Define default answer.
e4f9d3
            local ANSWER=${N}
e4f9d3
e4f9d3
            if [[ $FLAG_ANSWER == 'true' ]];then
e4f9d3
e4f9d3
                ANSWER=${Y}
e4f9d3
e4f9d3
            else
e4f9d3
e4f9d3
                # Print the question.
e4f9d3
                cli_printMessage "$MESSAGE [${Y}/${N}]: " --as-notrailingnew-line
e4f9d3
e4f9d3
                # Redefine default answer based on user's input.
e4f9d3
                read ANSWER
e4f9d3
e4f9d3
            fi
e4f9d3
e4f9d3
            # Verify user's answer. Only positive answer let the
e4f9d3
            # script flow to continue. Otherwise, if something
e4f9d3
            # different from possitive answer is passed, the script
e4f9d3
            # terminates its execution immediatly.
e4f9d3
            if [[ ! ${ANSWER} =~ "^${Y}" ]];then
e4f9d3
                exit
e4f9d3
            fi
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-notrailingnew-line )
e4f9d3
            echo -n "$MESSAGE" > /dev/stderr
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-stdout-line )
e4f9d3
            echo "$MESSAGE"
e4f9d3
            ;;
e4f9d3
f015d3
        --as-stderr-line )
f015d3
            echo "$MESSAGE" > /dev/stderr
f015d3
            ;;
f015d3
e4f9d3
        * )
e4f9d3
            echo "$MESSAGE" \
e4f9d3
                | awk 'BEGIN { FS=": " }
e4f9d3
                    { 
f015d3
                        if ( $0 ~ /^-+$/ )
e4f9d3
                            print $0
e4f9d3
                        else
f015d3
                            printf "%-15s\t%s\n", $1, $2
f015d3
                    }
f015d3
                    END {}' > /dev/stderr
e4f9d3
            ;;
e4f9d3
e4f9d3
    esac
5b396a
4c79b5
}