Skip to content

Commit

Permalink
Skip common emacs and vim temporary files in index and list.
Browse files Browse the repository at this point in the history
refs gh-171
refs gh-173
  • Loading branch information
xwmx committed Feb 27, 2022
1 parent 9a909e5 commit ad5d3e2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
16 changes: 15 additions & 1 deletion nb
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,16 @@ _COLUMNS="$(_tput cols)"
# An array of supported aggregate file types.
_FILE_TYPES=(archive audio bookmark document folder image text video)

# $_IGNORE_PATTERNS
#
# An array of filename patterns to ignore, formatted as `sed -e` arguments.
_IGNORE_PATTERNS=(
-e "/~$/d"
-e "/^#.*#$/d"
-e "/\.swa?p$/d"
-e "/^\.#*/d"
)

# $_MD
#
# Middle dot character.
Expand Down Expand Up @@ -4313,7 +4323,9 @@ _index() {

if ! grep -q "^${_basename}$" "${_index_path}"
then
printf "%s\\n" "${_basename}" >> "${_index_path}"
{
printf "%s\\n" "${_basename}" | sed -E "${_IGNORE_PATTERNS[@]:-}"
} >> "${_index_path}"
fi
;;
delete)
Expand Down Expand Up @@ -5364,6 +5376,8 @@ _list() {
_list_files "${_list_path}" "${_list_files_options[@]:-}" || :
fi
fi
} | {
sed -E "${_IGNORE_PATTERNS[@]:-}"
} | {
local _filenames_count=0
local _matches=0
Expand Down
33 changes: 33 additions & 0 deletions test/index.bats
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,39 @@ load test_helper
[[ -z "${lines[1]}" ]]
}

@test "'index add <filename>' skips temporary files." {
{
"${_NB}" init

declare _temp_filenames=(
"Example Temp One.md~"
"#Example Temp Two.md#"
"Example Temp Three.md.swp"
"Example Temp Four.md.swap"
".#Example Temp Five.md"
)

declare __filename=
for __filename in "${_temp_filenames[@]:-}"
do
"${_NB}" run touch "${__filename:-}"
done
}

declare __filename=
for __filename in "${_temp_filenames[@]:-}"
do
run "${_NB}" index add "${__filename:-}"

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 0 ]]
[[ -z "${output}" ]]
[[ -z "$(cat "${NB_DIR/home/.index}")" ]]
done
}

# get_basename ################################################################

@test "'index get_basename' with valid index prints the filename for an id." {
Expand Down
34 changes: 34 additions & 0 deletions test/list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,40 @@

load test_helper

# temporary files #############################################################

@test "'list' ignores common temporary files." {
{
"${_NB}" init

"${_NB}" add "File One.md" --content "Example content one."
"${_NB}" add "File Two.md" --content "Example content two."

"${_NB}" run touch "Example Temp One.md~"
"${_NB}" run touch "#Example Temp Two.md#"
"${_NB}" run touch "Example Temp Three.md.swp"
"${_NB}" run touch "Example Temp Four.md.swap"
"${_NB}" run touch ".#Example Temp Five.md"

"${_NB}" add "File Three.md" --content "Example content three."
}

run "${_NB}" list

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 0 ]]
[[ "${#lines[@]}" -eq 3 ]]

[[ "${lines[0]}" =~ \
.*[.*3.*].*\ File\ Three\.md\ \·\ \"Example\ content\ three\.\" ]]
[[ "${lines[1]}" =~ \
.*[.*2.*].*\ File\ Two\.md\ \·\ \"Example\ content\ two\.\" ]]
[[ "${lines[2]}" =~ \
.*[.*1.*].*\ File\ One\.md\ \·\ \"Example\ content\ one\.\" ]]
}

# list content (title, filename, first line) ##################################

@test "'list' includes titles when present, otherwise filenames with first lines." {
Expand Down

0 comments on commit ad5d3e2

Please sign in to comment.