mirror of
https://github.com/tonydamage/nux-env.git
synced 2025-12-11 13:24:28 +01:00
nux.repl: Improved autocompletion feature
Added: - autocompletion automaticly proposes tasks - autocompletion for debug, trace, help tasks - extension points for custom task specific autocompleters.
This commit is contained in:
parent
0888955756
commit
4481ca2c56
3 changed files with 108 additions and 0 deletions
|
|
@ -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() {
|
||||
:
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue