mirror of
https://github.com/denisidoro/navi
synced 2024-11-22 03:23:05 +00:00
parent
c8764ae224
commit
d42d192bdd
5 changed files with 39 additions and 7 deletions
2
navi
2
navi
|
@ -13,9 +13,9 @@ source "${SCRIPT_DIR}/src/main.sh"
|
|||
##? Options:
|
||||
##? --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 "$@"
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
18
src/main.sh
18
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
|
||||
}
|
12
src/ui.sh
12
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue