diff --git a/Scripts/Functions/cli_printMessage.sh b/Scripts/Functions/cli_printMessage.sh index 03dd277..69b5977 100755 --- a/Scripts/Functions/cli_printMessage.sh +++ b/Scripts/Functions/cli_printMessage.sh @@ -1,8 +1,7 @@ #!/bin/bash # -# cli_printMessage.sh -- This function outputs information in -# predifined formats to standard error. This function is the standard -# way to output information inside centos-art.sh script. +# cli_printMessage.sh -- This function standardizes the way messages +# are printed out from centos-art.sh script. # # Copyright (C) 2009, 2010, 2011 The CentOS Project # @@ -31,30 +30,11 @@ function cli_printMessage { return fi - # Define short options. - local ARGSS='' - - # Define long options. - 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-stdout-line,' - - # Initialize arguments with an empty value and set it as local - # variable to this function scope. - local ARGUMENTS='' - - # Redefine ARGUMENTS variable using current positional parameters. - cli_doParseArgumentsReDef "$@" - - # Redefine ARGUMENTS variable using getopt output. - cli_doParseArguments - - # Redefine positional parameters using ARGUMENTS variable. - eval set -- "$ARGUMENTS" - - # Initialize message variable locally using non-option arguments. - local MESSAGE=$(echo $@ | sed -r 's!^.*--[[:space:]](.+)$!\1!') + local MESSAGE="$1" + local FORMAT="$2" # Verify message variable, it cannot have an empty value. - if [[ $MESSAGE == '--' ]];then + if [[ $MESSAGE == '' ]];then cli_printMessage "`gettext "The message cannot be empty."`" --as-error-line fi @@ -65,226 +45,171 @@ function cli_printMessage { # in order to show the correct character when the message be # printed out on the screen. MESSAGE=$(echo $MESSAGE | sed -e "s/\\\0x27/'/g") - + # Reduce paths and leading spaces from output messages. The main # purpose for this is to free horizontal space on output messages. MESSAGE=$(echo "$MESSAGE" | sed -r \ -e "s!${HOME}/artwork/(trunk|branches|tags)/!\1/!g" \ -e 's!^[[:space:]]+!!') - # Look for options passed through positional parameters. - while true; do - - case "$1" in - - --as-separator-line ) - - # Define width of separator line. - local MAX=70 - - # Build the separator line. - MESSAGE=$(\ - until [[ $MAX -eq 0 ]];do - echo -n "$MESSAGE" - MAX=$(($MAX - 1)) - done) - - # Draw the separator line. - echo "$MESSAGE" > /dev/stderr - - shift 2 - break - ;; - - --as-banner-line ) - cli_printMessage '-' --as-separator-line - cli_printMessage "$MESSAGE" - cli_printMessage '-' --as-separator-line - shift 2 - break - ;; - - --as-updating-line ) - cli_printMessage "`gettext "Updating"`: $MESSAGE" - shift 2 - break - ;; - - --as-cropping-line ) - cli_printMessage "`gettext "Cropping from"`: $MESSAGE" - shift 2 - break - ;; - - --as-tuningup-line ) - cli_printMessage "`gettext "Tuning-up"`: $MESSAGE" - shift 2 - break - ;; - - --as-checking-line ) - cli_printMessage "`gettext "Checking"`: $MESSAGE" - shift 2 - break - ;; - - --as-creating-line ) - cli_printMessage "`gettext "Creating"`: $MESSAGE" - shift 2 - break - ;; - - --as-deleting-line ) - cli_printMessage "`gettext "Deleting"`: $MESSAGE" - shift 2 - break - ;; - - --as-reading-line ) - cli_printMessage "`gettext "Reading"`: $MESSAGE" - shift 2 - break - ;; - - --as-savedas-line ) - cli_printMessage "`gettext "Saved as"`: $MESSAGE" - shift 2 - break - ;; + # Print out messages based on format. + case "$FORMAT" in + + --as-separator-line ) + + # Define width of separator line. + local MAX=70 + + # Build the separator line. + MESSAGE=$(\ + until [[ $MAX -eq 0 ]];do + echo -n "$MESSAGE" + MAX=$(($MAX - 1)) + done) + + # Draw the separator line. + echo "$MESSAGE" > /dev/stderr + ;; + + --as-banner-line ) + cli_printMessage '-' --as-separator-line + cli_printMessage "$MESSAGE" + cli_printMessage '-' --as-separator-line + ;; + + --as-updating-line ) + cli_printMessage "`gettext "Updating"`: $MESSAGE" + ;; + + --as-cropping-line ) + cli_printMessage "`gettext "Cropping from"`: $MESSAGE" + ;; + + --as-tuningup-line ) + cli_printMessage "`gettext "Tuning-up"`: $MESSAGE" + ;; + + --as-checking-line ) + cli_printMessage "`gettext "Checking"`: $MESSAGE" + ;; + + --as-creating-line ) + cli_printMessage "`gettext "Creating"`: $MESSAGE" + ;; + + --as-deleting-line ) + cli_printMessage "`gettext "Deleting"`: $MESSAGE" + ;; + + --as-reading-line ) + cli_printMessage "`gettext "Reading"`: $MESSAGE" + ;; + + --as-savedas-line ) + cli_printMessage "`gettext "Saved as"`: $MESSAGE" + ;; - --as-linkto-line ) - cli_printMessage "`gettext "Linked to"`: $MESSAGE" - shift 2 - break - ;; + --as-linkto-line ) + cli_printMessage "`gettext "Linked to"`: $MESSAGE" + ;; - --as-movedto-line ) - cli_printMessage "`gettext "Moved to"`: $MESSAGE" - shift 2 - break - ;; - - --as-translation-line ) - cli_printMessage "`gettext "Translation"`: $MESSAGE" - shift 2 - break - ;; - - --as-design-line ) - cli_printMessage "`gettext "Design"`: $MESSAGE" - shift 2 - break - ;; - - --as-configuration-line ) - cli_printMessage "`gettext "Configuration"`: $MESSAGE" - shift 2 - break - ;; - - --as-palette-line ) - cli_printMessage "`gettext "Palette"`: $MESSAGE" - shift 2 - break - ;; - - --as-response-line ) - echo "--> $MESSAGE" > /dev/stderr - shift 2 - break - ;; - - --as-request-line ) - cli_printMessage "${MESSAGE}: " --as-notrailingnew-line - shift 2 - break - ;; - - --as-error-line ) - # This option is used to print error messsages. - echo "${CLI_PROGRAM} ($(caller 1 | gawk '{ print $2 " " $1 }')): ${MESSAGE}" - cli_printMessage "${FUNCDIRNAM}" --as-toknowmore-line - shift 2 - break - ;; - - --as-toknowmore-line ) - cli_printMessage '-' --as-separator-line - cli_printMessage "`gettext "To know more, run the following command"`:" - cli_printMessage "centos-art help --read trunk/Scripts/Functions/$MESSAGE" - cli_printMessage '-' --as-separator-line - exit # <-- ATTENTION: Do not remove this line. We use this - # option as convenction to end script - # execution. - ;; + --as-movedto-line ) + cli_printMessage "`gettext "Moved to"`: $MESSAGE" + ;; + + --as-translation-line ) + cli_printMessage "`gettext "Translation"`: $MESSAGE" + ;; + + --as-design-line ) + cli_printMessage "`gettext "Design"`: $MESSAGE" + ;; + + --as-configuration-line ) + cli_printMessage "`gettext "Configuration"`: $MESSAGE" + ;; + + --as-palette-line ) + cli_printMessage "`gettext "Palette"`: $MESSAGE" + ;; + + --as-response-line ) + cli_printMessage "--> $MESSAGE" + ;; + + --as-request-line ) + cli_printMessage "${MESSAGE}: " --as-notrailingnew-line + ;; + + --as-error-line ) + # This option is used to print error messsages. + echo "${CLI_PROGRAM} ($(caller 1 | gawk '{ print $2 " " $1 }')): ${MESSAGE}" > /dev/stderr + cli_printMessage "${FUNCDIRNAM}" --as-toknowmore-line + ;; + + --as-toknowmore-line ) + cli_printMessage '-' --as-separator-line + cli_printMessage "`gettext "To know more, run the following command"`:" + cli_printMessage "centos-art help --read trunk/Scripts/Functions/$MESSAGE" + cli_printMessage '-' --as-separator-line + exit # <-- ATTENTION: Do not remove this line. We use this + # option as convenction to end script + # execution. + ;; - --as-yesornorequest-line ) - # Define positive answer. - local Y="`gettext "yes"`" - - # Define negative answer. - local N="`gettext "no"`" - - # Define default answer. - local ANSWER=${N} - - if [[ $FLAG_ANSWER == 'true' ]];then - - ANSWER=${Y} - - else - - # Print the question. - cli_printMessage "$MESSAGE [${Y}/${N}]: " --as-notrailingnew-line - - # Redefine default answer based on user's input. - read ANSWER - - fi - - # Verify user's answer. Only positive answer let the - # script flow to continue. Otherwise, if something - # different from possitive answer is passed, the - # script terminates its execution immediatly. - if [[ ! ${ANSWER} =~ "^${Y}" ]];then - exit - fi - - shift 2 - break - ;; - - --as-notrailingnew-line ) - printf "$MESSAGE" > /dev/stderr - shift 2 - break - ;; - - --as-stdout-line ) - echo "$MESSAGE" - shift 2 - break - ;; - - * ) - echo "$MESSAGE" \ - | awk 'BEGIN { FS=": " } - { - if ( $0 ~ /^-+$/ ) - print $0 - else - printf "%-15s\t%s\n", $1, $2 - } - END {}' > /dev/stderr - shift 2 - break - ;; - - esac - - done - - # Redefine ARGUMENTS variable using current positional parameters. - cli_doParseArgumentsReDef "$@" + --as-yesornorequest-line ) + # Define positive answer. + local Y="`gettext "yes"`" + + # Define negative answer. + local N="`gettext "no"`" + + # Define default answer. + local ANSWER=${N} + + if [[ $FLAG_ANSWER == 'true' ]];then + + ANSWER=${Y} + + else + + # Print the question. + cli_printMessage "$MESSAGE [${Y}/${N}]: " --as-notrailingnew-line + + # Redefine default answer based on user's input. + read ANSWER + + fi + + # Verify user's answer. Only positive answer let the + # script flow to continue. Otherwise, if something + # different from possitive answer is passed, the script + # terminates its execution immediatly. + if [[ ! ${ANSWER} =~ "^${Y}" ]];then + exit + fi + ;; + + --as-notrailingnew-line ) + echo -n "$MESSAGE" > /dev/stderr + ;; + + --as-stdout-line ) + echo "$MESSAGE" + ;; + + * ) + echo "$MESSAGE" \ + | awk 'BEGIN { FS=": " } + { + if ( $0 ~ /^-+$/ ) + print $0 + else + printf "%-15s\t%s\n", $1, $2 + } + END {}' > /dev/stderr + ;; + + esac }