[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master 19ebe93 26/62: Table: Completion, improve _com
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master 19ebe93 26/62: Table: Completion, improve _compgen, put quotes |
Date: |
Thu, 13 May 2021 22:20:48 -0400 (EDT) |
branch: master
commit 19ebe93937dc70c97e1d30ab31b37f3cbdc7ac23
Author: Pedram Ashofteh Ardakani <pedramardakani@pm.me>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Table: Completion, improve _compgen, put quotes
Until now, the '_gnuastro_autocomplete_compgen' function took the
global variable '$word' and used it for filtering out possible
completions. With this commit, the function will take '$word' as its
second argument. This makes the function more independent and modular.
Also, Since double quotes '"..."' can be nested in command substitution
'$(...)' and don't care about the double quotes outside, I used them as
a precaution to prevent extra splitting and globs.
Lastly, there were minor formatting improvements and comment
elaborations.
---
bin/table/completion.sh | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)
diff --git a/bin/table/completion.sh b/bin/table/completion.sh
index 2ad7c81..a70e98e 100644
--- a/bin/table/completion.sh
+++ b/bin/table/completion.sh
@@ -55,25 +55,34 @@ _gnuastro_autocomplete_get_fits_hdu(){
}
_gnuastro_autocomplete_compgen(){
- # Accept either an array or a string split by normal bash conventions,
- # check if the current word being completed is present in the
- # suggestions, if so, put it in suggestions, continue otherwise.
+ # 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 is not passed, no filteration will be
+ # applied.
for w in $1
do
- [[ "$w" =~ $word ]] && COMPREPLY+=( "$w" )
+ # The right-hand side of '[[ ... =~ ... ]]' is considered a regex
+ # and should not be quoted or it will be taken as a literal.
+ # TODO: Let 'sed' or 'awk' do the regex matching if it screws up
+ # the portability.
+ [[ "$w" =~ $2 ]] && COMPREPLY+=( "$w" )
done
+
+ # Uncomment if you want to accept user's current word as a completion
+ # [[ -z "${COMPREPLY[*]}" ]] && COMPREPLY+=( "$2" )
}
_gnuastro_autocomplete_list_fits_hdu(){
# Checks for the current fits file and puts its headers into
# completion suggestions
if [ -f "$1" ]; then
- list=("$(_gnuastro_autocomplete_get_fits_hdu $1)")
+ list=("$(_gnuastro_autocomplete_get_fits_hdu "$1")")
# 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[@]}"
+ _gnuastro_autocomplete_compgen "${list[@]}" "$word"
unset list
fi
}
@@ -103,7 +112,7 @@ _gnuastro_autocomplete_get_fits_name(){
# 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?
- file_name="$(echo ${COMP_WORDS[@]} | awk -v
regex="[a-zA-Z0-9]*.[fF][iI][tT][sS]" 'match($0, regex) {print substr($0,
RSTART, RLENGTH)}')"
+ file_name="$(echo "${COMP_WORDS[@]}" | awk -v
regex="[a-zA-Z0-9]*.[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
@@ -126,7 +135,7 @@ _gnuastro_autocomplete_get_fits_columns(){
# 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.
- $ASTTABLE --information "$1" | awk 'NR>2' | awk '/^[0-9]/ {print $2}'
+ "$ASTTABLE" --information "$1" | awk 'NR>2' | awk '/^[0-9]/ {print $2}'
fi
}
@@ -134,8 +143,8 @@ _gnuastro_autocomplete_list_fits_columns(){
# Accept a fits file name as the first argument ($1). Read and suggest
# its column names. If the file does not exist, pass.
if [ -f "$1" ]; then
- list=$(_gnuastro_autocomplete_get_fits_columns "$1")
- COMPREPLY=($(compgen -W "${list[@]}"))
+ list=("$(_gnuastro_autocomplete_get_fits_columns "$1")")
+ _gnuastro_autocomplete_compgen "${list[@]}"
unset list
fi
}
@@ -155,8 +164,8 @@ _gnuastro_autocomplete_list_options(){
# 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.
- list=("$($1 --help | awk -v regex=' --+[a-zA-Z0-9]*=?' 'match($0, regex)
{print substr($0, RSTART, RLENGTH)}')")
- _gnuastro_autocomplete_compgen "${list[@]}"
+ list=("$("$1" --help | awk -v regex=" --+[a-zA-Z0-9]*=?" 'match($0, regex)
{print substr($0, RSTART, RLENGTH)}')")
+ _gnuastro_autocomplete_compgen "${list[@]}" "$word"
unset list
}
@@ -244,13 +253,14 @@ _gnuastro_asttable_completions(){
if [ $db -eq 0 ]; then
cat <<EOF
-*** DEBUG ***
+************ DEBUG ************
>>> prev: '$prev' -- \$3: '$3'
>>> word: '$word' -- \$2: '$2'
>>> fits_name: '$fits_name'
>>> COMPREPLY: '${COMPREPLY[@]}'
+*******************************
+>>> Line: "$COMP_LINE"
EOF
- printf ">>> line: %s" "$COMP_LINE"
fi
}
- [gnuastro-commits] master c593823 23/62: Table: Completion, fix hdu and fits suggestions, (continued)
- [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
- [gnuastro-commits] master 97d9a1e 09/62: Table: Completion suggests matching words, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 9ea367b 16/62: Table: Completion, handle '=', do not append space, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 7c95cec 24/62: Table: Completion, better POSIX portability, Mohammad Akhlaghi, 2021/05/13
- [gnuastro-commits] master 19ebe93 26/62: Table: Completion, improve _compgen, put quotes,
Mohammad Akhlaghi <=
- [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, 2021/05/13