Skip to content

Commit

Permalink
Only cache .index when $BASH_VERSION is 4+.
Browse files Browse the repository at this point in the history
refs gh-57
  • Loading branch information
xwmx committed Oct 24, 2020
1 parent 1087a8b commit ef963d8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
34 changes: 27 additions & 7 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -6291,18 +6291,28 @@ HEREDOC

return 0
else
local _bash_major_version="${BASH_VERSION%%.*}"
local _counter=0
local _matches=0
local _max_id
_max_id="$(_index get_max_id)"

if ! ((_no_id))
then
if ! ((_no_id)) && [[ "${_bash_major_version:-0}" -ge 4 ]]
then # cache .index
declare -A _index_map
__i=0
while IFS= read -r line; do
__i=$((++__i))
_index_map[$line]=$__i

local _id=0

while IFS= read -r _line
do
_id=$((_id+1))

if [[ -n "${_line:-}" ]]
then
_index_map["${_line}"]="${_id}"
else
_index_map["___x_nb_blank_x___"]="${_id}"
fi
done < "${_notebook_path:-}/.index"
fi

Expand Down Expand Up @@ -6451,7 +6461,17 @@ HEREDOC
_info_line="${_item_info}"
_info_line_color="${_item_info}"
else
local _item_identifier=${_index_map[${__basename:-}]}
if [[ "${_bash_major_version:-0}" -ge 4 ]]
then # get from cache map
local _item_identifier="${_index_map["${__basename:-}"]}"
else # get from .index
local _item_identifier="$(
sed -n "/^${__basename:-}$/=" "${_notebook_path:-}/.index"
)"
fi



local _max_identifier="${_max_id}"

if [[ -n "${_maybe_scope:-}" ]]
Expand Down
31 changes: 31 additions & 0 deletions test/list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,37 @@ Help information:
[[ "${lines[2]}" =~ 🔖 ]]
}

@test "\`list\` includes ids." {
{
"${_NB}" init
"${_NB}" add "one.md" --title "one"
"${_NB}" add "two.md" --title "two"
"${_NB}" add "three.md" --title "three"
"${_NB}" add "four.md" --title "four"
"${_NB}" add "five.md" --title "five"

run "${_NB}" delete "one.md" --force
run "${_NB}" delete "four.md" --force

_files=($(ls "${NB_NOTEBOOK_PATH}/"))
}

run "${_NB}" list

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
_compare "${_files[@]}" "${lines[@]}"

[[ ${status} -eq 0 ]]
[[ "${lines[0]}" =~ five ]]
[[ "${lines[0]}" =~ 5 ]]
[[ "${lines[1]}" =~ three ]]
[[ "${lines[1]}" =~ 3 ]]
[[ "${lines[2]}" =~ two ]]
[[ "${lines[2]}" =~ 2 ]]
[[ -z "${lines[3]:-}" ]]
}

# `list --no-id` ##############################################################

@test "\`list --no-id\` exits with 0 and lists files in reverse order." {
Expand Down

0 comments on commit ef963d8

Please sign in to comment.