Add possibility to pass argument values (#110)

If `navi best github` prompts for `user`, then `navi best github foo` will consider `foo` as `user`.
This commit is contained in:
Denis Isidoro 2019-10-04 18:13:11 -03:00 committed by GitHub
parent 0941166c9c
commit b7c5fb8fea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 18 deletions

14
navi
View file

@ -11,14 +11,15 @@ source "${SCRIPT_DIR}/src/main.sh"
##? navi [command] [<args>...] [options]
##?
##? Commands:
##? search <cmd> Search for cheatsheets on online repositories
##? query <cmd> Pre-filter results
##? search <cmd> Search for cheatsheets on online repositories
##? query <cmd> Pre-filter results
##? best <cmd> <args>... Considers the best match
##?
##? Options:
##? --print Prevent script execution
##? --path List of paths to look for cheats
##? --no-interpolation Prevent argument interpolation
##? --no-preview Hide command preview window
##? --print Prevent script execution
##? --path List of paths to look for cheats
##? --no-interpolation Prevent argument interpolation
##? --no-preview Hide command preview window
##?
##? Examples:
##? navi
@ -26,6 +27,7 @@ source "${SCRIPT_DIR}/src/main.sh"
##? navi search awk
##? navi search docker --print
##? navi query git
##? navi best 'sql create db' root mydb
##?
##? More info:
##? search

View file

@ -38,8 +38,16 @@ coll::remove() {
done
}
coll::_without_empty_line() {
local -r input="$(cat)"
local -r words="$(echo "$input" | wc -w | xargs)"
if [[ $words > 0 ]]; then
echo "$input"
fi
}
coll::add() {
cat
cat | coll::_without_empty_line
for x in "$@"; do
echo "$x"
done
@ -53,6 +61,12 @@ coll::set() {
sort -u
}
coll::get() {
local n="$1"
n=$((n+1))
sed "${n}q;d"
}
# TODO: implement tailrec
coll::reduce() {
local -r fn="$1"

View file

@ -32,6 +32,9 @@ handler::main() {
local cmd="$(selection::cmd "$selection" "$cheat")"
local arg value
local -r args="$(dict::get "$OPTIONS" args)"
local i=0
while $interpolation; do
arg="$(echo "$cmd" | arg::next || echo "")"
if [ -z "$arg" ]; then
@ -45,7 +48,9 @@ handler::main() {
cmd="$(echo "$cmd" | sed "s|<${arg}>|<${escaped_arg}>|g")"
arg="$escaped_arg"
value="$(arg::pick "$arg" "$cheat" || echo "")"
value="$(echo "$args" | coll::get $i)"
[ -z "$value" ] && value="$(arg::pick "$arg" "$cheat")"
if [ -z "$value" ]; then
echoerr "Unable to fetch suggestions for '$arg'!"
@ -54,6 +59,8 @@ handler::main() {
eval "local $arg"='$value'
cmd="$(echo "$cmd" | arg::interpolate "$arg" "$value")"
i=$((i+1))
done
local -r unresolved_arg="$(echo "$cmd" | arg::next || echo "")"

View file

@ -16,6 +16,7 @@ opts::eval() {
local autoselect=true
local best=false
local query=""
local args=""
case "${1:-}" in
--version|version) entry_point="version"; shift ;;
@ -30,15 +31,13 @@ opts::eval() {
widget) entry_point="widget"; shift; wait_for="widget" ;;
esac
i=0
for arg in "$@"; do
case $wait_for in
path) path="$arg"; wait_for="" ;;
preview) query="$(arg::deserialize "$arg")"; wait_for="" ;;
search) query="$arg"; wait_for=""; path="${path}:$(search::full_path "$query")"; ;;
query|best) query="$arg"; wait_for="" ;;
widget) SH="$arg"; wait_for="" ;;
path) path="$arg"; wait_for=""; continue ;;
preview) query="$(arg::deserialize "$arg")"; wait_for=""; continue ;;
search) query="$arg"; wait_for=""; path="${path}:$(search::full_path "$query")"; continue ;;
query|best) query="$arg"; wait_for=""; continue ;;
widget) SH="$arg"; wait_for=""; continue ;;
esac
case $arg in
@ -47,9 +46,8 @@ opts::eval() {
--no-preview) preview=false ;;
--path|--dir) wait_for="path" ;;
--no-autoselect) autoselect=false ;;
*) args="$(echo "$args" | coll::add "$arg")" ;;
esac
i=$((i+1))
done
OPTIONS="$(dict::new \
@ -59,7 +57,8 @@ opts::eval() {
preview "$preview" \
autoselect "$autoselect" \
query "$query" \
best "$best")"
best "$best" \
args "$args")"
export NAVI_PATH="$path"
}

View file

@ -45,3 +45,13 @@ str::reverse_lines() {
awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }'
fi
}
str::not_empty() {
local -r input="$(cat)"
if [ -n $input ]; then
echo "$input"
else
return 1
fi
}