[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master 7c95cec 24/62: Table: Completion, better POSIX
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master 7c95cec 24/62: Table: Completion, better POSIX portability |
Date: |
Thu, 13 May 2021 22:20:48 -0400 (EDT) |
branch: master
commit 7c95cecb972b20e30c94db1dce34a2eaee7ee29a
Author: Pedram Ashofteh Ardakani <pedramardakani@pm.me>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Table: Completion, better POSIX portability
Until now, portability was not heavily checked. With this commit,
warnings from 'shellcheck' are taken into consideration and acted upon
wherever possible.
Added few words about my thoughts on portability. In short, taking bash
version 3+ (2004) into consideration should suffice since it looks like
MacOS has bash 3.2 installed as its default shell environment.
Use '#!/usr/bin/env bash' instead of '#/usr/bin/env bash'
Fix redundancy of: '$ echo $(cmd)' with 'cmd'
Remove 'local' variables as it is not POSIX portable
TODO: There are more warnings that need tending to.
---
bin/table/completion.sh | 42 ++++++++++++++++++++++++++----------------
1 file changed, 26 insertions(+), 16 deletions(-)
diff --git a/bin/table/completion.sh b/bin/table/completion.sh
index 23b0510..814eba3 100644
--- a/bin/table/completion.sh
+++ b/bin/table/completion.sh
@@ -1,4 +1,4 @@
-#/usr/bin/env bash
+#!/usr/bin/env bash
# TODO: GNU Copyright ...
# Original Author:
@@ -21,6 +21,15 @@
# command on the current commandline, there should be no completion
# suggestions.
+# A thought on portability and obeying POSIX standards. This autocomplete
+# script should be compatible to bash 3.0 (2004) and newer. That is because
+# the MacOS standard is still around bash 3.2. Some commands such as
+# '[[ =~', 'complete', arrays, etc. are not POSIX compatible. But they are bash
+# built-in.
+
+# TODO: There should be some way to keep autocomplete from crashing in
+# older systems.
+
# TIP: Run the command below to initialize the bash completion feature for
# this specific program (i.e. astcosmiccal):
# $ source astcosmiccal-completion.bash
@@ -41,7 +50,7 @@ db=0 # Set 0 for printing debug messages, else set to 1
_gnuastro_autocomplete_get_fits_hdu(){
# Accepts a fits filename as input and echoes its headers
if [ -f "$1" ]; then
- echo "$($ASTFITS --quiet $1 | awk '{print $2}')"
+ $ASTFITS --quiet "$1" | awk '{print $2}'
fi
}
@@ -49,8 +58,8 @@ _gnuastro_autocomplete_list_fits_hdu(){
# Checks for the current fits file and puts its headers into
# completion suggestions
if [ -f "$1" ]; then
- local list="$(_gnuastro_autocomplete_get_fits_hdu $1)"
- COMPREPLY=($(compgen -W "${list[@]}"))
+ list=$(_gnuastro_autocomplete_get_fits_hdu "$1")
+ COMPREPLY=($(compgen -W "$list"))
fi
}
@@ -79,7 +88,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?
- local 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
@@ -102,7 +111,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.
- echo "$($ASTTABLE --information $1 | awk 'NR>2' | awk '/^[0-9]/ {print
$2}')"
+ $ASTTABLE --information "$1" | awk 'NR>2' | awk '/^[0-9]/ {print $2}'
fi
}
@@ -110,7 +119,7 @@ _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
- local list="$(_gnuastro_autocomplete_get_fits_columns $1)"
+ list=$(_gnuastro_autocomplete_get_fits_columns "$1")
COMPREPLY=($(compgen -W "$list"))
fi
}
@@ -130,31 +139,31 @@ _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.
- local list=$("$1" --help | awk -v regex=" --+[a-zA-Z0-9]*=?" 'match($0,
regex) {print substr($0, RSTART, RLENGTH)}')
+ 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(){
# TODO: @@
- local PROG_NAME="asttable";
+ PROG_NAME="asttable";
- local PROG_ADDRESS="$PREFIX/$PROG_NAME";
+ PROG_ADDRESS="$PREFIX/$PROG_NAME";
# Initialize the completion response with null
COMPREPLY=();
# Variable "word", is the current word being completed
- local word="${COMP_WORDS[COMP_CWORD]}";
+ word="${COMP_WORDS[COMP_CWORD]}";
# Variable "prev" is the word just before the current word
- local prev="${COMP_WORDS[COMP_CWORD-1]}";
+ prev="${COMP_WORDS[COMP_CWORD-1]}";
# 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)"
+ fits_name="$(_gnuastro_autocomplete_get_fits_name)"
# TODO: Prettify the code syntax, shorter ones on top
case "$prev" in
@@ -191,8 +200,9 @@ _gnuastro_asttable_completions(){
esac
if [[ ! "${COMPREPLY[@]}" =~ "=" ]]; then
- # '[[' and 'compopt' work for bash 4+
- # TODO: Find workaround for older bash
+ # '[[' and 'compopt' work for bash 3+ so it should be portable to
+ # systems younger than 2004.
+ # https://mywiki.wooledge.org/BashFAQ/061
compopt +o nospace
fi
@@ -205,7 +215,7 @@ _gnuastro_asttable_completions(){
>>> word: '$word' -- \$2: '$2'
>>> fits_name: '$fits_name'
EOF
- printf ">>> line: ${COMP_LINE[@]}"
+ printf ">>> line: %s" "$COMP_LINE"
fi
}
- [gnuastro-commits] master 6fe9cb6 20/62: Table: Completion, refactor awk regex fits name, (continued)
- [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
- [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 <=
- [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, 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