Blame Scripts/Functions/cli_doPrint.sh

4c79b5
#!/bin/bash
4c79b5
#
6d2593
# cli_doPrint.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
#
2d3646
# 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
6d2593
function cli_doPrint {
4c79b5
49237e
    # Verify `--quiet' option.
49237e
    if [[ "$FLAG_QUIET" == 'true' ]];then
49237e
        return
49237e
    fi
49237e
6d2593
    # Initialize message variable as empty.
6d2593
    local MESSAGE=''
bc9c90
6d2593
    # Define short options.
6d2593
    local ARGSS=''
c62219
6d2593
    # Define long options.
6d2593
    local ARGSL='message:,as-separator-line,as-banner-line,as-updating-line,as-cropping-line,as-tuningup-line,as-deleting-line,as-checking-line,as-creating-line,as-reading-line,as-savedas-line,as-linkto-line,as-movedto-line,as-translation-line,as-design-line,as-configuration-line,as-palette-line,as-response-line,as-request-line,as-error-line,as-toknowmore-line,as-yesornorequest-line,as-notrailingnew-line,as-regular-line,'
c62219
6d2593
    # Define ARGUMENTS as local variable in order to parse options
6d2593
    # from this function internlally.
6d2593
    local ARGUMENTS=''
c62219
6d2593
    # Redefine ARGUMENTS variable using current positional parameters. 
6d2593
    cli_doParseArgumentsReDef "$@"
03f43a
6d2593
    # Redefine ARGUMENTS variable using getopt output.
6d2593
    cli_doParseArguments
c62219
6d2593
    # Redefine positional parameters using ARGUMENTS variable.
6d2593
    eval set -- "$ARGUMENTS"
f259c7
6d2593
    # Look for options passed through positional parameters.
6d2593
    while true; do
903248
6d2593
        case "$1" in
c62219
6d2593
            '--message' )
c62219
6d2593
                # Redefine message.
6d2593
                MESSAGE="$2"
c62219
6d2593
                # Reduce paths inside output messages. The main
6d2593
                # purpose for this is to free horizontal space in
6d2593
                # output messages.
6d2593
                MESSAGE=$(echo "$MESSAGE" \
6d2593
                    | sed -r "s!${HOME}/artwork/(trunk|branches|tags)/!\1/!g")
c62219
6d2593
                # Remove blank spaces from lines' begining.
6d2593
                MESSAGE=$(echo "$MESSAGE" | sed -r 's!^[[:space:]]+!!')
6d2593
6d2593
                shift 2
6d2593
                ;;
6d2593
6d2593
            '--as-separator-line' )
6d2593
6d2593
                # Define separator width.
6d2593
                local MAX=70
6d2593
6d2593
                # Draw separator.
6d2593
                until [[ $MAX -eq 0 ]];do
6d2593
                    printf "$MESSAGE" > /dev/stderr
6d2593
                    MAX=$(($MAX - 1))
6d2593
                done
6d2593
6d2593
                # Output newline to end separator.
6d2593
                echo "" > /dev/stderr
6d2593
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-banner-line' )
6d2593
                cli_doPrint --message='-' --as-separator-line
6d2593
                cli_doPrint --message="$MESSAGE"
6d2593
                cli_doPrint --message='-' --as-separator-line
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-updating-line' )
6d2593
                cli_doPrint --message="`gettext "Updating"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-cropping-line' )
6d2593
                cli_doPrint --message="`gettext "Cropping from"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-tuningup-line' )
6d2593
                cli_doPrint --message="`gettext "Tuning-up"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-deleting-line' )
6d2593
                cli_doPrint --message="`gettext "Deleting"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-checking-line' )
6d2593
                cli_doPrint --message="`gettext "Checking"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-creating-line' )
6d2593
                cli_doPrint --message="`gettext "Creating"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-reading-line' )
6d2593
                cli_doPrint --message="`gettext "Reading"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-savedas-line' )
6d2593
                cli_doPrint --message="`gettext "Saved as"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
c62219
            
6d2593
            '--as-linkto-line' )
