Skip to content

Commit

Permalink
Handle brackets in filenames in index.
Browse files Browse the repository at this point in the history
refs gh-292
  • Loading branch information
xwmx committed Nov 27, 2023
1 parent 4a5086f commit 7b461ac
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
6 changes: 6 additions & 0 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -4661,6 +4661,9 @@ _index() {
_exit_1 _help index
fi

_basename="${_basename//'['/\\[}"
_basename="${_basename//']'/\\]}"

if grep -q "^${_basename}$" "${_index_path}"
then
_sed_i -e "s/^${_basename}$//g" "${_index_path}"
Expand Down Expand Up @@ -4944,6 +4947,9 @@ _index() {
_exit_1 _help index
fi

_old_basename="${_old_basename//'['/\\[}"
_old_basename="${_old_basename//']'/\\]}"

if grep -q "^${_old_basename}$" "${_index_path}"
then
_sed_i -e "s/^${_old_basename}$/${_new_basename}/g" "${_index_path}"
Expand Down
33 changes: 33 additions & 0 deletions test/index.bats
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ load test_helper
[[ "$(cat "${NB_DIR}/home/.index")" == "" ]]
}

@test "'index delete <filename>' with brackets and spaces in filename deletes an item from the index." {
{
"${_NB}" init
"${_NB}" add "[ ] first.md" --title "one"
}

run "${_NB}" index delete "$(ls "${NB_DIR}/home")"

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

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

@test "'index delete' with no argument returns 1 and prints help." {
{
"${_NB}" init
Expand Down Expand Up @@ -344,6 +359,24 @@ load test_helper
[[ "$(cat "${NB_DIR}/home/.index")" == "example.md" ]]
}

@test "'index update <old> <new>' with brackets and spaces in <old> filename updates the index." {
{
"${_NB}" init
"${_NB}" add "[ ] first.md" --title "# one"
}

run "${_NB}" index update "$(ls "${NB_DIR}/home")" "example.md"

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"
printf \
"$(cat \"\$\{NB_DIR\}/home/.index\"): '%s'\\n" \
"$(cat "${NB_DIR}/home/.index")"

[[ "${status}" -eq 0 ]]
[[ "$(cat "${NB_DIR}/home/.index")" == "example.md" ]]
}

@test "'index update' with no arguments returns 1 and prints help." {
{
"${_NB}" init
Expand Down
48 changes: 48 additions & 0 deletions test/move-rename.bats
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,54 @@ Moved\ to:\ .*[.*Example\ Folder/1.*].*\ .*Example\ Folder/Example\ File\.js.*

# --to title / --to-title #####################################################

@test "'move --to title' with brackets in existing filename renames todo." {
# NOTE: https://github.com/xwmx/nb/issues/292
{
"${_NB}" init

"${_NB}" add --filename "[ ] Example Filename.md" --title "Example Title"


_files=($(ls "${NB_DIR}/home/"))
printf "\${_files[0]}: '%s'\\n" "${_files[0]}"

[[ -f "${NB_DIR}/home/[ ] Example Filename.md" ]]
[[ ! -f "${NB_DIR}/home/example_title.md" ]]
}

run "${_NB}" rename 1 --to title <<< "y${_NEWLINE}"

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

# Returns status 0:

[[ ${status} -eq 0 ]]

# Moves file:

[[ ! -f "${NB_DIR}/home/[ ] Example Filename.md" ]]
[[ -f "${NB_DIR}/home/example_title.md" ]]

# Creates git commit:

cd "${NB_DIR}/home" || return 1
while [[ -n "$(git status --porcelain)" ]]
do
sleep 1
done
git log | grep -q '\[nb\] Move'

# Prints output:

# [[ "${lines[0]}" =~ \
# Moving:\ \ \ .*[.*2.*].*\ ✔️\ \ .*example_one-1.todo.md.*\ \".*[.*\ .*].*\ Example\ Two\" ]]
# [[ "${lines[1]}" =~ \
# To:\ \ \ \ \ \ \ .*example_two.todo.md.* ]]
# [[ "${lines[2]}" =~ \
# Moved\ to:\ .*[.*2.*].*\ .*example_two.todo.md.*\ \".*[.*\ .*].*\ Example\ Two\" ]]
}

@test "'move --to title' with duplicated, renamed todo renames title." {
# NOTE: https://github.com/xwmx/nb/issues/292
{
Expand Down

0 comments on commit 7b461ac

Please sign in to comment.