Add preview window (#28)

Fix #21
This commit is contained in:
Denis Isidoro 2019-09-21 18:55:35 -03:00 committed by GitHub
parent c8764ae224
commit d42d192bdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 7 deletions

6
navi
View File

@ -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 "$@"

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}