From 4481ca2c567b06f56f6c1e7142d8f612dddf1003 Mon Sep 17 00:00:00 2001 From: Tony Tkacik Date: Wed, 21 Jun 2017 15:09:08 +0200 Subject: [PATCH] nux.repl: Improved autocompletion feature Added: - autocompletion automaticly proposes tasks - autocompletion for debug, trace, help tasks - extension points for custom task specific autocompleters. --- bin/nux-runner | 6 +++ inc/nux-runner.inc.sh | 101 ++++++++++++++++++++++++++++++++++++++++++ inc/nux.repl.inc.sh | 1 + 3 files changed, 108 insertions(+) diff --git a/bin/nux-runner b/bin/nux-runner index 7eb2dba..65c635a 100755 --- a/bin/nux-runner +++ b/bin/nux-runner @@ -104,6 +104,12 @@ task.() { task.help } + +## interactive:: +## Executes interactive application shell. +task.interactive() { + nuxr.task.interactive "$@" +} ### ### diff --git a/inc/nux-runner.inc.sh b/inc/nux-runner.inc.sh index a9e762a..1808c0e 100644 --- a/inc/nux-runner.inc.sh +++ b/inc/nux-runner.inc.sh @@ -51,3 +51,104 @@ function nuxr.help.task.comment { return -1 fi } + +## +## nuxr.interactive:: +## Runs an interactive taskie shell with base taskie commands available. +## +nuxr.task.interactive() { + nux.use nux.repl + .process() { + backendFunc=task.$command; + if nux.check.function $backendFunc; then + + eval nuxr.run "$command" "$arguments" + else + echo "$command" is not defined. + fi + } + + .prompt() { + echo "${nc_green}$NUX_APPNAME${nc_end}> " + } + nux.repl.start .process .prompt nuxr.repl.completer +} + +nuxr.tasks.runtime.search() { + set | grep -G "^task\.$1.* ()" \ + | cut -d "." -f2- \ + | cut -d"(" -f1 +} + +nuxr.repl.completer.help() { + nux.log debug "Help completer" + nux.log debug "current_pos $current_pos" + nux.log debug "current word $current_word" + if [ $current_pos -eq 2 ]; then + nuxr.tasks.runtime.search $current_word | grep -v "help." + nuxr.tasks.runtime.search help.$current_word | cut -d"." -f2 + fi +} + +nuxr.repl.completer._prefix_task() { + nux.log debug "Prefix completer. $current_pos $current_word" + if [ $current_pos -eq 2 ]; then + nuxr.tasks.runtime.search $current_word + else + nuxr.repl.completer "${line#$command }" + fi +} + +nuxr.repl.completer() { + local line=$1; + nux.log debug "Requested completion for " "'$line'" + + local words=($line) + local current_pos=${#words[@]}; + if [ "$current_pos" -eq 0 ]; then + nuxr.tasks.runtime.search | sort | uniq + return + fi + + local current_word=${words[${#words[@]}-1]}; + if [ "$line" != "${line%% }" ] ; then + nux.log debug "Creating proposal for next word." + let current_pos=current_pos+1 + current_word="" + fi + local result=""; + if [ $current_pos -eq 1 ] ; then + result=$(nuxr.tasks.runtime.search $current_word) + elif [ $current_pos -ge 2 ]; then + command=${words[0]} + nux.log debug "Trying to use completer for '$command'" + case $command in + debug) ;& + trace) + result=$(nuxr.repl.completer._prefix_task) + ;; + *) + result=$(nux.exec.optional nuxr.repl.completer.$command) + ;; + esac; + fi + + if [ -n "$result" ]; then + nux.log debug "Completion found." + echo $result + else + nux.log debug "No completion found." + printf '\a' >> $(tty) + if [ $current_pos -gt 1 ]; then + echo $current_word + fi + fi +} + +nux.app() { + echo $NUX_APPNAME +} + +nux.app.dir() { + : +} diff --git a/inc/nux.repl.inc.sh b/inc/nux.repl.inc.sh index 108981f..3974931 100644 --- a/inc/nux.repl.inc.sh +++ b/inc/nux.repl.inc.sh @@ -76,6 +76,7 @@ function nux.repl.completion() { complete_command=$(complete -p | grep " ${program_name}$") COMPREPLY=() + COMPREPLY=($($compgen_command "$completion_line")) # get commmon prefix of available completions