mirror of
https://github.com/denisidoro/navi
synced 2024-11-22 03:23:05 +00:00
Fix local readonly (#42)
Based on https://github.com/denisidoro/navi/pull/12 by @qubitrenegade
This commit is contained in:
parent
b3f8e0b374
commit
a21f39ee64
8 changed files with 40 additions and 40 deletions
18
src/arg.sh
18
src/arg.sh
|
@ -11,8 +11,8 @@ arg::opts() {
|
|||
}
|
||||
|
||||
arg::interpolate() {
|
||||
local readonly arg="$1"
|
||||
local readonly value="$2"
|
||||
local -r arg="$1"
|
||||
local -r value="$2"
|
||||
|
||||
sed "s|<${arg}>|\"${value}\"|g"
|
||||
}
|
||||
|
@ -25,15 +25,15 @@ arg::next() {
|
|||
}
|
||||
|
||||
arg::pick() {
|
||||
local readonly arg="$1"
|
||||
local readonly cheat="$2"
|
||||
local -r arg="$1"
|
||||
local -r cheat="$2"
|
||||
|
||||
local readonly prefix="$ ${arg}:"
|
||||
local readonly length="$(echo "$prefix" | str::length)"
|
||||
local readonly arg_description="$(grep "$prefix" "$cheat" | str::sub $((length + 1)))"
|
||||
local -r prefix="$ ${arg}:"
|
||||
local -r length="$(echo "$prefix" | str::length)"
|
||||
local -r arg_description="$(grep "$prefix" "$cheat" | str::sub $((length + 1)))"
|
||||
|
||||
local readonly fn="$(echo "$arg_description" | arg::fn)"
|
||||
local readonly args_str="$(echo "$arg_description" | arg::opts | tr ' ' '\n' || echo "")"
|
||||
local -r fn="$(echo "$arg_description" | arg::fn)"
|
||||
local -r args_str="$(echo "$arg_description" | arg::opts | tr ' ' '\n' || echo "")"
|
||||
local arg_name=""
|
||||
|
||||
for arg_str in $args_str; do
|
||||
|
|
|
@ -21,10 +21,10 @@ cheat::read_many() {
|
|||
}
|
||||
|
||||
cheat::from_selection() {
|
||||
local readonly cheats="$1"
|
||||
local readonly selection="$2"
|
||||
local -r cheats="$1"
|
||||
local -r selection="$2"
|
||||
|
||||
local readonly tags="$(echo "$selection" | selection::tags)"
|
||||
local -r tags="$(echo "$selection" | selection::tags)"
|
||||
|
||||
for cheat in $cheats; do
|
||||
if grep -q "% $tags" "$cheat"; then
|
||||
|
|
14
src/main.sh
14
src/main.sh
|
@ -11,9 +11,9 @@ source "${SCRIPT_DIR}/src/str.sh"
|
|||
source "${SCRIPT_DIR}/src/ui.sh"
|
||||
|
||||
handler::main() {
|
||||
local readonly cheats="$(cheat::find)"
|
||||
local readonly selection="$(ui::select "$cheats")"
|
||||
local readonly cheat="$(cheat::from_selection "$cheats" "$selection")"
|
||||
local -r cheats="$(cheat::find)"
|
||||
local -r selection="$(ui::select "$cheats")"
|
||||
local -r cheat="$(cheat::from_selection "$cheats" "$selection")"
|
||||
[ -z "$cheat" ] && exit 67
|
||||
local cmd="$(selection::command "$selection" "$cheat")"
|
||||
local arg value
|
||||
|
@ -34,7 +34,7 @@ handler::main() {
|
|||
cmd="$(echo "$cmd" | arg::interpolate "$arg" "$value")"
|
||||
done
|
||||
|
||||
local readonly unresolved_arg="$(echo "$cmd" | arg::next || echo "")"
|
||||
local -r unresolved_arg="$(echo "$cmd" | arg::next || echo "")"
|
||||
|
||||
if $print || [ -n "$unresolved_arg" ]; then
|
||||
echo "$cmd"
|
||||
|
@ -44,9 +44,9 @@ handler::main() {
|
|||
}
|
||||
|
||||
handler::preview() {
|
||||
local readonly selection="$(echo "$query" | selection::standardize)"
|
||||
local readonly cheats="$(cheat::find)"
|
||||
local readonly cheat="$(cheat::from_selection "$cheats" "$selection")"
|
||||
local -r selection="$(echo "$query" | selection::standardize)"
|
||||
local -r cheats="$(cheat::find)"
|
||||
local -r cheat="$(cheat::from_selection "$cheats" "$selection")"
|
||||
[ -n "$cheat" ] && selection::command "$selection" "$cheat"
|
||||
}
|
||||
|
||||
|
|
|
@ -2,12 +2,12 @@
|
|||
set -euo pipefail
|
||||
|
||||
opts::extract_help() {
|
||||
local readonly file="$1"
|
||||
local -r file="$1"
|
||||
grep "^##?" "$file" | cut -c 5-
|
||||
}
|
||||
|
||||
opts::preview_hack() {
|
||||
local readonly arg="$1"
|
||||
local -r arg="$1"
|
||||
|
||||
if [ ${arg:0:1} = "'" ]; then
|
||||
echo "${arg:1:${#arg}-2}"
|
||||
|
@ -17,7 +17,7 @@ opts::preview_hack() {
|
|||
}
|
||||
|
||||
opts::eval() {
|
||||
local readonly wait_for=""
|
||||
local -r wait_for=""
|
||||
|
||||
entry_point="main"
|
||||
print=false
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
search::cheat() {
|
||||
local readonly cmd="$(echo "$1" | str::first_word)"
|
||||
local -r cmd="$(echo "$1" | str::first_word)"
|
||||
|
||||
echo "% ${cmd}, cheatsh"
|
||||
echo
|
||||
|
@ -9,7 +9,7 @@ search::cheat() {
|
|||
}
|
||||
|
||||
search::filename() {
|
||||
local readonly cmd="$(echo "$1" | str::first_word)"
|
||||
local -r cmd="$(echo "$1" | str::first_word)"
|
||||
|
||||
echo "${cmd}_cheatsh" \
|
||||
| head -n1 \
|
||||
|
@ -19,16 +19,16 @@ search::filename() {
|
|||
}
|
||||
|
||||
search::full_path() {
|
||||
local readonly cmd="$(echo "$1" | str::first_word)"
|
||||
local -r cmd="$(echo "$1" | str::first_word)"
|
||||
|
||||
echo "/tmp/navi/$(search::filename "$cmd").cheat"
|
||||
}
|
||||
|
||||
search::save() {
|
||||
local readonly cmd="$(echo "$1" | str::first_word)"
|
||||
local -r cmd="$(echo "$1" | str::first_word)"
|
||||
|
||||
local readonly filepath="$(search::full_path "$cmd")"
|
||||
local readonly filedir="$(dirname "$filepath")"
|
||||
local -r filepath="$(search::full_path "$cmd")"
|
||||
local -r filedir="$(dirname "$filepath")"
|
||||
|
||||
if [ -f "$filepath" ]; then
|
||||
return
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
selection::standardize() {
|
||||
local readonly str="$(cat)"
|
||||
local -r str="$(cat)"
|
||||
|
||||
local readonly tags="$(echo "$str" | awk -F'[' '{print $NF}' | tr -d ']')"
|
||||
local readonly core="$(echo "$str" | sed -e "s/ \[${tags}\]$//")"
|
||||
local -r tags="$(echo "$str" | awk -F'[' '{print $NF}' | tr -d ']')"
|
||||
local -r core="$(echo "$str" | sed -e "s/ \[${tags}\]$//")"
|
||||
|
||||
echo "${core}^${tags}"
|
||||
}
|
||||
|
@ -22,10 +22,10 @@ selection::core_is_comment() {
|
|||
}
|
||||
|
||||
selection::command() {
|
||||
local readonly selection="$1"
|
||||
local readonly cheat="$2"
|
||||
local -r selection="$1"
|
||||
local -r cheat="$2"
|
||||
|
||||
local readonly core="$(echo $selection | selection::core)"
|
||||
local -r core="$(echo $selection | selection::core)"
|
||||
|
||||
if echo "$core" | selection::core_is_comment; then
|
||||
grep "$core" "$cheat" -A999 \
|
||||
|
|
|
@ -5,14 +5,14 @@ str::length() {
|
|||
}
|
||||
|
||||
str::sub() {
|
||||
local readonly start="${1:-0}"
|
||||
local readonly finish="${2:-99999}"
|
||||
local -r start="${1:-0}"
|
||||
local -r finish="${2:-99999}"
|
||||
|
||||
cut -c "$((start + 1))-$((finish - 1))"
|
||||
}
|
||||
|
||||
str::column() {
|
||||
local readonly n="${1:-}"
|
||||
local -r n="${1:-}"
|
||||
|
||||
if [ -n "$n" ]; then
|
||||
awk "{print \$$n}"
|
||||
|
|
|
@ -5,9 +5,9 @@ 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}\" preview %"
|
||||
local -r cheats="$1"
|
||||
local -r script_path="$(which navi | head -n1 || echo "${SCRIPT_DIR}/navi")"
|
||||
local -r preview_cmd="echo \"{}\" | tr ' ' '^' | xargs -I% \"${script_path}\" preview %"
|
||||
|
||||
local args=()
|
||||
args+=("-i")
|
||||
|
|
Loading…
Reference in a new issue