-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneovim-weekly-review.sh
executable file
·175 lines (142 loc) · 4.2 KB
/
neovim-weekly-review.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
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
: "${ME:=$HOME/Me}"
week_no=${1:-$(date +%V)}
year=${2:-$(date +%Y)}
week_range() {
# Thanks to https://stackoverflow.com/a/61733557/1197008
local _u _F _V
# dow Jan 01 (Mon 01 ... Sun 07)
_u="$(date -d "$1-01-01" "+%u")"
# First Monday
_F="$(date -d "$1-01-01 + $(( (8 - _u) % 7)) days" "+%F")"
# Week number of first Monday
_V="$(date -d "$_F" "+%V")"
printf -- "%s %s\n" "$(date -d "$_F + $(( 7*($2 - _V) )) days" "+%F")" \
"$(date -d "$_F + $(( 7*($2 - _V) + 6 )) days" "+%F")"
}
count_notes() {
local path=$1
if [ ! -f "$path" ]; then
echo 0
else
grep -c -E '^\.\. ' "$path"
fi
}
count_positive_notes() {
local path=$1
if [ ! -f "$path" ]; then
echo 0
else
grep -c -E '^\.\+ ' "$path"
fi
}
count_negative_notes() {
local path=$1
if [ ! -f "$path" ]; then
echo 0
else
grep -c -E '^\.- ' "$path"
fi
}
positive_notes() {
local path=$1
if [ -f "$path" ]; then
grep -E '^\.\+ ' "$path"
fi
}
learnittoday() {
local path=$1
if [ -f "$path" ]; then
grep -E '^.. #til' "$path"
fi
}
key_notes() {
# notes to report in weekly regardless being neutral, positive or negative
local path=$1
if [ -f "$path" ]; then
grep -E 'KEY:' "$path"
fi
}
# TODO: that might be wrong, but on 2025-01-01 `date +%W` returns "00", while
# the right value is 01
if [ "$week_no" = "00" ]; then
week_no="01"
fi
# Remove leading 0
week_no=${week_no#0}
start_date=$(week_range "$year" "$week_no" | cut -d" " -f1)
end_date=$(week_range "$year" "$week_no" | cut -d" " -f2)
ONE_DAY_IN_SECONDS=86400
end_date_sec=$(date -d "$end_date" +%s)
if [ -n "$start_date" ]; then
start_date_sec=$(date -d "$start_date" +%s)
else
# default to a 7 days window
ONE_WEEK_IN_SECONDS=$((ONE_DAY_IN_SECONDS * 6))
start_date_sec=$((end_date_sec - ONE_WEEK_IN_SECONDS))
fi
NOTE_PATH=$ME/Notes/Journal
FILE_NAME=$(date -d @"$start_date_sec" +%Y-%m-W%V).md
WEEKLY_PATH=$NOTE_PATH/$FILE_NAME
FILE_NAME="$(date +%Y)-$(date +%m).md"
MONTHLY_PATH=$NOTE_PATH/$FILE_NAME
# Monthly review
{
echo "# $(date +'%Y %B') review"
for day in $(seq -w 1 31); do
out=$(grep "#weeklyreview" "$NOTE_PATH/$(date +%Y)-$(date +%m)-$day.md" 2>/dev/null)
if [ $? -eq 0 ]; then
dd="$(date +%Y)-$(date +%m)-$day"
echo ""
echo "[[$(date -d "$dd" +'%Y-%m-%d')]] $(date -d "$dd" +%A)"
echo "$out" | awk -F"weeklyreview" '{print $2}'
fi
done
} > "$NOTE_PATH/$(date +%Y)-$(date +%m).md"
# Just logging
echo "updating review between $(date -d @"$start_date_sec" +%F) and $(date -d @"$end_date_sec" +%F) in Week:$(basename $WEEKLY_PATH) and Month:$FILE_NAME files"
# Here start write the file from scratch (note the override ">" at the end of section)
{
# Weekly note header
echo "# Week $(date -d @"$start_date_sec" +%V) ($(date -d @"$start_date_sec" +%F) - $(date -d @"$end_date_sec" +%F)) review"
echo ""
echo ""
} > "$WEEKLY_PATH"
{
# Weekly goals
echo "## Week goals"
task weekly_goals
echo ""
echo "---"
echo ""
} >> "$WEEKLY_PATH"
# Weekly readitlater
readitlater-report.py >> "$WEEKLY_PATH"
echo ""
# Weekly review
week_notes=0
week_notes_pos=0
week_notes_neg=0
current=$start_date_sec
while [ "$current" -le "$end_date_sec" ]; do
day=$(date -d "@$current" +%F)
normal=$(count_notes "$NOTE_PATH/$day.md")
pos=$(count_positive_notes "$NOTE_PATH/$day.md")
neg=$(count_negative_notes "$NOTE_PATH/$day.md")
all=$((normal + pos + neg))
week_notes=$((week_notes + all))
week_notes_pos=$((week_notes_pos + pos))
week_notes_neg=$((week_notes_neg + neg))
echo "" >> "$WEEKLY_PATH"
echo [["$day"]]: "$all" notes, "$pos"+, "$neg"- >> "$WEEKLY_PATH"
if [ "$pos" -gt 0 ]; then
positive_notes "$NOTE_PATH/$day.md" >> "$WEEKLY_PATH"
fi
learnittoday "$NOTE_PATH/$day.md" >> "$WEEKLY_PATH"
key_notes "$NOTE_PATH/$day.md" >> "$WEEKLY_PATH"
current=$((current + ONE_DAY_IN_SECONDS))
done
echo "" >> "$WEEKLY_PATH"
echo "Overall: $week_notes notes, $week_notes_pos positives, $week_notes_neg negatives" | tee -a "$WEEKLY_PATH"
echo ""; echo "" >> "$WEEKLY_PATH"