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

878a2b
#!/bin/bash
878a2b
#
878a2b
# cli_printMessage.sh -- This function standardizes the way messages
d08996
# are printed by 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
    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
    # Remove empty spaces from message.
b90d00
    MESSAGE=$(echo $MESSAGE | sed -r -e 's!^[[:space:]]+!!')
878a2b
5ff1d9
    # Print messages that will always be printed no matter what value
5ff1d9
    # the FLAG_QUIET variable has.
5ff1d9
    case "$FORMAT" in
5ff1d9
5ff1d9
        --as-stdout-line )
5ff1d9
5ff1d9
            # Default printing format. This is the format used when no
5ff1d9
            # other specification is passed to this function. As
5ff1d9
            # convenience, we transform absolute paths into relative
5ff1d9
            # paths in order to free horizontal space on final output
5ff1d9
            # messages.
5ff1d9
            echo "$MESSAGE" | sed -r \
5ff1d9
                -e "s!${TCAR_WORKDIR}/(trunk|branches|tags)/!\1/!g" \
c521b7
                -e "s!/{2,}!/!g" \
5ff1d9
                | awk 'BEGIN { FS=": " }
5ff1d9
                    { 
5ff1d9
                        if ( $0 ~ /^-+$/ )
5ff1d9
                            print $0
5ff1d9
                        else
5ff1d9
                            printf "%-15s\t%s\n", $1, $2
5ff1d9
                    }
5ff1d9
                    END {}'
5ff1d9
            ;;
5ff1d9
5ff1d9
        --as-error-line )
5ff1d9
5ff1d9
            # Define where the error was originated inside the
5ff1d9
            # centos-art.sh script. Print out the function name and
5ff1d9
            # line from the caller.
5ff1d9
            local ORIGIN="$(caller 1 | gawk '{ print $2 " L." $1 }')"
5ff1d9
5ff1d9
            # Build the error message.
5ff1d9
            cli_printMessage "${CLI_NAME} (${ORIGIN}):" --as-stdout-line
5ff1d9
            cli_printMessage "${MESSAGE}" --as-response-line
5ff1d9
            cli_printMessage "${CLI_FUNCDIRNAM}" --as-toknowmore-line
5ff1d9
            ;;
5ff1d9
5ff1d9
        --as-toknowmore-line )
5ff1d9
            cli_printMessage '-' --as-separator-line
5ff1d9
            cli_printMessage "`gettext "To know more, run the following command"`:" --as-stdout-line
5ff1d9
            cli_printMessage "centos-art help --read trunk/Scripts/Functions/$MESSAGE" --as-stdout-line
5ff1d9
            cli_printMessage '-' --as-separator-line
5ff1d9
            exit # <-- ATTENTION: Do not remove this line. We use this
5ff1d9
                 #                option as convenction to end script
5ff1d9
                 #                execution.
5ff1d9
            ;;
5ff1d9
5ff1d9
        --as-yesornorequest-line )
5ff1d9
5ff1d9
            # Define positive answer.
5ff1d9
            local Y="`gettext "yes"`"
5ff1d9
5ff1d9
            # Define negative answer.
5ff1d9
            local N="`gettext "no"`"
5ff1d9
5ff1d9
            # Define default answer.
5ff1d9
            local ANSWER=${N}
5ff1d9
5ff1d9
            if [[ $FLAG_ANSWER == 'true' ]];then
5ff1d9
5ff1d9
                ANSWER=${Y}
5ff1d9
5ff1d9
            else
5ff1d9
5ff1d9
                # Print the question to standard error.
5ff1d9
                cli_printMessage "$MESSAGE [${Y}/${N}]" --as-request-line
5ff1d9
5ff1d9
                # Redefine default answer based on user's input.
5ff1d9
                read ANSWER
5ff1d9
5ff1d9
            fi
5ff1d9
5ff1d9
            # Verify user's answer. Only positive answer let the
5ff1d9
            # script flow to continue. Otherwise, if something
5ff1d9
            # different from positive answer is passed, the script
5ff1d9
            # terminates its execution immediately.
5ff1d9
            if [[ ! ${ANSWER} =~ "^${Y}" ]];then
5ff1d9
                exit
5ff1d9
            fi
5ff1d9
            ;;
5ff1d9
5ff1d9
        --as-selection-line )
5ff1d9
            # Create selection based on message.
5ff1d9
            local NAME=''
5ff1d9
            select NAME in ${MESSAGE};do
5ff1d9
                echo $NAME
5ff1d9
                break
5ff1d9
            done
5ff1d9
            ;;
