Blame Scripts/Bash/Functions/Commons/cli_printMessage.sh

878a2b
#!/bin/bash
878a2b
#
878a2b
# cli_printMessage.sh -- This function standardizes the way messages
878a2b
# are printed out from centos-art.sh script.
878a2b
#
03486a
# Copyright (C) 2009, 2010, 2011, 2012 The CentOS Project
878a2b
#
878a2b
# This program is free software; you can redistribute it and/or modify
878a2b
# it under the terms of the GNU General Public License as published by
878a2b
# the Free Software Foundation; either version 2 of the License, or (at
878a2b
# your option) any later version.
878a2b
#
878a2b
# This program is distributed in the hope that it will be useful, but
878a2b
# WITHOUT ANY WARRANTY; without even the implied warranty of
878a2b
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
878a2b
# General Public License for more details.
878a2b
#
878a2b
# You should have received a copy of the GNU General Public License
878a2b
# along with this program; if not, write to the Free Software
878a2b
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
878a2b
#
878a2b
# ----------------------------------------------------------------------
878a2b
# $Id$
878a2b
# ----------------------------------------------------------------------
878a2b
878a2b
function cli_printMessage {
878a2b
878a2b
    # Verify `--quiet' option.
878a2b
    if [[ "$FLAG_QUIET" == 'true' ]];then
878a2b
        return
878a2b
    fi
878a2b
878a2b
    local MESSAGE="$1"
878a2b
    local FORMAT="$2"
878a2b
878a2b
    # Verify message variable, it cannot have an empty value.
878a2b
    if [[ $MESSAGE == '' ]];then
878a2b
        cli_printMessage "`gettext "The message cannot be empty."`" --as-error-line
878a2b
    fi
878a2b
878a2b
    # Define message horizontal width. This is the max number of
878a2b
    # horizontal characters the message will use to be displayed on
878a2b
    # the screen.
878a2b
    local MESSAGE_WIDTH=66
878a2b
878a2b
    # Reverse the codification performed on characters that may affect
878a2b
    # parsing options and non-option arguments. This codification is
878a2b
    # realized before building the ARGUMENTS variable, at
878a2b
    # cli_parseArgumentsReDef, and we need to reverse it back here
878a2b
    # in order to show the correct character when the message be
878a2b
    # printed out on the screen.
878a2b
    MESSAGE=$(echo $MESSAGE | sed -e "s/\\\0x27/'/g")
878a2b
878a2b
    # Remove empty spaces from message.
878a2b
    MESSAGE=$(echo $MESSAGE | sed -e 's!^[[:space:]]+!!')
878a2b
878a2b
    # Print out messages based on format.
878a2b
    case "$FORMAT" in
878a2b
878a2b
        --as-separator-line )
878a2b
878a2b
            # Build the separator line. 
878a2b
            MESSAGE=$(\
878a2b
                until [[ $MESSAGE_WIDTH -eq 0 ]];do
878a2b
                    echo -n "$MESSAGE"
878a2b
                    MESSAGE_WIDTH=$(($MESSAGE_WIDTH - 1))
878a2b
                done)
878a2b
878a2b
            # Draw the separator line.
878a2b
            echo "$MESSAGE" > /dev/stderr
878a2b
            ;;
878a2b
878a2b
        --as-banner-line )
878a2b
            cli_printMessage '-' --as-separator-line
878a2b
            cli_printMessage "$MESSAGE"
878a2b
            cli_printMessage '-' --as-separator-line
878a2b
            ;;
878a2b
878a2b
        --as-cropping-line )
878a2b
            cli_printMessage "`gettext "Cropping from"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-tuningup-line )
878a2b
            cli_printMessage "`gettext "Tuning-up"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-checking-line )
878a2b
            cli_printMessage "`gettext "Checking"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-creating-line | --as-updating-line )
878a2b
            if [[ -a "$MESSAGE" ]];then
878a2b
                cli_printMessage "`gettext "Updating"`: $MESSAGE"
878a2b
            else
878a2b
                cli_printMessage "`gettext "Creating"`: $MESSAGE"
878a2b
            fi
878a2b
            ;;
878a2b
878a2b
        --as-deleting-line )
878a2b
            cli_printMessage "`gettext "Deleting"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-reading-line )
878a2b
            cli_printMessage "`gettext "Reading"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-savedas-line )
878a2b
            cli_printMessage "`gettext "Saved as"`: $MESSAGE"
878a2b
            ;;
878a2b
            
878a2b
        --as-linkto-line )
878a2b
            cli_printMessage "`gettext "Linked to"`: $MESSAGE"
