-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notes are saved with timestamps to a daily log. refs gh-300
- Loading branch information
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ]] | ||
} |