Skip to content

Commit

Permalink
Add daily plugin.
Browse files Browse the repository at this point in the history
Notes are saved with timestamps to a daily log.

refs gh-300
  • Loading branch information
xwmx committed Jan 31, 2024
1 parent 2fd5538 commit 14e9164
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
58 changes: 58 additions & 0 deletions plugins/daily.nb-plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bash
###############################################################################
# daily.nb-plugin
#
# Write to a daily log.
#
# Install with:
# nb plugin install https://github.com/xwmx/nb/blob/master/plugins/daily.nb-plugin
#
# A plugin for `nb`.
# https://github.com/xwmx/nb
###############################################################################

# Add the new subcommand name with `_subcommands add <name>`.
_subcommands add "daily"

# Define help and usage text with `_subcommands describe <subcommand> <usage>`.
_subcommands describe "daily" <<HEREDOC
Usage:
nb daily [<content>]
Description:
Add notes to a daily log.
Examples:
nb daily "Example note content."
HEREDOC

# Define the subcommand as a function, named with a leading underscore.
_daily() {
local _content="${1:-}"

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

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

local _target_filename=
_target_filename="$(date "+%Y%m%d").md"

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:-}" ]]
then
_add --content "${_timestamped_content}" --filename "${_target_filename}"
else
_edit "${_target_filename}" --append "${_timestamped_content}"
fi
}
92 changes: 92 additions & 0 deletions test/plugin-daily.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env bats

load test_helper

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

@test "'nb daily' with no content exits with 1 and prints help." {
{
"${_NB}" init
"${_NB}" plugins install "${NB_TEST_BASE_PATH}/../plugins/daily.nb-plugin"
}

run "${_NB}" daily

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

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

@test "'nb daily <content>' without existing note adds new note with content." {
{
"${_NB}" init
"${_NB}" plugins install "${NB_TEST_BASE_PATH}/../plugins/daily.nb-plugin"

declare _target_filename=
_target_filename="$(date "+%Y%m%d").md"
}

run "${_NB}" daily "Example content."

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

[[ "${status}" == 0 ]]

"${_NB}" show 1 --print

[[ "${output}" =~ Added:\ .*[.*1.*].*\ .*${_target_filename} ]]
[[ "$("${_NB}" show 1 --print | wc -l | awk '$1=$1')" == 1 ]]
[[ "$("${_NB}" show 1 --print)" =~ \
\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\]\ Example\ content\. ]]

}

@test "'nb daily <content>' with existing note adds content to existing note." {
{
"${_NB}" init
"${_NB}" plugins install "${NB_TEST_BASE_PATH}/../plugins/daily.nb-plugin"

declare _target_filename=
_target_filename="$(date "+%Y%m%d").md"

"${_NB}" daily "Example initial content."
}

run "${_NB}" daily "Example new content."

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

[[ "${status}" == 0 ]]

"${_NB}" show 1 --print

[[ "${output}" =~ Updated:\ .*[.*1.*].*\ .*${_target_filename} ]]
[[ "$("${_NB}" show 1 --print | wc -l | awk '$1=$1')" == 2 ]]
[[ "$("${_NB}" show 1 --print | sed -n '1p')" =~ \
\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\]\ Example\ initial\ content\. ]]
[[ "$("${_NB}" show 1 --print | sed -n '2p')" =~ \
\[[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\]\ Example\ new\ content\. ]]
}

# help ########################################################################

@test "'help example' exits with status 0 and prints usage." {
{
"${_NB}" init
"${_NB}" plugins install "${NB_TEST_BASE_PATH}/../plugins/daily.nb-plugin"
}

run "${_NB}" help daily

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

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

0 comments on commit 14e9164

Please sign in to comment.