[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master 8150e1c 50/62: Auto-completion: Refactor FITS
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master 8150e1c 50/62: Auto-completion: Refactor FITS related functions |
Date: |
Thu, 13 May 2021 22:20:53 -0400 (EDT) |
branch: master
commit 8150e1c08d6be9ee63a3b42979edb3009a040fa5
Author: Pedram Ashofteh Ardakani <pedramardakani@pm.me>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Auto-completion: Refactor FITS related functions
Until now, each function that had something to do with fits file had a
different approach of making sure the input file is actually a fits file.
However, Mohammad wrote a special function that checks if an input file
is an actual existing FITS file, regardless of its extension. With this
commit, I moved those functions below the '..._is_fits' function.
From now on, we should be careful to feed only valid FITS file to fits
accepting functions, or there will be confusing errors.
Also, I revised some comments on different functions and replaced some
variables with their human-readable substitutes.
---
bin/table/completion.bash | 95 ++++++++++++++++++++++-------------------------
1 file changed, 44 insertions(+), 51 deletions(-)
diff --git a/bin/table/completion.bash b/bin/table/completion.bash
index ddb3618..5638665 100644
--- a/bin/table/completion.bash
+++ b/bin/table/completion.bash
@@ -58,18 +58,6 @@ _gnuastro_asttable="$_gnuastro_prefix/asttable";
-# Accepts a FITS filename as input and echoes its headers.
-_gnuastro_autocomplete_get_fits_hdu(){
- local inputfile="$1"
- if [ -f $inputfile ]; then
- $_gnuastro_astfits --quiet $inputfile | awk '{print $2}'
- fi
-}
-
-
-
-
-
# Accept either an array or a string '$1' split by normal bash conventions,
# check if second argument '$2' is present in the suggestions, if so, put
# it in suggestions, continue otherwise. Note, in case the second agument
@@ -89,18 +77,13 @@ _gnuastro_autocomplete_compgen(){
-# Checks for the current fits file and puts its headers into completion
-# suggestions
-_gnuastro_autocomplete_list_fits_hdu(){
+
+# Check if the given file is a FITS file (that can actually be
+# opened). Note that FITS files have many possible extensions (see the
+# 'gal_fits_name_is_fits' function in 'lib/fits.c').
+_gnuastro_autocomplete_is_fits(){
local inputfile="$1"
- if [ -f $inputfile ]; then
- local list=("$(_gnuastro_autocomplete_get_fits_hdu $inputfile)")
- # A custom enhancement for the 'compgen' command. This version will
- # have no problem with the dash sign '-'. Because sometimes the
- # 'hdu' names might contain dash symbols in them. This ensures that
- # all of them are suggested.
- _gnuastro_autocomplete_compgen "${list[@]}" "$word"
- fi
+ if $_gnuastro_astfits $inputfile -h0 &> /dev/null; then return 0; else
return 1; fi
}
@@ -126,7 +109,7 @@ _gnuastro_autocomplete_list_fits_hdu(){
_gnuastro_autocomplete_list_fits_names(){
local files=($(ls | grep -e "^$word" --color=never))
for f in ${files[*]} ; do
- if $_gnuastro_astfits "$f" -q &> /dev/null; then COMPREPLY+=("$f"); fi
+ if _gnuastro_autocomplete_is_fits "$f"; then COMPREPLY+=("$f"); fi
done
}
@@ -151,7 +134,7 @@ _gnuastro_autocomplete_list_all_valid_files(){
for f in ${files[*]} ; do
if _gnuastro_autocomplete_is_plaintext_table "$f"; then
COMPREPLY+=("$f"); fi
- if $_gnuastro_astfits "$f" -q &> /dev/null; then COMPREPLY+=("$f"); fi
+ if _gnuastro_autocomplete_is_fits "$f"; then COMPREPLY+=("$f"); fi
done
}
@@ -165,14 +148,26 @@ _gnuastro_autocomplete_expect_number(){
+# Accepts a FITS filename as input and echoes its headers.
+_gnuastro_autocomplete_get_fits_hdu(){
+ local inputfile="$1"
+ $_gnuastro_astfits --quiet $inputfile | awk '{print $2}'
+}
-# Check if the given file is a FITS file (that can actually be
-# opened). Note that FITS files have many possible extensions (see the
-# 'gal_fits_name_is_fits' function in 'lib/fits.c').
-_gnuastro_autocomplete_is_fits(){
+
+
+
+# Checks for the current fits file and puts its headers into completion
+# suggestions
+_gnuastro_autocomplete_list_fits_hdu(){
local inputfile="$1"
- if $_gnuastro_astfits $inputfile -h0 &> /dev/null; then return 0; else
return 1; fi
+ local list=("$(_gnuastro_autocomplete_get_fits_hdu $inputfile)")
+ # A custom enhancement for the 'compgen' command. This version will
+ # have no problem with the dash sign '-'. Because sometimes the
+ # 'hdu' names might contain dash symbols in them. This ensures that
+ # all of them are suggested.
+ _gnuastro_autocomplete_compgen "${list[@]}" "$word"
}
@@ -324,32 +319,29 @@ _gnuastro_autocomplete_last_table(){
-# Checks if the argument contains a valid file. Does not check for its
-# extension. Then, reads the column names using the asttable program and
-# echoes the resulting STR.
+# Accepts a valid FITS file. Then, reads the column names using the
+# asttable program and echoes the resulting STR.
+#
+# Force 'awk' to read after the second line of 'asttable' output,
+# because the second line contains the filename. The filename might
+# start with numbers. If so, there will be an unwanted '(hdu:'
+# printed in the results. Here, 'awk' will print the second column
+# in lines that start with a number.
_gnuastro_autocomplete_get_fits_columns(){
- if [ -f "$1" ]; then
- # Force 'awk' to read after the second line of 'asttable' output,
- # because the second line contains the filename. The filename might
- # start with numbers. If so, there will be an unwanted '(hdu:'
- # printed in the results. Here, 'awk' will print the second column
- # in lines that start with a number.
- $_gnuastro_asttable --information "$1" \
- | awk 'NR>2' \
- | awk '/^[0-9]/ {print $2}'
- fi
+ local inputfile="$1"
+ $_gnuastro_asttable --information $inputfile \
+ | awk 'NR>2' \
+ | awk '/^[0-9]/ {print $2}'
}
-# Accept a fits file name as the first argument ($1). Read and suggest its
-# column names. If the file does not exist, pass.
+# Accept a valid FITS file and suggest its column names.
_gnuastro_autocomplete_list_fits_columns(){
- if [ -f "$1" ]; then
- local list=("$(_gnuastro_autocomplete_get_fits_columns "$1")")
- _gnuastro_autocomplete_compgen "${list[@]}"
- fi
+ local inputfile="$1"
+ local list=("$(_gnuastro_autocomplete_get_fits_columns "$1")")
+ _gnuastro_autocomplete_compgen "${list[@]}"
}
@@ -364,14 +356,15 @@ _gnuastro_autocomplete_get_file(){
-# Accept the command name and its absolute path, run the --help option and
+# Accept the program name and its absolute path, run the --help option and
# 'append' all long options to the current suggestions. 'Appending' seems a
# good idea because the program might accept multiple input types. For
# example the 'asttable' program can either accept a fits file or various
# short/long options as its first argument. In this case, autocompletion
# suggests both.
_gnuastro_autocomplete_list_options(){
- local list=("$("$1" --help \
+ local input_program="$1"
+ local list=("$($input_program --help \
| awk -v regex=" --+[a-zA-Z0-9]*=?" \
'match($0, regex) \
{print substr($0, RSTART, RLENGTH)}')")
- [gnuastro-commits] master c259040 39/62: bin/table/completion.bash: Fix '--information', (continued)
- [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, 2021/05/13
- [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 <=
- [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
- [gnuastro-commits] master 6379566 36/62: Book: edits to the Bash auto-completion section, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 9335953 44/62: Auto-completion: Faster fits parse, use 'local', Mohammad Akhlaghi, 2021/05/13