6d2593
                cli_doPrint --message="`gettext "Linked to"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
c62219
        
6d2593
            '--as-movedto-line' )
6d2593
                cli_doPrint --message="`gettext "Moved to"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-translation-line' )
6d2593
                cli_doPrint --message="`gettext "Translation"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-design-line' )
6d2593
                cli_doPrint --message="`gettext "Design"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-configuration-line' )
6d2593
                cli_doPrint --message="`gettext "Configuration"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-palette-line' )
6d2593
                cli_doPrint --message="`gettext "Palette"`: $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-response-line' )
6d2593
                cli_doPrint --message="--> $MESSAGE"
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-request-line' )
6d2593
                cli_doPrint --message="${MESSAGE}: " --as-notrailingnew-line
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-error-line' )
6d2593
                # This option is used to print error messsages.
6d2593
                echo "${CLI_PROGRAM} (${FUNCNAME[1]}): ${MESSAGE}" > /dev/stderr
6d2593
                cli_doPrint --message="${FUNCDIRNAM}" --as-toknowmore-line
6d2593
                break
6d2593
                ;;
6d2593
6d2593
            '--as-toknowmore-line' )
6d2593
                # This option receives the output of bash's caller
6d2593
                # built-in as message value and produces the
6d2593
                # documentation entry from it.
6d2593
                MESSAGE="trunk/Scripts/Functions/$MESSAGE"
6d2593
                cli_doPrint --message='-' --as-separator-line
6d2593
                cli_doPrint --message="`gettext "To know more, run the following command"`:"
6d2593
                cli_doPrint --message="centos-art help --read $MESSAGE"
6d2593
                cli_doPrint --message='-' --as-separator-line
6d2593
                exit # <-- ATTENTION: Do not remove this line. We use this
6d2593
                     #                option as convenction to end script
6d2593
                     #                execution.
6d2593
                ;;
c62219
    
6d2593
            '--as-yesornorequest-line' )
6d2593
                # Define positive answer.
6d2593
                local Y="`gettext "yes"`"
bc9c90
6d2593
                # Define negative answer.
6d2593
                local N="`gettext "no"`"
bc9c90
6d2593
                # Define default answer.
6d2593
                local ANSWER=${N}
3ccb0a
6d2593
                if [[ $FLAG_ANSWER == 'true' ]];then
3ccb0a
6d2593
                    ANSWER=${Y}
bc9c90
6d2593
                else
bc9c90
6d2593
                    # Print the question.
6d2593
                    cli_doPrint --message="$MESSAGE [${Y}/${N}]: " --as-notrailingnew-line
bc9c90
6d2593
                    # Redefine default answer based on user's input.
6d2593
                    read ANSWER
bc9c90
6d2593
                fi
bc9c90
6d2593
                # Verify user's answer. Only positive answer let the
6d2593
                # script flow to continue. Otherwise, if something
6d2593
                # different from possitive answer is passed, the
6d2593
                # script terminates its execution immediatly.
6d2593
                if [[ ! ${ANSWER} =~ "^${Y}" ]];then
6d2593
                    exit
6d2593
                fi
bc9c90
6d2593
                break
6d2593
                ;;
bc9c90
6d2593
            '--as-notrailingnew-line' )
6d2593
                printf "$MESSAGE" > /dev/stderr
6d2593
                break
6d2593
                ;;
bc9c90
6d2593
            '--as-regular-line' | * )
6d2593
                echo "$MESSAGE" \
6d2593
                        | awk 'BEGIN { FS=": " }
6d2593
                            { 
6d2593
                            if ( $0 ~ /^-+$/ ) 
6d2593
                                print $0
6d2593
                            else
6d2593
                                printf "%-15s\t%s\n", $1, $2 
6d2593
                            }
6d2593
                            END {}' > /dev/stderr
6d2593
                break
6d2593
                ;;
bc9c90
6d2593
            '--' )
6d2593
                shift 1
6d2593
                break
6d2593
                ;;
bc9c90
6d2593
        esac
bc9c90
6d2593
    done
4c79b5
4c79b5
}