From db09717957b0474fab4d7ee9d694b93c47c1c61b Mon Sep 17 00:00:00 2001 From: Alain Reguera Delgado Date: Feb 19 2011 19:58:29 +0000 Subject: Update cli_printMessage.sh. - Start using standard error (/dev/stderr) to output information. - Reduce FLAG_QUIET verifications and use of awk to base printing options only. Base printing options are reused by other printing options. So, there is no need to duplicate FLAG_QUIET verifications in all of them independently, doing so in base printing option will also apply all options that use base printing. One exception is the AsSeparatorLine option. If we use base printing here, the construction of separator line is too much evident to the human eye (at least mine at this moment). Instead use printf and echo to draw the separator and enclose the drawing inside a FLAG_QUIET verification. - Remove AsHeadingLine, AsWarningLine and AsNoteLine. They haven't been used since a long time now and it seems there is no need to use them in the future either. So, clean up. --- diff --git a/Scripts/Bash/Functions/cli_printMessage.sh b/Scripts/Bash/Functions/cli_printMessage.sh index 0d73ae1..94c55a2 100755 --- a/Scripts/Bash/Functions/cli_printMessage.sh +++ b/Scripts/Bash/Functions/cli_printMessage.sh @@ -1,13 +1,9 @@ #!/bin/bash # # cli_printMessage.sh -- This function outputs information in -# predifined formats. This function (cli_printMessage) is the standard +# predifined formats to standard error. This function is the standard # way to output information inside centos-art.sh script. # -# cli_printMessage $1 $2 -# $1 --> The message you want to output. -# $2 --> The message format. -# # Copyright (C) 2009-2011 Alain Reguera Delgado # # This program is free software; you can redistribute it and/or @@ -31,12 +27,8 @@ function cli_printMessage { - # Define variables as local to avoid conflicts outside. local MESSAGE="$1" local FORMAT="$2" - local Y='' - local N='' - local ANSWER='' # Reduce paths inside output messages. The main purpose for this # is to free horizontal space in output messages. @@ -49,194 +41,143 @@ function cli_printMessage { # Define message formats. case $FORMAT in - 'AsHeadingLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo '----------------------------------------------------------------------' - echo "$MESSAGE" | fmt --width=70 - echo '----------------------------------------------------------------------' - fi - ;; - - 'AsWarningLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo '----------------------------------------------------------------------' - echo "`gettext "WARNING"`: $MESSAGE" | fmt --width=70 - echo '----------------------------------------------------------------------' - fi - ;; - - 'AsNoteLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo '----------------------------------------------------------------------' - echo "`gettext "NOTE"`: $MESSAGE" | fmt --width=70 - echo '----------------------------------------------------------------------' - fi - ;; - 'AsUpdatingLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Updating"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Updating"`: $MESSAGE" ;; 'AsDeletingLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Deleting"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Deleting"`: $MESSAGE" ;; 'AsCheckingLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Checking"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Checking"`: $MESSAGE" ;; 'AsCreatingLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Creating"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Creating"`: $MESSAGE" ;; 'AsSavedAsLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Saved as"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Saved as"`: $MESSAGE" ;; 'AsLinkToLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Linked to"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Linked to"`: $MESSAGE" ;; 'AsMovedToLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Moved to"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Moved to"`: $MESSAGE" ;; 'AsTranslationLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Translation"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Translation"`: $MESSAGE" ;; 'AsDesignLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Design"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Design"`: $MESSAGE" ;; 'AsConfigurationLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Configuration"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Configuration"`: $MESSAGE" + cli_printMessage '-' 'AsSeparatorLine' ;; 'AsPaletteLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "`gettext "Palette"`: $MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk - fi + cli_printMessage "`gettext "Palette"`: $MESSAGE" ;; 'AsResponseLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "--> $MESSAGE" - fi + cli_printMessage "--> $MESSAGE" ;; 'AsRequestLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo -n "${MESSAGE}: " - fi + cli_printMessage "${MESSAGE}: " 'AsNoTrailingNewLine' ;; - 'AsSeparatorLine' ) - - # Define separator lenght. - local MAX=70 - - if [[ $FLAG_QUIET == 'false' ]];then - - # Draw separator line. - until [[ $MAX -eq 0 ]];do - echo -n "${MESSAGE}" - MAX=$(($MAX - 1)) - done - - # Output newline to end separator. - echo + 'AsErrorLine' ) + # This option is used to print error messsages. + cli_printMessage "${CLINAME}: ${MESSAGE}" + ;; - fi + 'AsToKnowMoreLine' ) + # This option receives the output of bash's caller + # built-in as message value and produces the documentation + # entry from it. + MESSAGE=$(cli_getRepoName "$(echo $MESSAGE | cut -d ' ' -f2-)" 'd') + cli_printMessage '-' 'AsSeparatorLine' + cli_printMessage "`gettext "To know more, run the following command"`:" + cli_printMessage "centos-art manual --read='$MESSAGE'" + cli_printMessage '-' 'AsSeparatorLine' + exit # <-- ATTENTION: Do not remove this line. We use this + # option as convenction to end script + # execution. ;; 'AsYesOrNoRequestLine' ) + # Define positive answer. - Y="`gettext "y"`" + local Y="`gettext "yes"`" # Define negative answer. - N="`gettext "N"`" + local N="`gettext "no"`" - # Define `yes or no' confirmation question. - if [[ $FLAG_QUIET == 'false' ]] && [[ $FLAG_YES == 'false' ]];then - echo -n "$MESSAGE [${Y}/${N}]: " - read ANSWER - fi + if [[ $FLAG_ANSWER == 'false' ]];then - # Redefine answer based on answer flag. - if [[ $FLAG_YES == 'true' ]];then - ANSWER=${Y} - fi + # Print the question. + cli_printMessage "$MESSAGE [${Y}/${N}]: " 'AsNoTrailingNewLine' + + # Wait for user's answer to be entered. + cli_printMessage "FLAG_ANSWER" 'AsReadLine' + + # 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 [[ ! ${FLAG_ANSWER} =~ "^${Y}" ]];then + exit + fi - # Verify answer. - if [[ ! $ANSWER =~ "^${Y}" ]];then - exit fi ;; - 'AsErrorLine' ) - # Add script name to message. We want to know where - # messages come from. The `--quiet' flag doesn't suppress - # error messages. - echo "${CLINAME}: ${MESSAGE}" + 'AsReadLine' ) + if [[ $FLAG_QUIET == 'false' ]];then + # Use variable indirect expantion to redefine the + # value of the variable of name passed as message. + read ${!MESSAGE} + fi ;; - 'AsToKnowMoreLine' ) - # This option receives the output of bash's caller builtin - # as message value, in order to produce the documentation - # entry automatically. The `--quiet' flag doesn't suppress - # `as to know more' messages. - MESSAGE=$(cli_getRepoName "$(echo $MESSAGE | cut -d ' ' -f2-)" 'd') - echo "----------------------------------------------------------------------" - echo "`gettext "To know more, run the following command"`:" - echo "centos-art manual --read='$MESSAGE'" - echo "----------------------------------------------------------------------" - exit # <-- ATTENTION: Do not remove this line. We use this - # case as convenction to end script - # execution. + 'AsSeparatorLine' ) + + if [[ "$FLAG_QUIET" == 'false' ]];then + + # Define separator width. + local MAX=70 + + # Draw separator. + until [[ $MAX -eq 0 ]];do + printf "${MESSAGE}" + MAX=$(($MAX - 1)) + done + + # Output newline to end separator. + echo "" + + fi ;; - 'AsRegularLine' ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "$MESSAGE" \ - | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk + 'AsNoTrailingNewLine' ) + if [[ "$FLAG_QUIET" == 'false' ]];then + printf "$MESSAGE" > /dev/stderr fi ;; - * ) - if [[ $FLAG_QUIET == 'false' ]];then - echo "$MESSAGE" + 'AsRegularLine' | * ) + if [[ "$FLAG_QUIET" == 'false' ]];then + echo "$MESSAGE" \ + | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk \ + > /dev/stderr fi ;;