From b7c5fb8feadad9ce97088cc89f672e884ec8a8b6 Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Fri, 4 Oct 2019 18:13:11 -0300 Subject: [PATCH] Add possibility to pass argument values (#110) If `navi best github` prompts for `user`, then `navi best github foo` will consider `foo` as `user`. --- navi | 14 ++++++++------ src/coll.sh | 16 +++++++++++++++- src/main.sh | 9 ++++++++- src/opts.sh | 19 +++++++++---------- src/str.sh | 10 ++++++++++ 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/navi b/navi index d367d76..ec77421 100755 --- a/navi +++ b/navi @@ -11,14 +11,15 @@ source "${SCRIPT_DIR}/src/main.sh" ##? navi [command] [...] [options] ##? ##? Commands: -##? search Search for cheatsheets on online repositories -##? query Pre-filter results +##? search Search for cheatsheets on online repositories +##? query Pre-filter results +##? best ... 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 diff --git a/src/coll.sh b/src/coll.sh index 2240e17..58240eb 100644 --- a/src/coll.sh +++ b/src/coll.sh @@ -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" diff --git a/src/main.sh b/src/main.sh index 23d86cc..620558a 100644 --- a/src/main.sh +++ b/src/main.sh @@ -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 "")" diff --git a/src/opts.sh b/src/opts.sh index 96441cf..e1cba28 100644 --- a/src/opts.sh +++ b/src/opts.sh @@ -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" } diff --git a/src/str.sh b/src/str.sh index 3d79273..e7e9a4f 100644 --- a/src/str.sh +++ b/src/str.sh @@ -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 +} \ No newline at end of file