878a2b
            ;;
878a2b
        
878a2b
        --as-movedto-line )
878a2b
            cli_printMessage "`gettext "Moved to"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-translation-line )
878a2b
            cli_printMessage "`gettext "Translation"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-validating-line )
878a2b
            cli_printMessage "`gettext "Validating"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-template-line )
878a2b
            cli_printMessage "`gettext "Template"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-configuration-line )
878a2b
            cli_printMessage "`gettext "Configuration"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-palette-line )
878a2b
            cli_printMessage "`gettext "Palette"`: $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-response-line )
878a2b
            cli_printMessage "--> $MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-request-line )
878a2b
            cli_printMessage "${MESSAGE}:\040" --as-notrailingnew-line
878a2b
            ;;
878a2b
878a2b
        --as-selection-line )
878a2b
            local NAME=''
878a2b
            select NAME in ${MESSAGE};do
878a2b
                echo $NAME
878a2b
                break
878a2b
            done
878a2b
            ;;
878a2b
878a2b
        --as-error-line )
878a2b
            # Define where the error was originated inside the
878a2b
            # centos-art.sh script. Print out the function name and
878a2b
            # line from the caller.
878a2b
            local ORIGIN="$(caller 1 | gawk '{ print $2 " " $1 }')"
878a2b
878a2b
            # Build the error message.
fb7c3e
            cli_printMessage "${CLI_NAME} (${ORIGIN}): $MESSAGE" --as-stderr-line
878a2b
            cli_printMessage "${CLI_FUNCDIRNAM}" --as-toknowmore-line
878a2b
            ;;
878a2b
878a2b
        --as-toknowmore-line )
878a2b
            cli_printMessage '-' --as-separator-line
878a2b
            cli_printMessage "`gettext "To know more, run the following command"`:"
878a2b
            cli_printMessage "centos-art help --read trunk/Scripts/Functions/$MESSAGE"
878a2b
            cli_printMessage '-' --as-separator-line
43af5e
            exit 1 # <-- ATTENTION: Do not remove this line. We use this
43af5e
                   #                option as convenction to end script
43af5e
                   #                execution.
878a2b
            ;;
878a2b
    
878a2b
        --as-yesornorequest-line )
878a2b
            # Define positive answer.
878a2b
            local Y="`gettext "yes"`"
878a2b
878a2b
            # Define negative answer.
878a2b
            local N="`gettext "no"`"
878a2b
878a2b
            # Define default answer.
878a2b
            local ANSWER=${N}
878a2b
878a2b
            if [[ $FLAG_ANSWER == 'true' ]];then
878a2b
878a2b
                ANSWER=${Y}
878a2b
878a2b
            else
878a2b
878a2b
                # Print the question.
878a2b
                cli_printMessage "$MESSAGE [${Y}/${N}]:\040" --as-notrailingnew-line
878a2b
878a2b
                # Redefine default answer based on user's input.
878a2b
                read ANSWER
878a2b
878a2b
            fi
878a2b
878a2b
            # Verify user's answer. Only positive answer let the
878a2b
            # script flow to continue. Otherwise, if something
878a2b
            # different from possitive answer is passed, the script
878a2b
            # terminates its execution immediatly.
878a2b
            if [[ ! ${ANSWER} =~ "^${Y}" ]];then
878a2b
                exit
878a2b
            fi
878a2b
            ;;
878a2b
878a2b
        --as-notrailingnew-line )
878a2b
            echo -e -n "$MESSAGE" > /dev/stderr
878a2b
            ;;
878a2b
878a2b
        --as-stdout-line )
878a2b
            echo "$MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-stderr-line )
878a2b
            echo "$MESSAGE"
878a2b
            ;;
878a2b
878a2b
        * )
878a2b
878a2b
            # Default printing format. This is the format used when no
878a2b
            # other specification is passed to this function. As
878a2b
            # convenience, we transform absolute paths into relative
878a2b
            # paths in order to free horizontal space on final output
878a2b
            # messages.
878a2b
            echo "$MESSAGE" | sed -r \
8bc03d
                -e "s!${TCAR_WORKDIR}/(trunk|branches|tags)/!\1/!g" \
878a2b
                | awk 'BEGIN { FS=": " }
878a2b
                    { 
878a2b
                        if ( $0 ~ /^-+$/ )
878a2b
                            print $0
878a2b
                        else
878a2b
                            printf "%-15s\t%s\n", $1, $2
878a2b
                    }
878a2b
                    END {}' > /dev/stderr
878a2b
            ;;
878a2b
878a2b
    esac
878a2b
878a2b
}