gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master e340452 42/62: Auto-completion: Minor polishin


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master e340452 42/62: Auto-completion: Minor polishing of the warning message
Date: Thu, 13 May 2021 22:20:52 -0400 (EDT)

branch: master
commit e3404524da5f723ef896e3204328205a661f5c8b
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    Auto-completion: Minor polishing of the warning message
    
    Until now, the warning message for the '--information' option was a single
    string that was repeated in two places (adding a check for it in general
    was a good idea Pedram!). However, based on previous experience, this can
    cause bugs if we later edit one of them and forget to the edit the other!
    
    With this commit a new 'infowarning' variable is defined at the top of the
    function and message is put there. That variable can now be used anywhere
    in the code, and all the time it will have the same value.
    
    Other changes with this commit:
    
     - When the 'last_table' variable is not empty, the existance of its file
       has already been checked. So we should just check if its an empty string
       or not. Don't forget that checking the existance of a file is
       computationally expensive (requires Bash to leave its internal
       evironment and go to the operating system!).
    
     - In my case the previous '$' wasn't enough to print the prompt. The new
       line simply started with a '$' sign, not my standard prompt. So I added
       '${PS1@P}'. As mentioned above it, there is a portability issue here
       with older Bash systems. We should find a more portable solution.
    
     - I removed the extra '-----' lines: they are distracting and waste a lot
       of valuable terminal lines ;-)!
    
    [To help in future reference to commits related to auto-completion, let's
    start the commit title with 'Auto-completion'.]
---
 bin/table/completion.bash | 41 ++++++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/bin/table/completion.bash b/bin/table/completion.bash
index 7d74e07..3472301 100644
--- a/bin/table/completion.bash
+++ b/bin/table/completion.bash
@@ -353,16 +353,15 @@ _gnuastro_autocomplete_list_options(){
 
 # Prints the message taken as $1 and asks for user action. Then reprints
 # the former prompt, which lives in $COMP_LINE.
+#
+# TODO: The newly printed prompt was taken from here:
+# https://stackoverflow.com/questions/22322879/how-to-print-current-bash-prompt
+# This is only available since Bash 4.4 (September 2016). We should find
+# A more low-level/basic operation.
 _gnuastro_autocomplete_print_message(){
     if ! [ x"$1" = x ]; then
-        printf "\n------------------------\n"
-        printf "$1"
-        printf "\n------------------------\n"
-        printf "\n\$ %s" "$COMP_LINE"
-        return 0
-    else
-        # Return 1 if the argument is NULL or not specified.
-        return 1
+        printf "\n$1\n"
+        printf "${PS1@P} %s" "$COMP_LINE"
     fi
 }
 
@@ -376,6 +375,9 @@ _gnuastro_asttable_completions(){
     # Initialize the completion response with null
     COMPREPLY=();
 
+    # Strings used in multiple places.
+    local infowarning="With '--information' (or '-i') all other options are 
disabled, you can press ENTER now."
+
     # Variable "word", is the current word being completed. "$2" is the
     # default value for the current word in completion scripts. But we are
     # using the longer form: "${COMP_WORDS[COMP_CWORD]}" for clarity.
@@ -411,18 +413,16 @@ _gnuastro_asttable_completions(){
             _gnuastro_autocomplete_list_options $PROG_NAME
             ;;
         -i|--information)
-            # when a File has been given, and the information option is
-            # called, we should tell the user to avoid trying new options
-            # and just press ENTER
-            if [ -f "$last_table" ]; then
-                _gnuastro_autocomplete_print_message \
-                    "The '--information' (or '-i') will disable all other 
options. You can safely press ENTER now."
-                COMPREPLY=()
-            else
-                # Check if the user has already specified a fits file. If
-                # the _gnuastro_autocomplete_get_file_name echoes an empty
-                # response, it means no fits files were specified.
+            # when a file has been given before this, and the
+            # '--information' option is called, we should tell the user to
+            # avoid trying new options and just press ENTER. Otherwise, we
+            # should let the user choose a FITS table (to view its
+            # information).
+            if [ x"$last_table" = x ]; then
                 _gnuastro_autocomplete_list_fits_names
+            else
+                _gnuastro_autocomplete_print_message "$infowarning"
+                COMPREPLY=()
             fi
             ;;
         -L|--catcolumnfile|-w|--wcsfile)
@@ -459,8 +459,7 @@ _gnuastro_asttable_completions(){
             if echo "$COMP_LINE" \
                     | grep -e ' --information' -e ' -i' &> /dev/null \
                     &&  [ -f "$last_table" ]; then
-                _gnuastro_autocomplete_print_message \
-                    "The '--information' (or '-i') will disable all other 
options. You can safely press ENTER now."
+                _gnuastro_autocomplete_print_message "$infowarning"
                 COMPREPLY=()
             else
                 _gnuastro_autocomplete_list_options $PROG_NAME



reply via email to

[Prev in Thread] Current Thread [Next in Thread]