[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master d631f5b 15/62: Table: Completion, remove globa
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master d631f5b 15/62: Table: Completion, remove global variables, etc |
Date: |
Thu, 13 May 2021 22:20:47 -0400 (EDT) |
branch: master
commit d631f5bbc981570ec078a7c09542192addb9d718
Author: Pedram Ashofteh Ardakani <pedramardakani@pm.me>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Table: Completion, remove global variables, etc
* Until now, the program relied on using global variables for holding
file names, etc. With this commit, the behaviour is changed with
modularity in mind. Instead of storing values in global variables, the
values are 'echo'ed. This means each function can be an independent
simple program.
* Unitl now, the functions did not check if the passed arguments are
actual files or not. With this commit, each function will check if the
passed argument is actually a file, i.e:
$ [ -f $file_name ] && echo "$file_name is present"
* Add information about the -X option in the 'compgen' command
* Try not to extend long codes to new lines so much. This can lead to
confusion in the developing process. Because there will be so many
things counted as 'modified' in the git history. We do not want that
for now.
* Add the 'wcshdu' option to suggestions.
---
bin/table/completion.sh | 95 +++++++++++++++++++++++++++++++------------------
1 file changed, 60 insertions(+), 35 deletions(-)
diff --git a/bin/table/completion.sh b/bin/table/completion.sh
index 4caa021..219dd0d 100644
--- a/bin/table/completion.sh
+++ b/bin/table/completion.sh
@@ -22,13 +22,18 @@ ASTTABLE="$PREFIX/asttable";
# astquery gaia --dataset=edr3 --center=24,25 --radius=0.1 --output=gaia.fits
--column=ra,dec,parallax --quiet -i | awk '/[0-9]+/ {print $2}'
-_gnuastro_autocomplete_fits_hdu_read(){
+_gnuastro_autocomplete_list_fits_hdu(){
# Accepts a fits filename as input and suggests its headers
- COMPREPLY=($("$ASTFITS --quiet $1" | awk '{print $2}'))
+ if [[ -f "$1" ]]; then
+ local list="$($ASTFITS --quiet $1 | awk '{print $2}')"
+ COMPREPLY=($(compgen -W "$list" -- "$word"))
+ fi
}
_gnuastro_autocomplete_list_fits_names(){
- # Suggest all 'FITS' files in current directory. Case insensitive.
+ # Suggest all 'FITS' files in current directory. Case insensitive. The
+ # -X option and its filter pattern are explained on bash programmable
+ # completion info page: $ info bash programmable
COMPREPLY=($(compgen -f -X "!*.[fF][iI][tT][sS]" -- "$word"))
}
@@ -38,27 +43,33 @@ _gnuastro_autocomplete_expect_number(){
}
_gnuastro_autocomplete_get_fits_name(){
- # Get the first fits file among the command line
- # TODO: Add all other fits file extensions
- comp_fits_name="$(echo ${COMP_WORDS[@]} | \
- awk -v regex="([a-z]|[A-Z])*.[fF][iI][tT][sS]" \
- 'match($0, regex) \
- {print substr($0, RSTART, RLENGTH)}')"
+ # Get the first fits file among the command line and put it into the
+ # $comp_fits_name variable
+ # TODO: How about all other fits file extensions?
+ local file_name="$(echo ${COMP_WORDS[@]} | awk -v
regex="[a-zA-Z]*.[fF][iI][tT][sS]" 'match($0, regex) {print substr($0, RSTART,
RLENGTH)}')"
+ if [ -f "$file_name" ]; then
+ # Check if file_name is actually an existing fits file. This
+ # prevents other functions from failing and producing obscure error
+ # messages
+ echo "$file_name"
+ fi
+}
+
+_gnuastro_autocomplete_get_fits_columns(){
+ # Checks if the argument contains a valid file. Does not check for its
+ # extension. Then, reads the column names using the asttable program
+ # and assigns the result to the $comp_fits_columns array.
+ if [ -f "$1" ]; then
+ echo "$($ASTTABLE --information $1 | awk -v regex="^[0-9]+" 'match($0,
regex) {print $2}')"
+ fi
}
_gnuastro_autocomplete_list_fits_columns(){
- # Get the fits file name in current command line, put into
- # the $comp_fits_file variable
- _gnuastro_autocomplete_get_fits_name
- # If the fits file does exist, fetch its column names
- if [[ -f "$comp_fits_name" ]]; then
- # Set a global array named comp_fits_columns that contains all columns
- # inside the fits file specified in the first argument: $1.
- comp_fits_columns=("$($ASTTABLE --information $comp_fits_name | \
- awk -v regex="^[0-9]+" \
- 'match($0, regex) \
- {print $2}')")
- COMPREPLY=($(compgen -W "${comp_fits_columns[@]}" -- "$word"))
+ # Accept a fits file name as the first argument ($1). Read and suggest
+ # its column names
+ if [ -f "$1" ]; then
+ local list="$(_gnuastro_autocomplete_get_fits_columns $1)"
+ COMPREPLY=($(compgen -W "$list" -- "$word"))
fi
}
@@ -71,13 +82,10 @@ _gnuastro_autocomplete_get_file(){
# astconvolve --help | awk -v pattern="^ *-([a-z]|[A-Z])" 'match($0, pattern)
{print $0}'
_gnuastro_autocomplete_list_options(){
- # The regex variable in the awk program contains the regular expression
- # pattern that matches all options provided in corresponding program
- COMPREPLY=($(compgen -W "$($1 --help | \
- awk -v regex=" --+([a-z]|[A-Z]|[0-9])*" \
- 'match($0, regex) \
- {print substr($0, RSTART, RLENGTH)}')" \
- -- "$word"))
+ # Accept the command name and its absolute path, run the --help option
+ # and print all long options available.
+ local list="$($1 --help | awk -v regex=" --+[a-zA-Z0-9]*" 'match($0,
regex) {print substr($0, RSTART, RLENGTH)}')"
+ COMPREPLY=($(compgen -W "$list" -- "$word"))
}
_gnuastro_asttable_completions(){
@@ -96,15 +104,32 @@ _gnuastro_asttable_completions(){
# Variable "prev" is the word just before the current word
local prev="${COMP_WORDS[COMP_CWORD-1]}";
+ # In case the option contains an equal sign '=' complexities arise.
+ # This is because bash considers '=' as a wordbreak. The WORDBREAK
+ # variable can be altered but it is discouraged. Instead, we will treat
+ # each case carefully.
+ #
+ if [ "$prev" == "=" ]; then prev="${COMP_WORDS[COMP_CWORD-2]}"; fi
+ if [ "$word" == "=" ]; then word="$prev"; fi
+
+ # A quick check to see if there is already a fits file name invoked in
+ # the current commandline. This means the order of commands does matter
+ # in this bash completion. If we do not want this, we should implement
+ # another method for suggesting completions.
+ local fits_name="$(_gnuastro_autocomplete_get_fits_name)"
+
# TODO: Prettify the code syntax, shorter ones on top
case "$prev" in
-i|--information) _gnuastro_autocomplete_list_fits_names ;;
- -c|--column) _gnuastro_autocomplete_list_fits_columns ;;
- -w|--wcsfile) _gnuastro_autocomplete_list_fits_names ;;
- -b|--noblank) ;;
- -h|--hdu) ;;
- *) _gnuastro_autocomplete_list_options $PROG_NAME ;;
- esac
-}
+ -c|--column)
+ # echo "fits_name: $fits_name"
+ # _gnuastro_autocomplete_list_fits_columns "$fits_name"
+ local fits_columns="$(_gnuastro_autocomplete_get_fits_columns
'$fits_name')"
+ printf "\n*** DEBUG ***\n>>> prev: $prev\n>>> word: $word\n>>>
line: ${COMP_LINE[@]}"
+ ;;
+ -w|--wcsfile) _gnuastro_autocomplete_list_fits_names ;;
+ -W|--wcshdu) _gnuastro_autocomplete_list_fits_hdu "$fits_name"
+ ;; -b|--noblank) ;; -h|--hdu) ;; *)
+ _gnuastro_autocomplete_list_options $PROG_NAME ;; esac }
complete -F _gnuastro_asttable_completions asttable
- [gnuastro-commits] master updated (ffc7608 -> b947f79), Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 1c1fe3d 01/62: New feature: Auto Complete (WIP), Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master c02ae2c 02/62: Completion: feed options from `help`, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master b06fbce 03/62: Completion: Fix indentations, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master a829b24 05/62: Completion: Suggest FITS files in relevant options, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 3da00c3 07/62: Table: Prepare initial completion script, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 477d23d 06/62: Rename completion files, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 39efeea 08/62: Table: First working completion, general commands, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 32cbe32 13/62: Table: Completion, better formatting, add function, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master d631f5b 15/62: Table: Completion, remove global variables, etc,
Mohammad Akhlaghi <=
- [gnuastro-commits] master f4cf33d 17/62: Tabel: Completion, examine for fits availability, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master e1c1a4b 18/62: Table: Completion, fix 'wcshdu', Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 6fe9cb6 20/62: Table: Completion, refactor awk regex fits name, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master c593823 23/62: Table: Completion, fix hdu and fits suggestions, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 0420521 04/62: Completion: bug fix in catching the options, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 3d8e96d 10/62: Table: Fix completion issue, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master f2ccd6e 12/62: Table: Completion, fix reading fits column, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master d0e1362 14/62: Table: Completion, better function name, copyright, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 7ad725a 21/62: Table: Completion, fix hdu and fits suggestions, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master b391ee8 22/62: Table: Completion, bash4+ long option no space, Mohammad Akhlaghi, 2021/05/13