Skip to content

Commit

Permalink
Improve listing in daily plugin.
Browse files Browse the repository at this point in the history
refs gh-300
  • Loading branch information
xwmx committed Jan 31, 2024
1 parent cf26f09 commit 5c9d8fc
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 7 deletions.
92 changes: 86 additions & 6 deletions plugins/daily.nb-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,36 @@ _subcommands add "daily"

# Define help and usage text with `_subcommands describe <subcommand> <usage>`.
_subcommands describe "daily" <<HEREDOC
Usage:
nb daily [<content>]
$(_color_primary "Usage"):
nb daily [<content>] [--prev [<number>]]
Description:
$(_color_primary "Options"):
--prev [<number>] List previous days and show day by previous <number>.
$(_color_primary "Description"):
Add notes to a daily log.
Examples:
$(_color_primary "Examples"):
nb daily "Example note content."
nb daily --prev
nb daily --prev 3
HEREDOC

# Define the subcommand as a function, named with a leading underscore.
_daily() {
# Usage: _daily_show <path>
_daily_show() {
local _target_path="${1:-}"

[[ -n "${_target_path:-}" ]] || return 1

printf "%s:\\n" "$(_color_primary "${_target_path##*\/}")"

_show "${_target_path:-}" --print

return 0
}

local _content="${1:-}"

local _notebook_path=
Expand All @@ -44,16 +62,78 @@ _daily() {
if [[ ! -e "${_target_path:-}" ]]
then
printf "Add the first note of the day: %s daily <content>\\n" "${_ME}"

return 0
else
_daily_show "${_target_path:-}"
fi
elif _contains "${_content:-}" \
"--all" "--ago" "--day" "--days" "--prev" "--previous"
then
local _daily_note_paths=()

_daily_note_paths=($(
find "${_notebook_path:-}" \
-type f \
-maxdepth 1 \
-name "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].md" \
| sort
))

if ! ((${#_daily_note_paths[@]}))
then
printf "Add the first daily note: %s daily <content>\\n" "${_ME}"

return 0
fi

if [[ "${2:-}" =~ ^[0-9]+$ ]]
then
local _note_number="${2:-0}"

if [[ "${_note_number:-}" -gt "${#_daily_note_paths[@]}" ]]
then
_exit_1 printf "Not found.\\n"
fi

_note_number=$((_note_number + 1))

_target_path="$(
printf "%s\\n" "${_daily_note_paths[@]:-}" \
| tail -${_note_number:-} \
| head -1
)"

_daily_show "${_target_path:-}"
else
_show "${_target_path:-}" --print
{
printf "%s\\n" "${_daily_note_paths[@]:-}"
} | {
local _counter=
_counter=$((${#_daily_note_paths[@]}))

local __line=
while IFS= read -r __line || [[ -n "${__line:-}" ]]
do
_counter=$((_counter - 1))

{
_list "${__line}" --no-color
} | {
printf "%s %s\\n" \
"$(_color_brackets "${_counter:-0}")" \
"$(cat)"
}
done
}
fi
else
local _timestamp=
_timestamp="$(date "+%H:%M:%S")"

local _timestamped_content="[${_timestamp}] ${_content}"

if [[ ! -e "${_target_path:-}" ]]
if [[ ! -e "${_target_path:-}" ]]
then
_add --content "${_timestamped_content}" --filename "${_target_filename}"
else
Expand Down
108 changes: 107 additions & 1 deletion test/plugin-daily.bats
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,110 @@

load test_helper

# `daily --prev` ##############################################################

@test "'nb daily --prev' with no content and no existing note prints message." {
{
"${_NB}" init
"${_NB}" plugins install "${NB_TEST_BASE_PATH}/../plugins/daily.nb-plugin"
}

run "${_NB}" daily --prev

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

[[ "${status}" -eq 0 ]]

[[ "${output}" == "Add the first daily note: nb daily <content>" ]]
}

@test "'nb daily --prev' with existing notes lists notes." {
{
"${_NB}" init
"${_NB}" plugins install "${NB_TEST_BASE_PATH}/../plugins/daily.nb-plugin"

"${_NB}" add \
--content "[01:01:01] Example content one." \
--filename "43210101.md"

"${_NB}" add \
--content "[02:02:02] Example content two." \
--filename "43210202.md"

"${_NB}" add \
--content "[03:03:03] Example content three." \
--filename "43210303.md"
}

run "${_NB}" daily --prev

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

[[ "${status}" -eq 0 ]]

[[ "${lines[0]}" =~ \
.*[.*2.*].*\ \[1\]\ 43210101.md\ ·\ \"\[01:01:01\]\ Example\ content\ one\.\" ]]
[[ "${lines[1]}" =~ \
.*[.*1.*].*\ \[2\]\ 43210202.md\ ·\ \"\[02:02:02\]\ Example\ content\ two\.\" ]]
[[ "${lines[2]}" =~ \
.*[.*0.*].*\ \[3\]\ 43210303.md\ ·\ \"\[03:03:03\]\ Example\ content\ three\.\" ]]
}

@test "'nb daily --prev <number>' shows notes." {
{
"${_NB}" init
"${_NB}" plugins install "${NB_TEST_BASE_PATH}/../plugins/daily.nb-plugin"

"${_NB}" add \
--content "[01:01:01] Example content one." \
--filename "43210101.md"

"${_NB}" add \
--content "[02:02:02] Example content two." \
--filename "43210202.md"

"${_NB}" add \
--content "[03:03:03] Example content three." \
--filename "43210303.md"
}

run "${_NB}" daily --prev 1

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

[[ "${status}" -eq 0 ]]

[[ "${lines[0]}" =~ \
.*43210202\.md.*: ]]
[[ "${lines[1]}" =~ \
\[02:02:02\]\ Example\ content\ two\. ]]

run "${_NB}" daily --prev 0

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

[[ "${status}" -eq 0 ]]

[[ "${lines[0]}" =~ \
.*43210303\.md.*: ]]
[[ "${lines[1]}" =~ \
\[03:03:03\]\ Example\ content\ three\. ]]

run "${_NB}" daily --prev 42

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

[[ "${status}" -eq 1 ]]

[[ "${lines[0]}" =~ \
.*!.*\ Not\ found\. ]]
}

# `daily` #####################################################################

@test "'nb daily' with no content and no existing note prints message." {
Expand Down Expand Up @@ -39,8 +143,10 @@ load test_helper
"${_NB}" show 1 --print

[[ "${lines[0]}" =~ \
\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\]\ Example\ content\ one\. ]]
.*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.md.*: ]]
[[ "${lines[1]}" =~ \
\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\]\ Example\ content\ one\. ]]
[[ "${lines[2]}" =~ \
\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\]\ Example\ content\ two\. ]]
}

Expand Down

0 comments on commit 5c9d8fc

Please sign in to comment.