[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master 229ac51 41/62: /bin/table/completion.bash: Ref
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master 229ac51 41/62: /bin/table/completion.bash: Refactor code |
Date: |
Thu, 13 May 2021 22:20:52 -0400 (EDT) |
branch: master
commit 229ac5197d27f8bea14f5e396727dd77b6265294
Author: Pedram Ashofteh Ardakani <pedramardakani@pm.me>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
/bin/table/completion.bash: Refactor code
Until now, I used a case inside an 'if' statement. The syntax looked
pretty confusing and redundant. In this commit, the completion script
will scan the entire prompt for '-i' or '--information' option. If it
finds any instances, it will check if there is a valid fits file
specified. If both cases are TRUE, the completion will print a message
and let the user know that s/he can press ENTER safely.
Also, I declared a new _gnuastro_autocomplete_print_message function.
This function will take only one argument, and pretty prints it. Then,
it will reprint the user prompt with a '$' at line beginning for a
natural and familiar experience.
Last but not least, I used 'grep' to scan the prompt. This is POSIX
compatible and easier to implement when there is more than one pattern
to check.
---
bin/table/completion.bash | 125 +++++++++++++++++++++++++++++-----------------
1 file changed, 79 insertions(+), 46 deletions(-)
diff --git a/bin/table/completion.bash b/bin/table/completion.bash
index 2a8ad5d..7d74e07 100644
--- a/bin/table/completion.bash
+++ b/bin/table/completion.bash
@@ -351,6 +351,24 @@ _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.
+_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
+ fi
+}
+
+
+
+
_gnuastro_asttable_completions(){
# Basic definitions.
PROG_NAME=$1
@@ -386,54 +404,69 @@ _gnuastro_asttable_completions(){
# was a FITS table), will be put in 'last_table_hdu'.
_gnuastro_autocomplete_last_table
- # 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" ] && [[ "$COMP_LINE" =~ "--information" ]] || [[
"$COMP_LINE" =~ " -i" ]]; 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)
+
+ case "$prev" in
+ asttable)
+ _gnuastro_autocomplete_list_fits_names
+ _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.
_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"))
+ ;;
+ *)
+ # Check the entire prompt line $COMP_LINE, if the '-i' or
+ # '--information' option is passed and there is a valid fits
+ # file present in the command line, prompt user that they can
+ # safely press ENTER since this configuration disables all
+ # other options. Otherwise, just print all available options.
+ 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."
+ COMPREPLY=()
+ else
_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
+ fi
+ ;;
+ esac
if [[ "${COMPREPLY[@]}" =~ "=" ]]; then
# Do not append 'space' character to the end of line in case there
- [gnuastro-commits] master 19ebe93 26/62: Table: Completion, improve _compgen, put quotes, (continued)
- [gnuastro-commits] master 19ebe93 26/62: Table: Completion, improve _compgen, put quotes, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 7b584a4 31/62: Table: Completion, update shebang, fix formatting, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master c259040 39/62: bin/table/completion.bash: Fix '--information', Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 6efd4ff 32/62: Book: Add auto-complete to the developing section, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 0e382bc 11/62: Table: Completion, suggest columns inside fits, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 328ab31 19/62: Table: Completion, thoughts on short options, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master b5c4f29 27/62: Table: Completion, add options, improve get name, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 86df9d8 25/62: Table: Completion, custom compgen, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 51daecd 30/62: Table: Completion, update copyright, minor edits, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master e690fc5 37/62: Bash completion: renamed fixed file to completion.bash, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 229ac51 41/62: /bin/table/completion.bash: Refactor code,
Mohammad Akhlaghi <=
- [gnuastro-commits] master 50026ec 48/62: Auto-completion: Rename boolean query functions, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 8150e1c 50/62: Auto-completion: Refactor FITS related functions, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master afadccf 52/62: Auto-completion: Call ..._last_table where needed, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 90f08c2 53/62: Auto-completion: Refactor ..._is_table function, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 4598eba 54/62: Auto-completion: clean re-implementation for Table, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 59669d9 61/62: Book: new section on known issues with Crop, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master b947f79 62/62: TAB completion: enabled in Fits program, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master acf58c1 58/62: TAB completion: now supported in ConvertType and Convolve, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 9ae830f 34/62: Table: Completion, replace double negative, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 7f1a203 35/62: Book: Autocompletion for developers example, Mohammad Akhlaghi, 2021/05/13