[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master 32cbe32 13/62: Table: Completion, better forma
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master 32cbe32 13/62: Table: Completion, better formatting, add function |
Date: |
Thu, 13 May 2021 22:20:46 -0400 (EDT) |
branch: master
commit 32cbe327ac928f9bb0653f9eaea21ba1aca8d346
Author: Pedram Ashofteh Ardakani <pedramardakani@pm.me>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Table: Completion, better formatting, add function
* Rename functions for consistency. Functions that populate the
completion suggestions contain '_list_' in their names.
* Functions that assign a value to a global variable such as getting a
fits file's name contain '_get_' in their names.
* Functions that use 'awk' and regular expressions are written in
several lines for better readablity.
---
bin/table/completion.sh | 51 ++++++++++++++++++++++++++++++++++---------------
1 file changed, 36 insertions(+), 15 deletions(-)
diff --git a/bin/table/completion.sh b/bin/table/completion.sh
index 9f2deff..e74bb69 100644
--- a/bin/table/completion.sh
+++ b/bin/table/completion.sh
@@ -10,6 +10,10 @@ PREFIX="/usr/local/bin";
ASTFITS="$PREFIX/astfits";
ASTTABLE="$PREFIX/asttable";
+# Use extended globs in the case statements if needed
+# https://mywiki.wooledge.org/BashGuide/Patterns#Extended_Globs
+# shopt -s extglob
+
# 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(){
@@ -19,13 +23,7 @@ _gnuastro_autocomplete_fits_hdu_read(){
_gnuastro_autocomplete_list_fits_files(){
# Suggest all 'FITS' files in current directory. Case insensitive.
- COMPREPLY=($(compgen -f -X "!*.[fF][iI][tT][sS]" -- $word));
-}
-
-_gnuastro_autocomplete_list_columns(){
- #
- local fits_file="$("${COMP_WORDS[@]}" | awk -v
regex="([a-z]|[A-Z])*.[fF][iI][tT][sS]" 'match($0, regex) {print substr($0,
RSTART, RLENGTH)}')"
- COMPREPLY=($(compgen -W "$($ASTTABLE --information $fits_file | awk -v
regex="^[0-9]+" 'match($0, regex) {print $2}')" -- "$word"))
+ COMPREPLY=($(compgen -f -X "!*.[fF][iI][tT][sS]" -- "$word"))
}
_gnuastro_autocomplete_expect_number(){
@@ -33,12 +31,32 @@ _gnuastro_autocomplete_expect_number(){
echo "Pass"
}
-_gnuastro_fits_last_occurance(){
- # The last FITS file in COMP_LINE
- echo "Pass"
+_gnuastro_autocomplete_get_fits_file(){
+ # Get the first fits file among the command line
+ # TODO: Add all other fits file extensions
+ comp_fits_file="$(echo ${COMP_WORDS[@]} | \
+ awk -v regex="([a-z]|[A-Z])*.[fF][iI][tT][sS]" \
+ 'match($0, regex) \
+ {print substr($0, RSTART, RLENGTH)}')"
}
-_gnuastro_file_last_occurance(){
+_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_file
+ # If the fits file does exist, fetch its column names
+ if [[ -f "$comp_fits_file" ]]; 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_file | \
+ awk -v regex="^[0-9]+" \
+ 'match($0, regex) \
+ {print $2}')")
+ COMPREPLY=($(compgen -W "${comp_fits_columns[@]}" -- "$word"))
+ fi
+}
+
+_gnuastro_autocomplete_get_file(){
# The last file name (.txt/.fits) in COMP_LINE
echo "Pass"
}
@@ -49,7 +67,11 @@ _gnuastro_file_last_occurance(){
_gnuastro_autocomplete_list_all_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"))
+ COMPREPLY=($(compgen -W "$($1 --help | \
+ awk -v regex=" --+([a-z]|[A-Z]|[0-9])*" \
+ 'match($0, regex) \
+ {print substr($0, RSTART, RLENGTH)}')" \
+ -- "$word"))
}
_gnuastro_asttable_completions(){
@@ -68,15 +90,14 @@ _gnuastro_asttable_completions(){
# Variable "prev" is the word just before the current word
local prev="${COMP_WORDS[COMP_CWORD-1]}";
+ # TODO: Prettify the code syntax, shorter ones on top
case "$prev" in
-i|--information) _gnuastro_autocomplete_list_fits_files ;;
- -c|--column) _gnuastro_autocomplete_list_columns ;;
+ -c|--column) _gnuastro_autocomplete_list_fits_columns ;;
-b|--noblank) ;;
-h|--hdu) ;;
- # The default case populates suggestions with all options available
*) _gnuastro_autocomplete_list_all_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 <=
- [gnuastro-commits] master d631f5b 15/62: Table: Completion, remove global variables, etc, Mohammad Akhlaghi, 2021/05/13
- [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