5ff1d9
5ff1d9
        --as-response-line )
5ff1d9
            cli_printMessage "--> $MESSAGE" --as-stdout-line
5ff1d9
            ;;
5ff1d9
5ff1d9
        --as-request-line )
5ff1d9
            cli_printMessage "${MESSAGE}:\040" --as-notrailingnew-line
5ff1d9
            ;;
5ff1d9
5ff1d9
        --as-notrailingnew-line )
5ff1d9
            echo -e -n "${MESSAGE}" | sed -r \
5ff1d9
                -e "s!${TCAR_WORKDIR}/(trunk|branches|tags)/!\1/!g"
5ff1d9
            ;;
5ff1d9
5ff1d9
        --as-stderr-line )
5ff1d9
            echo "$MESSAGE" | sed -r \
5ff1d9
                -e "s!${TCAR_WORKDIR}/(trunk|branches|tags)/!\1/!g" 1>&2
5ff1d9
            ;;
5ff1d9
5ff1d9
    esac
5ff1d9
5ff1d9
    # Verify verbose option. The verbose option controls whether
5ff1d9
    # messages are printed or not.
5ff1d9
    if [[ "$FLAG_QUIET" == 'true' ]];then
5ff1d9
        return
5ff1d9
    fi
5ff1d9
5ff1d9
    # Print messages that will be printed only when the FLAG_QUIET
5ff1d9
    # variable is provided to centos-art.sh script.
878a2b
    case "$FORMAT" in
878a2b
878a2b
        --as-separator-line )
878a2b
5ff1d9
            # Build the separator line.
878a2b
            MESSAGE=$(\
878a2b
                until [[ $MESSAGE_WIDTH -eq 0 ]];do
00187b
                    echo -n "$(echo $MESSAGE | sed -r 's!(.).*!\1!')"
878a2b
                    MESSAGE_WIDTH=$(($MESSAGE_WIDTH - 1))
878a2b
                done)
878a2b
878a2b
            # Draw the separator line.
00187b
            echo "$MESSAGE"
878a2b
            ;;
878a2b
878a2b
        --as-banner-line )
878a2b
            cli_printMessage '-' --as-separator-line
5ff1d9
            cli_printMessage "$MESSAGE" --as-stdout-line
878a2b
            cli_printMessage '-' --as-separator-line
878a2b
            ;;
878a2b
878a2b
        --as-cropping-line )
5ff1d9
            cli_printMessage "`gettext "Cropping from"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
878a2b
        --as-tuningup-line )
5ff1d9
            cli_printMessage "`gettext "Tuning-up"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
878a2b
        --as-checking-line )
5ff1d9
            cli_printMessage "`gettext "Checking"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
633e7c
        --as-combining-line )
5ff1d9
            cli_printMessage "`gettext "Combining"`: $MESSAGE" --as-stdout-line
633e7c
            ;;
633e7c
878a2b
        --as-creating-line | --as-updating-line )
878a2b
            if [[ -a "$MESSAGE" ]];then
5ff1d9
                cli_printMessage "`gettext "Updating"`: $MESSAGE" --as-stdout-line
878a2b
            else
5ff1d9
                cli_printMessage "`gettext "Creating"`: $MESSAGE" --as-stdout-line
878a2b
            fi
878a2b
            ;;
878a2b
878a2b
        --as-deleting-line )
5ff1d9
            cli_printMessage "`gettext "Deleting"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
878a2b
        --as-reading-line )
5ff1d9
            cli_printMessage "`gettext "Reading"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
878a2b
        --as-savedas-line )
5ff1d9
            cli_printMessage "`gettext "Saved as"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
            
878a2b
        --as-linkto-line )
5ff1d9
            cli_printMessage "`gettext "Linked to"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
        
878a2b
        --as-movedto-line )
5ff1d9
            cli_printMessage "`gettext "Moved to"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
878a2b
        --as-translation-line )
5ff1d9
            cli_printMessage "`gettext "Translation"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
878a2b
        --as-validating-line )
5ff1d9
            cli_printMessage "`gettext "Validating"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
878a2b
        --as-template-line )
5ff1d9
            cli_printMessage "`gettext "Template"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
878a2b
        --as-configuration-line )
5ff1d9
            cli_printMessage "`gettext "Configuration"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
878a2b
        --as-palette-line )
5ff1d9
            cli_printMessage "`gettext "Palette"`: $MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
5ff1d9
        --as-inkscape-line )
5ff1d9
            cli_printMessage "$MESSAGE" --as-stdout-line
878a2b
            ;;
878a2b
878a2b
    esac
878a2b
878a2b
}