Skip to content

Commit

Permalink
Support notebook selectors in nb delete.
Browse files Browse the repository at this point in the history
  • Loading branch information
xwmx committed Aug 26, 2023
1 parent 30025b3 commit 22dc37d
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 6 deletions.
49 changes: 43 additions & 6 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -14205,35 +14205,72 @@ _delete() {
_exit_1 _help "delete"
fi

local _notebook_paths=()
local _notebook_selectors=()
local _target_paths=()
local _target_selectors=()

local __selector=
for __selector in "${_selectors[@]:-}"
do
[[ -z "${__selector:-}" ]] && continue
[[ -z "${__selector:-}" ]] && continue

local _selector_path=
_selector_path="$(_show "${__selector:-}" --path)"
_selector_path="$(_show "${__selector:-}" --path 2>/dev/null || :)"

if [[ -n "${_selector_path:-}" ]]
local _selector_notebook_path=
_selector_notebook_path="$(_notebooks show "${__selector:-}" --path 2>/dev/null || :)"

if [[ -n "${_selector_path}" ]]
then
_target_paths+=("${_selector_path:-}")
if [[ "${_selector_path:-}" == "${_selector_notebook_path:-}" ]]
then
_notebook_paths+=("${_selector_notebook_path}")
_notebook_selectors+=("${__selector}")
else
_target_paths+=("${_selector_path}")
_target_selectors+=("${__selector}")
fi
elif [[ -n "${_selector_notebook_path:-}" ]]
then
_notebook_paths+=("${_selector_notebook_path}")
_notebook_selectors+=("${__selector}")
else
_exit_1 printf "Not found: %s\\n" "$(_color_primary "${__selector}")"
fi
done

local __notebook_selector=
for __notebook_selector in "${_notebook_selectors[@]:-}"
do
[[ -z "${__notebook_selector:-}" ]] && continue

if ((_force))
then
_notebooks delete "${__notebook_selector}" --force
else
_notebooks delete "${__notebook_selector}"
fi
done

if ! ((${#_target_paths[@]}))
then
return
fi

if ((_print_prompt_list)) || ! ((_force))
then
if [[ "${#_selectors[@]}" == 1 ]]
if [[ "${#_target_paths[@]}" == 1 ]]
then
printf "Deleting: %s\\n" "$(_show "${_selectors[0]:-}" --info-line)"
printf "Deleting: %s\\n" "$(_show "${_target_selectors[0]:-}" --info-line)"
else
printf "Deleting:\\n"

local __prompt_selector=
for __prompt_selector in "${_target_paths[@]:-}"
do
[[ -z "${__prompt_selector:-}" ]] && continue

printf "%s\\n" "$(_show "${__prompt_selector:-}" --info-line)"
done

Expand Down
74 changes: 74 additions & 0 deletions test/delete.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,80 @@

load test_helper

# notebooks ###################################################################

_setup_notebooks() {
"${_NB}" init

mkdir -p "${NB_DIR}/Example Notebook"
cd "${NB_DIR}/Example Notebook" || return 1

git init

git remote add origin "${_GIT_REMOTE_URL}"

touch "${NB_DIR}/Example Notebook/.index"

cd "${NB_DIR}" || return 1
}

@test "'delete <notebook>' with non-affirmative prompt response prints message and exits." {
{
_setup_notebooks
}

run "${_NB}" delete "Example Notebook" <<< "n${_NEWLINE}"

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

[[ ${status} -eq 0 ]]

[[ "${output}" =~ \
Deleting\ .*Example\ Notebook.*\.|Moving\ to\ Trash:\ .*Example\ Notebook.* ]]
[[ "${output}" =~ Exiting.*\.\.\. ]]

[[ -e "${NB_DIR}/Example Notebook" ]]
}

@test "'delete <notebook>:' exits with 0 and deletes notebook." {
{
_setup_notebooks

[[ -e "${NB_DIR}/Example Notebook" ]]
}

run "${_NB}" delete "Example Notebook:" <<< "Example Notebook${_NEWLINE}"

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

[[ ${status} -eq 0 ]]
[[ "${output}" =~ Notebook\ deleted\: ]]
[[ "${output}" =~ Example\ Notebook ]]
[[ ! -e "${NB_DIR}/Example Notebook" ]]
[[ "$(cat "${NB_DIR}/.current")" == "home" ]]
}

@test "'delete <notebook>' exits with 0 and deletes notebook." {
{
_setup_notebooks

[[ -e "${NB_DIR}/Example Notebook" ]]
}

run "${_NB}" delete "Example Notebook" --force

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

[[ ${status} -eq 0 ]]
[[ "${output}" =~ Notebook\ deleted\: ]]
[[ "${output}" =~ Example\ Notebook ]]
[[ ! -e "${NB_DIR}/Exampl Notebook" ]]
[[ "$(cat "${NB_DIR}/.current")" == "home" ]]
}

# aliases #####################################################################

@test "'<notebook>:trash <id>' deletes properly without errors." {
Expand Down

0 comments on commit 22dc37d

Please sign in to comment.