Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix local readonly #42

Merged
merged 2 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Replace readonly by -r
  • Loading branch information
denisidoro committed Sep 23, 2019
commit 86a99f41b179096009415c1feb65aa748f0edc99
18 changes: 9 additions & 9 deletions src/arg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/cheat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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"
}

Expand Down
6 changes: 3 additions & 3 deletions src/opts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -17,7 +17,7 @@ opts::preview_hack() {
}

opts::eval() {
local readonly wait_for=""
local -r wait_for=""

entry_point="main"
print=false
Expand Down
12 changes: 6 additions & 6 deletions src/search.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/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
curl -s "${CHTSH_URL:-http://cht.sh}/${cmd}?T"
}

search::filename() {
local readonly cmd="$(echo "$1" | str::first_word)"
local -r cmd="$(echo "$1" | str::first_word)"

echo "${cmd}_cheatsh" \
| head -n1 \
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/selection.sh
Original file line number Diff line number Diff line change
@@ -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}"
}
Expand All @@ -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 \
Expand Down
6 changes: 3 additions & 3 deletions src/ui.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down