gnuastro-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnuastro-commits] master 1c1fe3d 01/62: New feature: Auto Complete (WIP


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 1c1fe3d 01/62: New feature: Auto Complete (WIP)
Date: Thu, 13 May 2021 22:20:44 -0400 (EDT)

branch: master
commit 1c1fe3d006a5257b31f2a44ea75c99d6f84355b8
Author: Pedram Ashofteh Ardakani <pedramardakani@pm.me>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>

    New feature: Auto Complete (WIP)
    
    Until now, the user did not have the convenience of using auto
    completion in Gnuastro. With this commit, user can push the <TAB>
    character in the middle of typing and select the appropriate option
    or command.
    
    This commit is a work in progress (hence the WIP in the subject). For
    now, the feature is only available in `astcosmiccal`. But we are
    planning to expand it to all Gnuastro subprograms in near future.
---
 bin/cosmiccal/astcosmiccal-completion.bash | 42 ++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/bin/cosmiccal/astcosmiccal-completion.bash 
b/bin/cosmiccal/astcosmiccal-completion.bash
new file mode 100644
index 0000000..8e81306
--- /dev/null
+++ b/bin/cosmiccal/astcosmiccal-completion.bash
@@ -0,0 +1,42 @@
+#/usr/bin/env bash
+
+# TIP: Run the command below to initialize the bash completion feature for
+# this specific program (i.e. astcosmiccal):
+# $ source astcosmiccal-completion.bash
+
+_astcosmiccal_completions(){
+
+    # PATH_TO_COMMAND_BIN, i.e. the directory which the astcosmiccal
+    # command sits in.
+    local PATH_TO_COMMAND_BIN=".";
+
+    # Use "args.h" file to filter out all valid options of the command and
+    # put them all in the variable "commandoptions".
+
+    local commandoptions=$(sed -n -E '/^\s+\"[a-z]+/p' \
+                               "$PATH_TO_COMMAND_BIN/args.h" | \
+                               # remove newline and space characters
+                               tr -d '\n" ' | \
+                               # replace commas with spaces
+                               tr ',' ' ');
+
+    # Initialize the completion response with null
+    COMPREPLY=();
+
+    # Variable "word", is the current word being completed
+    local word="${COMP_WORDS[COMP_CWORD]}";
+
+    # Variable "prev" is the word just before the current word
+    local prev="${COMP_WORDS[COMP_CWORD-1]}";
+
+    if [ $prev = "lineatz" ]; then
+        # Show options related to "lineatz"
+        COMPREPLY=($(compgen -W "$(astcosmiccal --listlines | \
+                           awk '!/^#/ {print $2}') " \
+                           -- "$word"));
+    else
+        COMPREPLY=($(compgen -W "$commandoptions" -- "$word"));
+    fi;
+}
+
+complete -F _astcosmiccal_completions astcosmiccal



reply via email to

[Prev in Thread] Current Thread [Next in Thread]