From bc9bbbecdc9451afa52eea431b21ed187c7ead9e Mon Sep 17 00:00:00 2001 From: William Melody Date: Wed, 30 Sep 2020 10:20:30 -0700 Subject: [PATCH 01/12] Stream data through _list() Introduce an alternative streaming version ot _list(), piping the list from _list_files() and implementing local counters within the while loop to count the number of items. The existing _list() implementation loads the filenames from _list_files() and then loops over that list. This streaming version is currently much less performant on large notebooks than the existing _list() implementation. --- nb | 514 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 263 insertions(+), 251 deletions(-) diff --git a/nb b/nb index 03e9ec226..b83331bd6 100755 --- a/nb +++ b/nb @@ -1245,6 +1245,11 @@ _git_should_autosync() { # Assign newline with ANSI-C quoting for string building. _NEWLINE=$'\n' +# $_NO_LIMIT +# +# A value for math operations to represent no limit. +_NO_LIMIT=999999999999999999 + # _alias_subcommand() # # Usage: @@ -5715,327 +5720,334 @@ _list() { ((_reverse)) && _list_files_options+=("--reverse") ((_sort)) && _list_files_options+=("--sort") - _filenames=($(_list_files "${_list_files_options[@]}")) || : - - local _filenames_count="${#_filenames[@]}" - if _notebooks current --selected then _maybe_scope="$(_notebooks show "${_notebook_path}" --escaped --no-color):" _maybe_scope_padded_right="${_maybe_scope} " fi - # Use code block to capture all output for piping to pager. - { - if ! ((_filenames_count)) && [[ -z "${_selector_identifier:-}" ]] - then - if ((_hard_empty)) - then - return 1 - elif [[ "${_type}" =~ ^bookmark$|^bookmarks$ ]] - then - cat <") -Help information: - $(_color_primary "${_ME} help bookmark") -HEREDOC - elif [[ "${_type}" =~ ^archive|^audio|^document|^image|^video ]] - then - _type="$(printf "%s\\n" "${_type}" | sed -e 's/s$//')" + local _filenames_counter=0 + local _printed_counter=0 + local _counter=0 - cat < | )") -Help information: - $(_color_primary "${_ME} help import") -HEREDOC - else - cat <") -Import a file: - $(_color_primary "${_ME} ${_maybe_scope}import ( | )") -Help information: - $(_color_primary "${_ME} help") -HEREDOC + if [[ "${_printed_counter}" -ge "${_limit:-${_NO_LIMIT}}" ]] + then # the limit has been reached. + continue fi - return 0 - else - local _counter=0 - local _max_id - _max_id="$(_index get_max_id)" - - for __basename in "${_filenames[@]:-}" - do - local _maybe_title= + local _maybe_title= - if [[ -n "${_selector_identifier:-}" ]] + if [[ -n "${_selector_identifier:-}" ]] + then + # If a valid has been specified, skip the rest of the + # loop unless the basename matches the current one. + if [[ -n "${_selector_basename}" ]] then - # If a valid has been specified, skip the rest of the - # loop unless the basename matches the current one. - if [[ -n "${_selector_basename}" ]] + if [[ "${__basename}" != "${_selector_basename}" ]] then - if [[ "${__basename}" != "${_selector_basename}" ]] - then - continue - fi + continue + fi + else + # Turn on a case-insensitive matching (-s set nocasematch) + shopt -s nocasematch + + if [[ "${__basename:-}" =~ ${_selector_identifier} ]] + then + : # include the note in the list else - # Turn on a case-insensitive matching (-s set nocasematch) - shopt -s nocasematch + _maybe_title="$(_get_title "${_notebook_path}/${__basename}")" - if [[ "${__basename:-}" =~ ${_selector_identifier} ]] + if [[ "${_maybe_title:-}" =~ ${_selector_identifier} ]] then : # include the note in the list else - _maybe_title="$(_get_title "${_notebook_path}/${__basename}")" - - if [[ "${_maybe_title:-}" =~ ${_selector_identifier} ]] - then - : # include the note in the list - else - continue # don't include note in list - fi + continue # don't include note in list fi - - # Turn off a case-insensitive matching (-u unset nocasematch) - shopt -u nocasematch fi - fi - - if [[ -n "${_limit:-}" ]] && [[ "${_counter}" -eq "${_limit}" ]] - then # the limit has been reached. - printf "%s omitted. %s total.\\n" \ - "$((_filenames_count-_counter))" \ - "${_filenames_count}" - break + # Turn off a case-insensitive matching (-u unset nocasematch) + shopt -u nocasematch fi + fi - local _title_or_basename="${__basename}" + local _title_or_basename="${__basename}" - if ! ((_list_filenames)) + if ! ((_list_filenames)) + then + if [[ -z "${_maybe_title}" ]] then - if [[ -z "${_maybe_title}" ]] - then - _maybe_title="$(_get_title "${_notebook_path}/${__basename}")" - fi + _maybe_title="$(_get_title "${_notebook_path}/${__basename}")" + fi - if [[ -n "${_maybe_title}" ]] - then # note file has title. - _title_or_basename="${_maybe_title}" - fi + if [[ -n "${_maybe_title}" ]] + then # note file has title. + _title_or_basename="${_maybe_title}" fi + fi - local _extra_length=0 - local _indicators= - local _maybe_padded_indicators= + local _extra_length=0 + local _indicators= + local _maybe_padded_indicators= - if ((_print_indicators)) + if ((_print_indicators)) + then + if _file_is_bookmark "${_notebook_path}/${__basename}" then - if _file_is_bookmark "${_notebook_path}/${__basename}" - then - _indicators+="๐Ÿ”– " - elif _file_is_image "${_notebook_path}/${__basename}" - then - _indicators+="๐ŸŒ„ " - elif _file_is_document "${_notebook_path}/${__basename}" - then - _indicators+="๐Ÿ“„ " - elif [[ -d "${_notebook_path}/${__basename}" ]] - then - _indicators+="๐Ÿ“‚ " - elif _file_is_video "${_notebook_path}/${__basename}" - then - _indicators+="๐Ÿ“น " - elif _file_is_audio "${_notebook_path}/${__basename}" - then - _indicators+="๐Ÿ”‰ " - fi + _indicators+="๐Ÿ”– " + elif _file_is_image "${_notebook_path}/${__basename}" + then + _indicators+="๐ŸŒ„ " + elif _file_is_document "${_notebook_path}/${__basename}" + then + _indicators+="๐Ÿ“„ " + elif [[ -d "${_notebook_path}/${__basename}" ]] + then + _indicators+="๐Ÿ“‚ " + elif _file_is_video "${_notebook_path}/${__basename}" + then + _indicators+="๐Ÿ“น " + elif _file_is_audio "${_notebook_path}/${__basename}" + then + _indicators+="๐Ÿ”‰ " + fi - if _file_is_encrypted "${_notebook_path}/${__basename}" - then - _indicators+="๐Ÿ”’ " - fi + if _file_is_encrypted "${_notebook_path}/${__basename}" + then + _indicators+="๐Ÿ”’ " + fi - if [[ "${_title_or_basename}" =~ todo|Todo|TODO ]] - then - _indicators+="โœ… " - _extra_length=1 - fi + if [[ "${_title_or_basename}" =~ todo|Todo|TODO ]] + then + _indicators+="โœ… " + _extra_length=1 + fi - if [[ -n "${_indicators:-}" ]] - then - _maybe_padded_indicators="${_indicators}" - fi + if [[ -n "${_indicators:-}" ]] + then + _maybe_padded_indicators="${_indicators}" fi + fi - local _list_item="${_maybe_padded_indicators}${_title_or_basename}" + local _list_item="${_maybe_padded_indicators}${_title_or_basename}" - if ! ((_no_id)) - then - # NOTE: Plain version serves as the reference version and can be - # used for length calculations. - local _item_identifier= - local _max_identifier= + if ! ((_no_id)) + then + # NOTE: Plain version serves as the reference version and can be + # used for length calculations. + local _item_identifier= + local _max_identifier= - _item_identifier="$( - NB_NOTEBOOK_PATH="${_notebook_path}" _index get_id "${__basename}" - )" + _item_identifier="$( + NB_NOTEBOOK_PATH="${_notebook_path}" _index get_id "${__basename}" + )" - if [[ -n "${_maybe_scope:-}" ]] - then - _item_identifier="${_notebook_escaped_name}:${_item_identifier}" - _max_identifier="${_notebook_escaped_name}:${_max_id}" - else - _item_identifier="${_item_identifier}" - _max_identifier="${_max_id}" - fi + if [[ -n "${_maybe_scope:-}" ]] + then + _item_identifier="${_notebook_escaped_name}:${_item_identifier}" + _max_identifier="${_notebook_escaped_name}:${_max_id}" + else + _item_identifier="${_item_identifier}" + _max_identifier="${_max_id}" + fi - local _id_item= - _id_item="[${_item_identifier}]" + local _id_item= + _id_item="[${_item_identifier}]" - local _id_item_color= - _id_item_color="$(_color_id_brackets "${_item_identifier}")" + local _id_item_color= + _id_item_color="$(_color_id_brackets "${_item_identifier}")" - # Use calculated number of spaces for nicer formatting. - local _spaces='' - local _spaces_length=1 - _spaces_length=$(( ${#_max_identifier} + 3 - ${#_id_item} )) - printf -v _spaces '%*s' "${_spaces_length}" "" - fi + # Use calculated number of spaces for nicer formatting. + local _spaces='' + local _spaces_length=1 + _spaces_length=$(( ${#_max_identifier} + 3 - ${#_id_item} )) + printf -v _spaces '%*s' "${_spaces_length}" "" + fi - if ! ((_list_filenames)) && - ! ((_print_excerpt)) && - [[ "${_title_or_basename}" == "${__basename}" ]] - then - local _first_line= - _first_line="$(_get_first_line "${_notebook_path}/${__basename}")" + if ! ((_list_filenames)) && + ! ((_print_excerpt)) && + [[ "${_title_or_basename}" == "${__basename}" ]] + then + local _first_line= + _first_line="$(_get_first_line "${_notebook_path}/${__basename}")" - if [[ -n "${_first_line:-}" ]] - then - _list_item="${_list_item} ยท \"${_first_line}\"" - fi + if [[ -n "${_first_line:-}" ]] + then + _list_item="${_list_item} ยท \"${_first_line}\"" fi + fi - local _print_item= - local _print_item_color= + local _print_item= + local _print_item_color= - if ((_no_id)) - then - _print_item="${_list_item}" - _print_item_color="${_list_item}" - else - local _list_item_id="${_id_item}${_spaces}${_list_item}" - local _list_item_id_color="${_id_item_color}${_spaces}${_list_item}" + if ((_no_id)) + then + _print_item="${_list_item}" + _print_item_color="${_list_item}" + else + local _list_item_id="${_id_item}${_spaces}${_list_item}" + local _list_item_id_color="${_id_item_color}${_spaces}${_list_item}" - _print_item="${_list_item_id}" - _print_item_color="${_list_item_id_color}" - fi + _print_item="${_list_item_id}" + _print_item_color="${_list_item_id_color}" + fi - if ((_list_color_enabled)) && - ! ((_list_filenames)) && - ! ((_no_id)) - then - # Calculate apparent length of the list item string, with byte and - # character calculations for handling unicode characters. - # - # More info: https://stackoverflow.com/a/31009961 - local _item_line_length_chars= - _item_line_length_chars="${#_print_item}" + if ((_list_color_enabled)) && + ! ((_list_filenames)) && + ! ((_no_id)) + then + # Calculate apparent length of the list item string, with byte and + # character calculations for handling unicode characters. + # + # More info: https://stackoverflow.com/a/31009961 + local _item_line_length_chars= + _item_line_length_chars="${#_print_item}" - LANG=C LC_ALL=C + LANG=C LC_ALL=C - local _item_line_length_bytes= - _item_line_length_bytes="${#_print_item}" + local _item_line_length_bytes= + _item_line_length_bytes="${#_print_item}" - LANG="${_LANG}" LC_ALL="${_LC_ALL}" + LANG="${_LANG}" LC_ALL="${_LC_ALL}" - local _item_line_length_diff= - _item_line_length_diff=$(((_item_line_length_bytes-_item_line_length_chars)/3)) + local _item_line_length_diff= + _item_line_length_diff=$(((_item_line_length_bytes-_item_line_length_chars)/3)) - local _item_line_length= - _item_line_length=$((_item_line_length_chars+_item_line_length_diff)) + local _item_line_length= + _item_line_length=$((_item_line_length_chars+_item_line_length_diff)) - if ((_extra_length)) - then - _item_line_length=$((_item_line_length+_extra_length)) - fi + if ((_extra_length)) + then + _item_line_length=$((_item_line_length+_extra_length)) fi + fi - if ((_list_color_enabled)) && - ! ((_print_excerpt)) && - ! ((_list_filenames)) && - ! ((_no_id)) + if ((_list_color_enabled)) && + ! ((_print_excerpt)) && + ! ((_list_filenames)) && + ! ((_no_id)) + then + if [[ "${_item_line_length}" -gt "${_columns}" ]] && + { + [[ -n "${_maybe_title:-}" ]] || + [[ -n "${_first_line:-}" ]] + } then - if [[ "${_item_line_length}" -gt "${_columns}" ]] && - { - [[ -n "${_maybe_title:-}" ]] || - [[ -n "${_first_line:-}" ]] - } - then - _wrap off - fi + _wrap off fi + fi - if ((_list_color_enabled)) + if ((_list_color_enabled)) + then + printf "%s" "${_print_item_color}" + else + printf "%s" "${_print_item}" + fi + + if ((_list_color_enabled)) && + ! ((_print_excerpt)) && + ! ((_list_filenames)) && + ! ((_no_id)) + then + if [[ "${_item_line_length}" -gt "${_columns}" ]] && + { + [[ -n "${_maybe_title:-}" ]] || + [[ -n "${_first_line:-}" ]] + } then - printf "%s" "${_print_item_color}" - else - printf "%s" "${_print_item}" + _wrap on fi + fi - if ((_list_color_enabled)) && - ! ((_print_excerpt)) && - ! ((_list_filenames)) && - ! ((_no_id)) + # End of line. + printf "\\n" + + if ((_print_excerpt)) && + [[ "${_excerpt_length}" =~ ^[1-9] ]] && + _file_is_text "${_notebook_path}/${__basename}" + then # excerpt is specified with a non-zero digit, required by `head`. + if [[ -z "${_item_line_length:-}" ]] || + [[ "${_item_line_length}" -gt "${_columns}" ]] then - if [[ "${_item_line_length}" -gt "${_columns}" ]] && - { - [[ -n "${_maybe_title:-}" ]] || - [[ -n "${_first_line:-}" ]] - } - then - _wrap on - fi + _print_line "$(printf "%-${_columns}s" '.')" | tr -d '\n' + else + _print_line "$(printf "%-${_item_line_length}s" '.')" fi - - # End of line. + head -"${_excerpt_length}" "${_notebook_path}/${__basename}" printf "\\n" + fi - if ((_print_excerpt)) && - [[ "${_excerpt_length}" =~ ^[1-9] ]] && - _file_is_text "${_notebook_path}/${__basename}" - then # excerpt is specified with a non-zero digit, required by `head`. - if [[ -z "${_item_line_length:-}" ]] || - [[ "${_item_line_length}" -gt "${_columns}" ]] - then - _print_line "$(printf "%-${_columns}s" '.')" | tr -d '\n' - else - _print_line "$(printf "%-${_item_line_length}s" '.')" - fi - head -"${_excerpt_length}" "${_notebook_path}/${__basename}" - printf "\\n" - fi + _printed_counter=$((_printed_counter+1)) + done - _counter=$((_counter+1)) - done + if [[ "${_limit:-}" ]] && + [[ "${_filenames_counter}" -gt "${_printed_counter}" ]] + then # the limit has been reached. + printf "%s omitted. %s total.\\n" \ + "$((_filenames_counter-_printed_counter))" \ + "${_filenames_counter}" + fi - if ! ((_counter)) && [[ -n "${_selector:-}" ]] + if [[ -n "${_selector:-}" ]] && ! ((_printed_counter)) + then + _warn \ + printf "Note not found: %s\\n" "$(_color_primary "${_selector}")" && + return 1 + fi + + if ! ((_filenames_counter)) && [[ -z "${_selector_identifier:-}" ]] + then + if ((_hard_empty)) then - _warn \ - printf "Note not found: %s\\n" "$(_color_primary "${_selector}")" && - return 1 + return 1 + elif [[ "${_type}" =~ ^bookmark$|^bookmarks$ ]] + then + cat <") +Help information: + $(_color_primary "${_ME} help bookmark") +HEREDOC + elif [[ "${_type}" =~ ^archive|^audio|^document|^image|^video ]] + then + _type="$(printf "%s\\n" "${_type}" | sed -e 's/s$//')" + + cat < | )") +Help information: + $(_color_primary "${_ME} help import") +HEREDOC + else + cat <") +Import a file: + $(_color_primary "${_ME} ${_maybe_scope}import ( | )") +Help information: + $(_color_primary "${_ME} help") +HEREDOC fi + + return 0 fi } | if ((_use_pager)) && [[ -n "${PAGER:-}" ]] && [[ ! "${PAGER}" =~ less ]] then @@ -6411,7 +6423,7 @@ _ls() { case "${__arg}" in -a|--all) - _limit=999999999999999 + _limit="${_NO_LIMIT}" ;; --auto-align|--centered|--justified|--plain) _header_flags+=("${__arg}") From f5e275fc236a7344fa74081ca062422fe1d09a63 Mon Sep 17 00:00:00 2001 From: William Melody Date: Wed, 30 Sep 2020 11:29:28 -0700 Subject: [PATCH 02/12] Improve logic for displaying "omitted" in _list(). --- nb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nb b/nb index b83331bd6..31ebe335a 100755 --- a/nb +++ b/nb @@ -5990,7 +5990,7 @@ _list() { _printed_counter=$((_printed_counter+1)) done - if [[ "${_limit:-}" ]] && + if [[ -z "${_selector:-}" ]] && [[ "${_filenames_counter}" -gt "${_printed_counter}" ]] then # the limit has been reached. printf "%s omitted. %s total.\\n" \ From 642d77865640319f9c989d3c1387dbb061edd67c Mon Sep 17 00:00:00 2001 From: William Melody Date: Sat, 24 Oct 2020 15:58:47 -0700 Subject: [PATCH 03/12] Include additional test for `list` ids. --- test/list.bats | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/list.bats b/test/list.bats index 72dd650f4..56b301f21 100644 --- a/test/list.bats +++ b/test/list.bats @@ -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." { From de9c0bb07c5ec0ce38bb0860506c330a6a9f55bd Mon Sep 17 00:00:00 2001 From: William Melody Date: Sat, 24 Oct 2020 16:32:24 -0700 Subject: [PATCH 04/12] Update filename counting in `list` for streaming. --- nb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/nb b/nb index 51b8ac04c..9fba5c010 100755 --- a/nb +++ b/nb @@ -6232,10 +6232,6 @@ _list() { ((_reverse)) && _list_files_options+=("--reverse") ((_sort)) && _list_files_options+=("--sort") - _filenames=($(_list_files "${_list_files_options[@]}")) || : - - local _filenames_count="${#_filenames[@]}" - if _notebooks current --selected then _maybe_scope="$(_notebooks show "${_notebook_path}" --escaped --no-color):" @@ -6502,11 +6498,11 @@ ${_TPUT_SGR0}" return 0 fi - elif [[ "${_filenames_count}" -gt "${_limit}" ]] + elif [[ "${_filenames_counter}" -gt "${_limit}" ]] then printf "%s omitted. %s total.\\n" \ - "$((_filenames_count-_printed_counter))" \ - "${_filenames_count}" + "$((_filenames_counter-_printed_counter))" \ + "${_filenames_counter}" return 0 fi fi From 3677f3092d20d71e627dbd761d82c368388a2717 Mon Sep 17 00:00:00 2001 From: William Melody Date: Sat, 24 Oct 2020 16:37:40 -0700 Subject: [PATCH 05/12] Improve selection limit behavior in `list`. --- nb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/nb b/nb index 9fba5c010..fc13e0f37 100755 --- a/nb +++ b/nb @@ -6301,7 +6301,14 @@ _list() { if [[ -n "${_limit:-}" ]] && [[ "${_printed_counter}" -ge "${_limit}" ]] then # the limit has been reached - continue + if [[ -n "${_selector_identifier:-}" ]] + then + # keep looking for matches to count total + continue + else + _filenames_counter="$(_count)" + break + fi fi if ((_list_paths)) From 6bd324e796a3548a724b664e9f1549415bf12098 Mon Sep 17 00:00:00 2001 From: William Melody Date: Sat, 24 Oct 2020 17:32:11 -0700 Subject: [PATCH 06/12] Count items scoped with options in `list`. --- nb | 14 ++++++++++---- test/count.bats | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/nb b/nb index fc13e0f37..97c25c204 100755 --- a/nb +++ b/nb @@ -4071,10 +4071,14 @@ Description: Print the number of items in the current notebook. HEREDOC _count() { - local _notebook_path - _notebook_path="$(_notebooks current --path)" + local _list_files_options=(${@:-}) + + if [[ -z "${_list_files_options[*]:-}" ]] + then + _list_files_options+=("$(_notebooks current --path)") + fi - _list_files "${_notebook_path}" | wc -l | tr -d ' ' + _list_files "${_list_files_options[@]}" | wc -l | tr -d ' ' } # delete ############################################################### delete @@ -6306,7 +6310,9 @@ _list() { # keep looking for matches to count total continue else - _filenames_counter="$(_count)" + _filenames_counter="$( + _count "${_list_files_options[@]}" + )" break fi fi diff --git a/test/count.bats b/test/count.bats index 2082ace64..3e505bb31 100644 --- a/test/count.bats +++ b/test/count.bats @@ -42,6 +42,45 @@ HEREDOC [[ "${lines[0]}" -eq 3 ]] } +@test "\`count --type \` exits with 0 and counts items of type." { + { + "${_NB}" init + + cat < Date: Sat, 24 Oct 2020 17:33:32 -0700 Subject: [PATCH 07/12] Remove unneeded assignment in `count`. --- nb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/nb b/nb index 97c25c204..88e27ad70 100755 --- a/nb +++ b/nb @@ -4073,11 +4073,6 @@ HEREDOC _count() { local _list_files_options=(${@:-}) - if [[ -z "${_list_files_options[*]:-}" ]] - then - _list_files_options+=("$(_notebooks current --path)") - fi - _list_files "${_list_files_options[@]}" | wc -l | tr -d ' ' } From ceef5cb5d9ae483af16659fa2c1597530177d47f Mon Sep 17 00:00:00 2001 From: William Melody Date: Sat, 24 Oct 2020 17:37:46 -0700 Subject: [PATCH 08/12] Improve alignment. --- nb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nb b/nb index 88e27ad70..0a56bdfae 100755 --- a/nb +++ b/nb @@ -6508,7 +6508,7 @@ ${_TPUT_SGR0}" elif [[ "${_filenames_counter}" -gt "${_limit}" ]] then - printf "%s omitted. %s total.\\n" \ + printf "%s omitted. %s total.\\n" \ "$((_filenames_counter-_printed_counter))" \ "${_filenames_counter}" return 0 From 6210b437f709bcead5e1579bf125b3adba1e5826 Mon Sep 17 00:00:00 2001 From: William Melody Date: Sat, 24 Oct 2020 17:39:23 -0700 Subject: [PATCH 09/12] Improve variable names in `list`. --- nb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/nb b/nb index 0a56bdfae..6321ed6c2 100755 --- a/nb +++ b/nb @@ -6239,8 +6239,8 @@ _list() { local _counter=0 local _matches=0 - local _filenames_counter=0 - local _printed_counter=0 + local _filenames_count=0 + local _printed_count=0 local _max_id _max_id="$(_index get_max_id)" @@ -6251,7 +6251,7 @@ _list() { } | { while read -r __basename do - _filenames_counter=$((_filenames_counter+1)) + _filenames_count=$((_filenames_count+1)) local _first_line= local _maybe_title= @@ -6298,14 +6298,14 @@ _list() { fi fi - if [[ -n "${_limit:-}" ]] && [[ "${_printed_counter}" -ge "${_limit}" ]] + if [[ -n "${_limit:-}" ]] && [[ "${_printed_count}" -ge "${_limit}" ]] then # the limit has been reached if [[ -n "${_selector_identifier:-}" ]] then # keep looking for matches to count total continue else - _filenames_counter="$( + _filenames_count="$( _count "${_list_files_options[@]}" )" break @@ -6484,7 +6484,7 @@ ${_TPUT_SGR0}" printf "\\n" fi - _printed_counter=$((_printed_counter+1)) + _printed_count=$((_printed_count+1)) done if [[ -n "${_limit:-}" ]] @@ -6495,7 +6495,7 @@ ${_TPUT_SGR0}" then local _label="matches" local _omitted_count - _omitted_count=$((_matches-_printed_counter)) + _omitted_count=$((_matches-_printed_count)) [[ "${_omitted_count}" -eq 1 ]] && _label="match" @@ -6506,16 +6506,16 @@ ${_TPUT_SGR0}" return 0 fi - elif [[ "${_filenames_counter}" -gt "${_limit}" ]] + elif [[ "${_filenames_count}" -gt "${_limit}" ]] then - printf "%s omitted. %s total.\\n" \ - "$((_filenames_counter-_printed_counter))" \ - "${_filenames_counter}" + printf "%s omitted. %s total.\\n" \ + "$((_filenames_count-_printed_count))" \ + "${_filenames_count}" return 0 fi fi - if ! ((_printed_counter)) && [[ -n "${_selector:-}" ]] + if ! ((_printed_count)) && [[ -n "${_selector:-}" ]] then local _filter_message= _filter_message="$(_color_primary "${_selector}")" @@ -6530,7 +6530,7 @@ ${_TPUT_SGR0}" return 1 fi - if ! ((_filenames_counter)) && [[ -z "${_selector_identifier:-}" ]] + if ! ((_filenames_count)) && [[ -z "${_selector_identifier:-}" ]] then if ((_error_on_empty)) then From f87951f13ac279b887ad798f20b49080309bfe4b Mon Sep 17 00:00:00 2001 From: William Melody Date: Sat, 24 Oct 2020 17:39:53 -0700 Subject: [PATCH 10/12] Improve spacing in `list`. --- nb | 1 + 1 file changed, 1 insertion(+) diff --git a/nb b/nb index 6321ed6c2..0ff1a3a8c 100755 --- a/nb +++ b/nb @@ -6308,6 +6308,7 @@ _list() { _filenames_count="$( _count "${_list_files_options[@]}" )" + break fi fi From c2c23c5209275076257e7e71235f89034efbc841 Mon Sep 17 00:00:00 2001 From: William Melody Date: Sat, 24 Oct 2020 17:47:39 -0700 Subject: [PATCH 11/12] Improve formatting in _list(). --- nb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nb b/nb index 0ff1a3a8c..42cd39b97 100755 --- a/nb +++ b/nb @@ -6516,7 +6516,7 @@ ${_TPUT_SGR0}" fi fi - if ! ((_printed_count)) && [[ -n "${_selector:-}" ]] + if ! ((_printed_count)) && [[ -n "${_selector:-}" ]] then local _filter_message= _filter_message="$(_color_primary "${_selector}")" From 3291bbf50db12cf0e6abd6ad81f9fdeef0eaf629 Mon Sep 17 00:00:00 2001 From: William Melody Date: Sat, 24 Oct 2020 18:09:25 -0700 Subject: [PATCH 12/12] Avoid unbound variable errors on macOS. --- nb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nb b/nb index 42cd39b97..80b4f6b5b 100755 --- a/nb +++ b/nb @@ -4073,7 +4073,7 @@ HEREDOC _count() { local _list_files_options=(${@:-}) - _list_files "${_list_files_options[@]}" | wc -l | tr -d ' ' + _list_files "${_list_files_options[@]:-}" | wc -l | tr -d ' ' } # delete ############################################################### delete