diff --git a/navi b/navi index 99c8a05..9bd591a 100755 --- a/navi +++ b/navi @@ -11,11 +11,11 @@ source "${SCRIPT_DIR}/src/main.sh" ##? cheats [options] ##? ##? Options: -##? --print Prevent script execution [default: false] -##? --no-interpolation Prevent argument interpolation [default: false] +##? --print Prevent script execution [default: false] +##? --no-interpolation Prevent argument interpolation [default: false] +##? --no-preview Hide command preview window [default: false] VERSION="0.6.1" docs::eval "$@" -health::fzf main "$@" diff --git a/src/cheat.sh b/src/cheat.sh index c62185d..8fb0888 100755 --- a/src/cheat.sh +++ b/src/cheat.sh @@ -27,7 +27,10 @@ cheat::from_selection() { for cheat in $cheats; do if grep -q "% $tags" "$cheat"; then echo "$cheat" - break + exit 0 fi done + + echoerr "No valid cheatsheet!" + exit 67 } diff --git a/src/docs.sh b/src/docs.sh index a72117e..bb3bf4f 100644 --- a/src/docs.sh +++ b/src/docs.sh @@ -9,12 +9,15 @@ docs::extract_help() { docs::eval() { local wait_for="" + entry_point="main" print=false interpolation=true + preview=true for arg in $@; do case $wait_for in dir) NAVI_DIR="$arg"; wait_for="" ;; + command-for) query="$(echo "$arg" | tr "^" " ")"; entry_point="preview"; break ;; esac case $arg in @@ -22,6 +25,8 @@ docs::eval() { --no-interpolation) interpolation=false ;; --version) echo "${VERSION:-unknown}" && exit 0 ;; --help) docs::extract_help "$0" && exit 0 ;; + --command-for) wait_for="command-for" ;; + --no-preview) preview=false ;; -d|--dir) wait_for="dir" ;; esac done diff --git a/src/main.sh b/src/main.sh index 01cb9b9..5bbae81 100644 --- a/src/main.sh +++ b/src/main.sh @@ -1,5 +1,4 @@ #!/usr/bin/env bash -set -euo pipefail source "${SCRIPT_DIR}/src/arg.sh" source "${SCRIPT_DIR}/src/cheat.sh" @@ -10,10 +9,11 @@ source "${SCRIPT_DIR}/src/selection.sh" source "${SCRIPT_DIR}/src/str.sh" source "${SCRIPT_DIR}/src/ui.sh" -main() { +handler::main() { local readonly cheats="$(cheat::find)" local readonly selection="$(ui::select "$cheats")" local readonly cheat="$(cheat::from_selection "$cheats" "$selection")" + [ -z "$cheat" ] && exit 67 local cmd="$(selection::command "$selection" "$cheat")" local arg value @@ -41,3 +41,17 @@ main() { eval "$cmd" fi } + +handler::preview() { + local readonly selection="$(echo "$query" | selection::standardize)" + local readonly cheats="$(cheat::find)" + local readonly cheat="$(cheat::from_selection "$cheats" "$selection")" + [ -n "$cheat" ] && selection::command "$selection" "$cheat" +} + +main() { + case ${entry_point:-} in + preview) handler::preview "$@" 2>/dev/null || echo "Unable to find command for '${query:-}'";; + *) health::fzf && handler::main "$@" ;; + esac +} \ No newline at end of file diff --git a/src/ui.sh b/src/ui.sh index 7d0091d..a41cb85 100644 --- a/src/ui.sh +++ b/src/ui.sh @@ -6,10 +6,20 @@ ui::pick() { ui::select() { local readonly cheats="$1" + local readonly script_path="$(which navi | head -n1 || echo "${SCRIPT_DIR}/navi")" + local readonly preview_cmd="echo {} | tr ' ' '^' | xargs -I% \"${script_path}\" --command-for %" + + local args=() + args+=("-i") + args+=("--ansi") + if $preview; then + args+=("--preview"); args+=("$preview_cmd") + args+=("--preview-window"); args+=("up:1") + fi echo "$cheats" \ | cheat::read_many \ - | ui::pick -i --ansi \ + | ui::pick "${args[@]}" \ | selection::standardize }