Blame Scripts/centos-art/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
#
2fe9b7
# Copyright (C) 2009, 2010, 2011 The CentOS Project
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
2843de
    # Define message horizontal width. This is the max number of
2843de
    # horizontal characters the message will use to be displayed on
2843de
    # the screen.
35e7f8
    local MESSAGE_WIDTH=66
2843de
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
5ea081
    # Remove empty spaces from message.
5ea081
    MESSAGE=$(echo $MESSAGE | sed -e 's!^[[:space:]]+!!')
2fa569
e4f9d3
    # Print out messages based on format.
e4f9d3
    case "$FORMAT" in
e4f9d3
e4f9d3
        --as-separator-line )
e4f9d3
2843de
            # Build the separator line. 
2843de
            MESSAGE=$(\
2843de
                until [[ $MESSAGE_WIDTH -eq 0 ]];do
2843de
                    echo -n "$MESSAGE"
2843de
                    MESSAGE_WIDTH=$(($MESSAGE_WIDTH - 1))
2843de
                done)
e4f9d3
2843de
            # Draw the separator line.
2843de
            echo "$MESSAGE" > /dev/stderr
2843de
            ;;
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-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
5ea081
        --as-creating-line | --as-updating-line )
5ea081
            if [[ -a "$MESSAGE" ]];then
5ea081
                cli_printMessage "`gettext "Updating"`: $MESSAGE"
5ea081
            else
5ea081
                cli_printMessage "`gettext "Creating"`: $MESSAGE"
5ea081
            fi
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
6c620e
        --as-template-line )
6c620e
            cli_printMessage "`gettext "Template"`: $MESSAGE"
6c620e
            ;;
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 )
2843de
            cli_printMessage "${MESSAGE}:\040" --as-notrailingnew-line
e4f9d3
            ;;
e4f9d3
d83bf6
        --as-selection-line )
d83bf6
            local NAME=''
d83bf6
            select NAME in ${MESSAGE};do
d83bf6
                echo $NAME
d83bf6
                break
d83bf6
            done
d83bf6
            ;;
d83bf6
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.
35e7f8
            local ORIGIN="$(caller 1 | gawk '{ print $2 " " $1 }')"
f015d3
f015d3
            # Build the error message.
35e7f8
            cli_printMessage "${CLI_PROGRAM} (${ORIGIN}): $MESSAGE" --as-stderr-line
903cf1
            cli_printMessage "${CLI_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.
2843de
                cli_printMessage "$MESSAGE [${Y}/${N}]:\040" --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 )
2843de
            echo -e -n "$MESSAGE" > /dev/stderr
e4f9d3
            ;;
e4f9d3
e4f9d3
        --as-stdout-line )
e4f9d3
            echo "$MESSAGE"
e4f9d3
            ;;
e4f9d3
f015d3
        --as-stderr-line )
35e7f8
            echo "$MESSAGE"
f015d3
            ;;
f015d3
e4f9d3
        * )
5ea081
5ea081
            # Default printing format. This is the format used when no
5ea081
            # other specification is passed to this function. As
5ea081
            # convenience, we transform absolute paths into relative
5ea081
            # paths in order to free horizontal space on final output
5ea081
            # messages.
5ea081
            echo "$MESSAGE" | sed -r \
62fffe
                -e "s!${CLI_WRKCOPY}/(trunk|branches|tags)/!\1/!g" \
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
}