-
-
Notifications
You must be signed in to change notification settings - Fork 181
/
daily.nb-plugin
149 lines (120 loc) · 3.97 KB
/
daily.nb-plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/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
$(_color_primary "Usage"):
${_ME} daily [<content>] [--prev [<number>]]
$(_color_primary "Options"):
--prev [<number>] List previous days and show day by previous <number>.
$(_color_primary "Description"):
Add notes to a daily log. When called without arguments, the current day's
log is displayed. When passed \`<content>\`, a new timestamped entry is added
to the current day's log, which is created if it doesn't yet exist.
Previous day's logs can be listed with the \`--prev\` option. View a previous
day's log by passing its \`<number>\` in the list.
$(_color_primary "Examples"):
${_ME} daily "Example note content."
${_ME} daily
${_ME} daily --prev
${_ME} 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=
_notebook_path="$(_notebooks current --path)"
local _target_filename=
_target_filename="$(date "+%Y%m%d").md"
local _target_path=
_target_path="${_notebook_path}/${_target_filename}"
if [[ -z "${_content:-}" ]]
then
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:-}" -ge "${#_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
{
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:-}" ]]
then
_add --content "${_timestamped_content}" --filename "${_target_filename}"
else
_edit "${_target_filename}" --append "${_timestamped_content}"
fi
fi
}