Skip to content

Commit

Permalink
Print current day's notes when <content> is omitted with daily.
Browse files Browse the repository at this point in the history
refs gh-300
  • Loading branch information
xwmx committed Jan 31, 2024
1 parent 14e9164 commit 50e9724
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
34 changes: 21 additions & 13 deletions plugins/daily.nb-plugin
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ HEREDOC
_daily() {
local _content="${1:-}"

if [[ -z "${_content:-}" ]]
then
_exit_1 _help "daily"
fi

local _notebook_path=
_notebook_path="$(_notebooks current --path)"

Expand All @@ -44,15 +39,28 @@ _daily() {
local _target_path=
_target_path="${_notebook_path}/${_target_filename}"

local _timestamp=
_timestamp="$(date "+%H:%M:%S")"

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

if [[ ! -e "${_target_path:-}" ]]
if [[ -z "${_content:-}" ]]
then
if [[ ! -e "${_target_path:-}" ]]
then
printf "Add the first note of the day: %s daily <content>\\n" "${_ME}"
else
_show "${_target_path:-}"
fi
elif [[ "${_content:-}" == "--print" ]]
then
_add --content "${_timestamped_content}" --filename "${_target_filename}"
_show "${_target_path:-}" --print
else
_edit "${_target_filename}" --append "${_timestamped_content}"
local _timestamp=
_timestamp="$(date "+%H:%M:%S")"

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

if [[ ! -e "${_target_path:-}" ]]
then
_add --content "${_timestamped_content}" --filename "${_target_filename}"
else
_edit "${_target_filename}" --append "${_timestamped_content}"
fi
fi
}
32 changes: 28 additions & 4 deletions test/plugin-daily.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load test_helper

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

@test "'nb daily' with no content exits with 1 and prints help." {
@test "'nb daily' with no content and no existing note prints message." {
{
"${_NB}" init
"${_NB}" plugins install "${NB_TEST_BASE_PATH}/../plugins/daily.nb-plugin"
Expand All @@ -15,9 +15,33 @@ load test_helper
printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 1 ]]
[[ "${lines[0]}" =~ Usage.*\: ]]
[[ "${lines[1]}" =~ nb\ daily ]]
[[ "${status}" -eq 0 ]]

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

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

"${_NB}" daily "Example content one."
"${_NB}" daily "Example content two."
}

run "${_NB}" daily

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

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

"${_NB}" show 1 --print

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

@test "'nb daily <content>' without existing note adds new note with content." {
Expand Down

0 comments on commit 50e9724

Please sign in to comment.