gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master c259040 39/62: bin/table/completion.bash: Fix


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master c259040 39/62: bin/table/completion.bash: Fix '--information'
Date: Thu, 13 May 2021 22:20:51 -0400 (EDT)

branch: master
commit c25904020f57c8534401a2c1a93f39e50f6e1784
Author: Pedram Ashofteh Ardakani <pedramardakani@pm.me>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    bin/table/completion.bash: Fix '--information'
    
    Until now, if the user asked for information of a fits file, the
    autocompletion would still print other options as suggestions. With this
    commit, autocompletion checks if the '--information' option is invoked
    while also a valid fits file has been specified. In this case, the
    autocompletion will cease printing other suggestions, and keep telling
    the user that they can just press ENTER and move on.
    
    FIX: This will fix the problem mentioned in the commit below:
    e18d46e476dab290087ca7bcb1dd8f096f59923e
    
    The error showed up because there was no variable $NAME, replaced it with
    $PROG_NAME and the error '-bash: command : not found' went away.
---
 bin/table/completion.bash | 100 +++++++++++++++++++++++-----------------------
 1 file changed, 50 insertions(+), 50 deletions(-)

diff --git a/bin/table/completion.bash b/bin/table/completion.bash
index b94338b..2384b1b 100644
--- a/bin/table/completion.bash
+++ b/bin/table/completion.bash
@@ -101,6 +101,8 @@ _gnuastro_autocomplete_list_fits_hdu(){
         # 'hdu' names might contain dash symbols in them. This ensures that
         # all of them are suggested.
         _gnuastro_autocomplete_compgen "${list[@]}" "$word"
+        # TODO: If using `local` is allowed, remove the `unset` commands
+        # and declare variables as `local` instead
         unset list
     fi
 }
@@ -384,56 +386,54 @@ _gnuastro_asttable_completions(){
     # was a FITS table), will be put in 'last_table_hdu'.
     _gnuastro_autocomplete_last_table
 
-    # TODO: Prettify the code syntax, shorter ones on top
-    case "$prev" in
-        asttable)
-            _gnuastro_autocomplete_list_fits_names
-            _gnuastro_autocomplete_list_options $PROG_NAME
-            ;;
-        -i|--information)
-            if [ -f "$last_table" ]; then
-                # The extra, empty array element is a hack so Bash prints a
-                # warning in a separate line and waits for input.
-                #
-                # PROBLEM: In this scenario (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. But I can't get the script to do this!
-                COMPREPLY+=("The '--information' (or '-i') will disable all 
other options, you can safely press ENTER now", "")
-            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, and the information option is called, we
+    # should tell the user to avoid trying new options and just press ENTER
+    if [[ "$COMP_LINE" =~ "--information" ]] && [ -f "$last_table" ]; then
+        printf "\nThe '--information' (or '-i') will disable all other 
options, you can safely press ENTER now.\n%s" "$COMP_LINE"
+        COMPREPLY=()
+    else
+        # Regular completion
+        case "$prev" in
+            asttable)
                 _gnuastro_autocomplete_list_fits_names
-            fi
-            ;;
-        -L|--catcolumnfile|-w|--wcsfile)
-            # Only suggest a fits filename
-            _gnuastro_autocomplete_list_fits_names
-            ;;
-        -c|--column|-r|--range|-s|--sort|-C|--catcolumns| \
-            -m|--colmetadata|--inpolygon|--outpolygon| \
-            -e|--equal|-n|--notequal|-b|--noblank|--searchin)
-            # The function below returns the columns inside the last fits
-            # file specified in the commandline. If no fits files were
-            # detected, there will be no response from autocompletion. This
-            # might alert the user that something is going wrong.
-            _gnuastro_autocomplete_list_fits_columns "$last_table"
-            ;;
-        -W|--wcshdu|-u|--catcolumnhdu|-h|--hdu)
-            # Description is same as the '--column' option.
-            _gnuastro_autocomplete_list_fits_hdu "$last_table"
-            ;;
-        -o|--output|--polygon|-H|--head|-t|--tail| \
-            --onlyversion|-N|--numthreads|--minmapsize)
-            # Do not suggest anything.
-            ;;
-        --config)
-            # Suggest config files
-            COMPREPLY=($(compgen -f -X "!*.[cC][oO][nN][fF]" -- "$word"))
-            ;;
-        *) _gnuastro_autocomplete_list_options $NAME ;;
-    esac
+                _gnuastro_autocomplete_list_options $PROG_NAME
+                ;;
+            -i|--information)
+                if ! [ -f "$last_table" ]; then
+                    # 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.
+                    _gnuastro_autocomplete_list_fits_names
+                fi
+                ;;
+            -L|--catcolumnfile|-w|--wcsfile)
+                # Only suggest a fits filename
+                _gnuastro_autocomplete_list_fits_names
+                ;;
+            -c|--column|-r|--range|-s|--sort|-C|--catcolumns| \
+                -m|--colmetadata|--inpolygon|--outpolygon| \
+                -e|--equal|-n|--notequal|-b|--noblank|--searchin)
+                # The function below returns the columns inside the last fits
+                # file specified in the commandline. If no fits files were
+                # detected, there will be no response from autocompletion. This
+                # might alert the user that something is going wrong.
+                _gnuastro_autocomplete_list_fits_columns "$last_table"
+                ;;
+            -W|--wcshdu|-u|--catcolumnhdu|-h|--hdu)
+                # Description is same as the '--column' option.
+                _gnuastro_autocomplete_list_fits_hdu "$last_table"
+                ;;
+            -o|--output|--polygon|-H|--head|-t|--tail| \
+                --onlyversion|-N|--numthreads|--minmapsize)
+                # Do not suggest anything.
+                ;;
+            --config)
+                # Suggest config files
+                COMPREPLY=($(compgen -f -X "!*.[cC][oO][nN][fF]" -- "$word"))
+                ;;
+            *) _gnuastro_autocomplete_list_options $PROG_NAME ;;
+        esac
+    fi
 
     if [[ "${COMPREPLY[@]}" =~ "=" ]]; then
         # Do not append 'space' character to the end of line in case there
@@ -443,7 +443,7 @@ _gnuastro_asttable_completions(){
     fi
 
     # Be verbose in debugging mode, where $db is set to '0'.
-    if [ 1 = 0 ]; then
+    if [ $_gnuastro_completion_debug = 0 ]; then
         cat <<EOF
 
 ************ DEBUG ************



reply via email to

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