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
6
navi
6
navi
|
@ -11,11 +11,11 @@ source "${SCRIPT_DIR}/src/main.sh"
|
||||||
##? cheats [options]
|
##? cheats [options]
|
||||||
##?
|
##?
|
||||||
##? Options:
|
##? Options:
|
||||||
##? --print Prevent script execution [default: false]
|
##? --print Prevent script execution [default: false]
|
||||||
##? --no-interpolation Prevent argument interpolation [default: false]
|
##? --no-interpolation Prevent argument interpolation [default: false]
|
||||||
|
##? --no-preview Hide command preview window [default: false]
|
||||||
|
|
||||||
VERSION="0.6.1"
|
VERSION="0.6.1"
|
||||||
docs::eval "$@"
|
docs::eval "$@"
|
||||||
|
|
||||||
health::fzf
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
|
|
@ -27,7 +27,10 @@ cheat::from_selection() {
|
||||||
for cheat in $cheats; do
|
for cheat in $cheats; do
|
||||||
if grep -q "% $tags" "$cheat"; then
|
if grep -q "% $tags" "$cheat"; then
|
||||||
echo "$cheat"
|
echo "$cheat"
|
||||||
break
|
exit 0
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
echoerr "No valid cheatsheet!"
|
||||||
|
exit 67
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,12 +9,15 @@ docs::extract_help() {
|
||||||
docs::eval() {
|
docs::eval() {
|
||||||
local wait_for=""
|
local wait_for=""
|
||||||
|
|
||||||
|
entry_point="main"
|
||||||
print=false
|
print=false
|
||||||
interpolation=true
|
interpolation=true
|
||||||
|
preview=true
|
||||||
|
|
||||||
for arg in $@; do
|
for arg in $@; do
|
||||||
case $wait_for in
|
case $wait_for in
|
||||||
dir) NAVI_DIR="$arg"; wait_for="" ;;
|
dir) NAVI_DIR="$arg"; wait_for="" ;;
|
||||||
|
command-for) query="$(echo "$arg" | tr "^" " ")"; entry_point="preview"; break ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
case $arg in
|
case $arg in
|
||||||
|
@ -22,6 +25,8 @@ docs::eval() {
|
||||||
--no-interpolation) interpolation=false ;;
|
--no-interpolation) interpolation=false ;;
|
||||||
--version) echo "${VERSION:-unknown}" && exit 0 ;;
|
--version) echo "${VERSION:-unknown}" && exit 0 ;;
|
||||||
--help) docs::extract_help "$0" && exit 0 ;;
|
--help) docs::extract_help "$0" && exit 0 ;;
|
||||||
|
--command-for) wait_for="command-for" ;;
|
||||||
|
--no-preview) preview=false ;;
|
||||||
-d|--dir) wait_for="dir" ;;
|
-d|--dir) wait_for="dir" ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
18
src/main.sh
18
src/main.sh
|
@ -1,5 +1,4 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
source "${SCRIPT_DIR}/src/arg.sh"
|
source "${SCRIPT_DIR}/src/arg.sh"
|
||||||
source "${SCRIPT_DIR}/src/cheat.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/str.sh"
|
||||||
source "${SCRIPT_DIR}/src/ui.sh"
|
source "${SCRIPT_DIR}/src/ui.sh"
|
||||||
|
|
||||||
main() {
|
handler::main() {
|
||||||
local readonly cheats="$(cheat::find)"
|
local readonly cheats="$(cheat::find)"
|
||||||
local readonly selection="$(ui::select "$cheats")"
|
local readonly selection="$(ui::select "$cheats")"
|
||||||
local readonly cheat="$(cheat::from_selection "$cheats" "$selection")"
|
local readonly cheat="$(cheat::from_selection "$cheats" "$selection")"
|
||||||
|
[ -z "$cheat" ] && exit 67
|
||||||
local cmd="$(selection::command "$selection" "$cheat")"
|
local cmd="$(selection::command "$selection" "$cheat")"
|
||||||
local arg value
|
local arg value
|
||||||
|
|
||||||
|
@ -41,3 +41,17 @@ main() {
|
||||||
eval "$cmd"
|
eval "$cmd"
|
||||||
fi
|
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() {
|
ui::select() {
|
||||||
local readonly cheats="$1"
|
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" \
|
echo "$cheats" \
|
||||||
| cheat::read_many \
|
| cheat::read_many \
|
||||||
| ui::pick -i --ansi \
|
| ui::pick "${args[@]}" \
|
||||||
| selection::standardize
|
| selection::standardize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue