[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master 86df9d8 25/62: Table: Completion, custom compg
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master 86df9d8 25/62: Table: Completion, custom compgen |
Date: |
Thu, 13 May 2021 22:20:48 -0400 (EDT) |
branch: master
commit 86df9d899619ecaff3e524ee3f0bb6c834162ce8
Author: Pedram Ashofteh Ardakani <pedramardakani@pm.me>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Table: Completion, custom compgen
Until now, the completions used the built-in 'compgen' command. But
'compgen' did not handle '-' inside a single suggestion such as
'QUERY-CONFIG' for '--wcshdu' option. With this commit, the
'_gnuastro_autocomplete_compgen' is introduced. The behavior is
completely predictable.
Also, the global variables are 'unset' when they are not needed.
Lastly, the '=' sign is handled well in this version. It is removed if
it is counted as the current word, and it is ignored if it was counted
as the previous word. This way we have a way of knowing which long
option is being completed. Furthermore, this prevents some complications
while we are filtering out the suggestions.
---
bin/table/completion.sh | 53 ++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 44 insertions(+), 9 deletions(-)
diff --git a/bin/table/completion.sh b/bin/table/completion.sh
index 814eba3..2ad7c81 100644
--- a/bin/table/completion.sh
+++ b/bin/table/completion.sh
@@ -54,12 +54,27 @@ _gnuastro_autocomplete_get_fits_hdu(){
fi
}
+_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.
+ for w in $1
+ do
+ [[ "$w" =~ $word ]] && COMPREPLY+=( "$w" )
+ done
+}
+
_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")
- COMPREPLY=($(compgen -W "$list"))
+ 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[@]}"
+ unset list
fi
}
@@ -120,7 +135,8 @@ _gnuastro_autocomplete_list_fits_columns(){
# 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"))
+ COMPREPLY=($(compgen -W "${list[@]}"))
+ unset list
fi
}
@@ -139,8 +155,9 @@ _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)}')
- COMPREPLY+=($(compgen -W "$list" -- "$word"))
+ list=("$($1 --help | awk -v regex=' --+[a-zA-Z0-9]*=?' 'match($0, regex)
{print substr($0, RSTART, RLENGTH)}')")
+ _gnuastro_autocomplete_compgen "${list[@]}"
+ unset list
}
_gnuastro_asttable_completions(){
@@ -153,11 +170,28 @@ _gnuastro_asttable_completions(){
# Initialize the completion response with null
COMPREPLY=();
- # Variable "word", is the current word being completed
- word="${COMP_WORDS[COMP_CWORD]}";
+ # Variable "word", is the current word being completed. "$2" is the
+ # default value for the current word in completion scripts. But we are
+ # using the longer form: "${COMP_WORDS[COMP_CWORD]}" for clarity.
+ word="${COMP_WORDS[COMP_CWORD]}"
+ if [ "$word" = "=" ]; then
+ # The equal sign '=' raises complexities when filling suggestions
+ # for long options. Things will work out fine when they are simply
+ # ignored.
+ word=""
+ fi
- # Variable "prev" is the word just before the current word
- prev="${COMP_WORDS[COMP_CWORD-1]}";
+ # Variable "prev", is one word before the one being completed. By
+ # default, this is set as "$3" in completion scripts. But we are using
+ # the longer form: "${COMP_WORDS[COMP_CWORD-1]}" for clarity.
+ prev="${COMP_WORDS[COMP_CWORD-1]}"
+ if [ "$prev" = "=" ]; then
+ # While a user is writing a long option's argument, the previous
+ # word will be the equal sign '='. This is not helpful at all. But
+ # looking at a word just before '=' helps us understand which long
+ # option is being called upon.
+ prev="${COMP_WORDS[COMP_CWORD-2]}"
+ 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
@@ -214,6 +248,7 @@ _gnuastro_asttable_completions(){
>>> prev: '$prev' -- \$3: '$3'
>>> word: '$word' -- \$2: '$2'
>>> fits_name: '$fits_name'
+>>> COMPREPLY: '${COMPREPLY[@]}'
EOF
printf ">>> line: %s" "$COMP_LINE"
fi
- [gnuastro-commits] master 97d9a1e 09/62: Table: Completion suggests matching words, (continued)
- [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, 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 <=
- [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, 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