Skip to content

Commit

Permalink
Add possibility to pass argument values (denisidoro#110)
Browse files Browse the repository at this point in the history
If `navi best github` prompts for `user`, then `navi best github foo` will consider `foo` as `user`.
  • Loading branch information
denisidoro authored Oct 4, 2019
1 parent 0941166 commit b7c5fb8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
14 changes: 8 additions & 6 deletions navi
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ 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
##? navi --path '/some/folder:/another/folder'
##? navi search awk
##? navi search docker --print
##? navi query git
##? navi best 'sql create db' root mydb
##?
##? More info:
##? search
Expand Down
16 changes: 15 additions & 1 deletion src/coll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'!"
Expand All @@ -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 "")"
Expand Down
19 changes: 9 additions & 10 deletions src/opts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;;
Expand All @@ -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
Expand All @@ -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 \
Expand All @@ -59,7 +57,8 @@ opts::eval() {
preview "$preview" \
autoselect "$autoselect" \
query "$query" \
best "$best")"
best "$best" \
args "$args")"

export NAVI_PATH="$path"
}
10 changes: 10 additions & 0 deletions src/str.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit b7c5fb8

Please sign in to comment.