-
Notifications
You must be signed in to change notification settings - Fork 1
/
journ.sh
executable file
·337 lines (298 loc) · 9.52 KB
/
journ.sh
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#!/bin/bash
set -o nounset -o pipefail
# are set internally but exposed
DATE=""
ENTRY_NAME=""
FROM=""
TO=""
# can be overriden by conf later
THIS_DIR=$(dirname $(readlink -f $0))
JOURNAL_DIR="$HOME/.journeh"
TEMPLATE_NAME=".journ_template"
TEMPLATE_FILE=""
HOOK_FILE=""
HOOK=".hook"
FILE_EXT="md"
CONTEXT_BEFORE=1
CONTEXT_AFTER=1
# CONTEXT=1
AT_TOP=1
OLDEST_FIRST=0
# day 1 since, day 0 would break some of the logic
EPOCH=$(date -d "@1" +%F)
EDITOR="${EDITOR:-vim}"
MODIFIED_FILES=()
function find_file() {
find "${2}" -type f -name "${1}" | xargs ls -t | head -n 1
}
function localize_file() {
file_location="$( find_file "${1}" "${PWD}" )"
[ "_" != "_${file_location}" ] || file_location="$( find_file "${1}" "${JOURNAL_DIR}" )"
[ "_" != "_${file_location}" ] || file_location="$( find_file "${1}" "${THIS_DIR}" )"
echo "${file_location}"
}
function init_file() {
if [ ! -f "$JOURNAL_DIR/$1.${FILE_EXT}" ]; then
touch "$JOURNAL_DIR/$1.${FILE_EXT}"
if [ -f "${TEMPLATE_FILE}" ]; then
eval "echo \"$(cat ${TEMPLATE_FILE})\"" > "$JOURNAL_DIR/$1.${FILE_EXT}"
fi
fi
}
function clone_file() {
MODIFIED_FILES+=( "$JOURNAL_DIR/$1.${FILE_EXT}" )
printf "" > "$JOURNAL_DIR/$1.${FILE_EXT}.swap"
printf "" > "$JOURNAL_DIR/$1.${FILE_EXT}.modified"
[ -f "$JOURNAL_DIR/$1.${FILE_EXT}" ] &&\
cat "$JOURNAL_DIR/$1.${FILE_EXT}" > "$JOURNAL_DIR/$1.${FILE_EXT}.swap"
}
function write_back() {
filename=""
while IFS="" read -r lines || [ -n "$lines" ]
do
case "$lines" in
'<!--BEGIN-'*'-->')
lines=${lines/<\!--BEGIN-/}
lines=${lines/-->/}
filename="$JOURNAL_DIR/$lines.${FILE_EXT}"
;;
*)
echo "$lines" >> "$filename.modified"
;;
esac
done < "$1"
# now check if the modified files don't match the swap files, if so write back
wrote_changes=1
for modified_file in "${MODIFIED_FILES[@]}"; do
#returns false on diff, making the if a bit counter intuitive
if ! diff "${modified_file}.swap" "${modified_file}.modified" -q &>/dev/null; then
cat "${modified_file}.modified" > "${modified_file}"
wrote_changes=0
fi
rm "${modified_file}.modified" "${modified_file}.swap"
done
return "${wrote_changes}"
}
function load_entry() {
local _starts_at
{
[ -f "$JOURNAL_DIR/$1.${FILE_EXT}" ] && [ -n "$(tail -c1 "$JOURNAL_DIR/$1.${FILE_EXT}")" ] &&\
echo "";
echo "<!--BEGIN-$1-->";
[ -f "$JOURNAL_DIR/$1.${FILE_EXT}" ] &&\
cat "$JOURNAL_DIR/$1.${FILE_EXT}";
} >> "$2";
clone_file "$1"
}
function split() {
read -r input
delimiter="$*"
input="$input$delimiter"
while [[ $input ]]; do
echo "${input%%"$delimiter"*}"
input=${input#*"$delimiter"}
done
}
function parse_args() {
FROM="$(date +%F)"
TO="${FROM}"
case "_${1:-}" in
_)
;;
_todo)
# we had things since the beginning of time... let it show
DATE="${EPOCH}"
FROM="${EPOCH}"
TO="${EPOCH}"
;;
_day)
[ "_${*:2}" != "_" ] && DATE=$(date -d "${*:2}" +%F)
FROM=${DATE}
TO="${FROM}"
;;
_week)
[ "_${*:2}" != "_" ] && DATE=$(date -d "${*:2}" +%F)
FROM=$(date -d "$DATE - $(date -d "$DATE" +%u) days" +%F)
TO=$(date -d "$FROM + 6 days" +%F)
;;
_month)
[ "_${*:2}" != "_" ] && DATE=$(date -d "${*:2}" +%F)
local _days_in_month
_days_in_month=$(cal "$DATE" | awk 'NF {DAYS = $NF}; END {print DAYS}')
FROM=$(date -d "$DATE" +"%Y-%m-01")
TO=$(date -d "$DATE" +"%Y-%m-$_days_in_month")
;;
_quarter)
[ "_${*:2}" != "_" ] && DATE=$(date -d "${*:2}" +%F)
local _quarter_len
local _last_day_of_quarter
local _year
_quarter_len=91
_last_day_of_quarter=$(( _quarter_len * $(date -d "$DATE" +%q) ))
_year=$(date -d "$DATE" +"%Y")
TO=$(date -d "$_year-01-01 + $_last_day_of_quarter days" +"%F")
FROM=$(date -d "$TO - $_quarter_len days" +%F)
;;
_year)
[ "_${*:2}" != "_" ] && DATE=$(date -d "${*:2}" +%F)
local _year
_year=$(date -d "$DATE" +"%Y")
FROM=$(date -d "$_year-01-01" +"%F")
TO=$(date -d "$_year-12-31" +"%F")
;;
_*)
set -o errexit
case "_$*" in
_*' to '*)
mapfile -t values <<< "$(echo "$*" | split " to ")"
FROM="$(date -d"${values[0]}" +%F)"
TO="$(date -d"${values[1]}" +%F)"
;;
_*' from '*)
mapfile -t values <<< "$(echo "$*" | split " from ")"
FROM="$(date -d"${values[0]}" +%F)"
TO="$(date -d"$DATE ${values[1]}" +%F)"
;;
_*)
FROM="$(date -d"$*" +%F)"
TO="$FROM"
;;
esac
set +o errexit
;;
esac
}
function journeh() {
git ls-remote &> /dev/null
REMOTE=$?
# convert to second
FROM=$(date -d"${FROM}" +%s)
TO=$(date -d"${TO}" +%s)
if (( FROM > TO ));
then
local _tmp_date
_tmp_date=$FROM
FROM=$TO
TO=$_tmp_date
fi
local _diff_days
_diff_days=$(( ( TO - FROM ) / 86400 ))
# convert back
FROM=$(date -d"@$FROM" +%F)
TO=$(date -d"@$TO" +%F)
local _context_before_end
local _context_after_start
_context_before_end=1
_context_after_start=1
if [ "_${_diff_days}" == "_0" ];
then
DATE="${FROM}"
ENTRY_NAME="${DATE}"
else
# override context
if [[ "_$AT_TOP" == "_$OLDEST_FIRST" ]]
then
CONTEXT_BEFORE="${_diff_days}"
_context_before_end=0
DATE="${TO}"
CONTEXT_AFTER=0
else
CONTEXT_BEFORE=0
DATE="${FROM}"
CONTEXT_AFTER="${_diff_days}"
_context_after_start=0
fi
ENTRY_NAME="${FROM}_to_${TO}"
fi
TEMPLATE_FILE="$(localize_file "${TEMPLATE_NAME}.*")"
HOOK_FILE="$(localize_file "${HOOK}")"
FILE_EXT=${TEMPLATE_FILE##*.}
local _tmp_file
_tmp_file=$(mktemp --suffix=".${FILE_EXT}")
# sync with remote
[[ -z $REMOTE ]] && git pull
if [ "$OLDEST_FIRST" == "0" ];
then
# import all the previous context
for (( i = CONTEXT_BEFORE; i >= _context_before_end; i -= 1 ));
do
load_entry "$(date -d "$DATE - $i days" +%F)" "$_tmp_file"
done
else
# import all the previous context
for (( i = CONTEXT_AFTER; i >= _context_after_start; i -= 1 ));
do
load_entry "$(date -d "$DATE + $i days" +%F)" "$_tmp_file"
done
fi
if [ -t 0 ] && [ -t 1 ]; then
init_file "$ENTRY_NAME"
else
# if we dont have stdin or stdout then don't run interactive
EDITOR=cat
fi
load_entry "$ENTRY_NAME" "$_tmp_file"
if [ "$OLDEST_FIRST" == "0" ];
then
# import all the previous context
for (( i = _context_after_start; i <= CONTEXT_AFTER; i += 1 ));
do
load_entry "$(date -d "$DATE + $i days" +%F)" "$_tmp_file"
done
else
# import all the previous context
for (( i = _context_before_end; i <= CONTEXT_BEFORE; i += 1 ));
do
load_entry "$(date -d "$DATE - $i days" +%F)" "$_tmp_file"
done
fi
# open for changes and writeback on success
"${EDITOR}" "$_tmp_file"
if write_back "$_tmp_file"; then
# push sync with remote
git add . &> /dev/null
git commit -m "update $ENTRY_NAME" &> /dev/null
[[ -z $REMOTE ]] && git push
if [ -x "${HOOK_FILE}" ]; then
${HOOK_FILE}
fi
echo "Upated journeh"
fi
}
if [ -d "$JOURNAL_DIR" ]
then
cd "$JOURNAL_DIR"
elif [ "_$1" != "_init" ]
then
echo "Your journal directory has not been initialized, please run 'init <repo url>' first"
fi
case "_${1:-}" in
_init)
mkdir -p "$JOURNAL_DIR"
pushd "$JOURNAL_DIR"
[ ! -d .git ] && git init
git ls-remote &> /dev/null && git remote remove origin
[ "_$2" != "_" ] && git remote add origin "$2"
;;
_help)
echo "
Usage:
init <git url> this will init the repo for remote backup
todo create/update a rolling todo list
day [date -d arguments] create a daily journal entry
week [date -d arguments] create a weekly-end journal entry
month [date -d arguments] create a month-end journal entry
quarter [date -d arguments] create a quarter-end journal entry
year [date -d arguments] create a year-end journal entry
[date -d arguments] to [date -d arguments] create a ranged journal entry
[date -d arguments] from [date -d arguments] create a relative from ranged journal entry
[date -d arguments] create a daily journal entry for date
<blank> create a daily journal entry for today
help this
"
;;
*)
parse_args "${@}"
journeh
;;
esac