-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournal
More file actions
executable file
·300 lines (264 loc) · 6.47 KB
/
journal
File metadata and controls
executable file
·300 lines (264 loc) · 6.47 KB
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
#!/usr/bin/env bash
# Daily Journal Script
# Author: Ben Borla
# Email: benborla@icloud.com
# License: MIT
# Description: A simple script to manage daily journal entries
set -euo pipefail
# @INFO: Default Configuration
DEFAULT_JOURNAL_DIR=~/journal
DEFAULT_DATE_FORMAT="%m-%d-%Y"
DEFAULT_EDITOR=vim
# @INFO: Configuration file
CONFIG_FILE=~/.journal_config
load_config() {
if [[ -f "$CONFIG_FILE" ]]; then
source "$CONFIG_FILE"
fi
JOURNAL_DIR=${JOURNAL_DIR:-$DEFAULT_JOURNAL_DIR}
DATE_FORMAT=${DATE_FORMAT:-$DEFAULT_DATE_FORMAT}
EDITOR=${EDITOR:-$DEFAULT_EDITOR}
}
save_config() {
cat >"$CONFIG_FILE" <<EOF
JOURNAL_DIR="$JOURNAL_DIR"
DATE_FORMAT="$DATE_FORMAT"
EDITOR="$EDITOR"
EOF
echo "Configuration saved to $CONFIG_FILE"
}
list_config() {
printf "Current configuration:\n\n"
echo "JOURNAL_DIR=$JOURNAL_DIR"
echo "DATE_FORMAT=$DATE_FORMAT"
echo "EDITOR=$EDITOR"
}
edit_config() {
if [[ ! -f "$CONFIG_FILE" ]]; then
save_config
fi
$EDITOR "$CONFIG_FILE"
echo "Configuration file edited. Reloading settings..."
load_config
printf "\nSettings reloaded. Current configuration:\n\n"
list_config
}
show_help() {
cat <<EOF
Usage: ${0##*/} [OPTIONS] [DATE]
Manage your daily journal entries.
Options:
-h, --help Display this help message and exit
-v, --version Display version information and exit
--list List all journal entries
--set-journal-dir DIR Set the journal directory
--set-editor EDITOR Set the preferred editor
--list-config Prints all the set configuration
--edit-config Modify the entire configuration
DATE can be:
- Empty (default to today)
- 'yesterday'
- A specific date in $DATE_FORMAT format
Examples:
${0##*/}
${0##*/} yesterday
${0##*/} 10-18-2024
${0##*/} --list
${0##*/} --set-journal-dir ~/my_journals
${0##*/} --set-editor vim
EOF
}
show_version() {
echo "${0##*/} version 1.0.0"
}
get_date() {
local input_date="$1"
if [ -z "$input_date" ]; then
date +"$DATE_FORMAT"
elif [ "$input_date" = "yesterday" ]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
date -v-1d +"$DATE_FORMAT"
else
date -d "yesterday" +"$DATE_FORMAT"
fi
else
if [[ "$OSTYPE" == "darwin"* ]]; then
date -j -f "$DATE_FORMAT" "$input_date" +"$DATE_FORMAT" 2>/dev/null || {
echo "Error: Invalid date format. Please use $DATE_FORMAT or 'yesterday'." >&2
exit 1
}
else
date -d "$input_date" +"$DATE_FORMAT" 2>/dev/null || {
echo "Error: Invalid date format. Please use $DATE_FORMAT or 'yesterday'." >&2
exit 1
}
fi
fi
}
# Function to format date for display (Month DD, YYYY)
format_date_for_display() {
local date_string="$1"
if [[ "$OSTYPE" == "darwin"* ]]; then
date -j -f "$DATE_FORMAT" "$date_string" "+%B %d, %Y"
else
date -d "$date_string" "+%B %d, %Y"
fi
}
create_journal_entry() {
local journal_file="$1"
local entry_date="$2"
cat <<EOF >"$journal_file"
Filename: $(basename "$journal_file")
# Daily Journal - $entry_date
## Morning Reflection
### Gratitude
Three things I'm grateful for today:
1.
2.
3.
### Meditation
- [ ] I've completed a 5-minute meditation
### Yesterday's Review
- How I did yesterday:
- Who I was yesterday:
- Were my actions yesterday in line with my goals?
- [ ] Yes
- [ ] No
- If no, what can I improve?
## Today's Intentions
### Who I'm Going to Be Today
I intend to embody the following qualities today:
-
-
-
### My Intentions for Today
Top 3 priorities:
1.
2.
3.
## Evening Reflection
(Fill this out at the end of your day)
### Today's Achievements
-
### Lessons Learned
-
### Tomorrow's Focus
-
---
Remember: "I am capable of greatness, and I take purposeful action each day to manifest my potential."
EOF
echo "Created new journal entry for $entry_date."
}
# Function to add filename to existing journal entry
add_filename_to_entry() {
local journal_file="$1"
local temp_file
temp_file=$(mktemp)
if ! grep -q "^Filename: " "$journal_file"; then
echo "Filename: $(basename "$journal_file")" >"$temp_file"
echo "" >>"$temp_file" # Add a blank line after the filename
cat "$journal_file" >>"$temp_file"
mv "$temp_file" "$journal_file"
else
rm "$temp_file"
fi
}
list_entries() {
printf "Journal entries in $JOURNAL_DIR:\n\n"
local entry_count=0
for entry in "$JOURNAL_DIR"/*.md; do
if [[ -f "$entry" ]]; then
local filename=$(basename "$entry")
local date_part="${filename%.*}"
if [[ $date_part =~ ^[0-9]{2}-[0-9]{2}-[0-9]{4}$ ]]; then
local formatted_date=$(format_date_for_display "$date_part")
echo "- $formatted_date"
((entry_count++))
fi
fi
done
if [[ $entry_count -eq 0 ]]; then
echo "No journal entries found."
else
printf "\nTotal entries: $entry_count\n"
fi
}
# Main function
main() {
load_config
local date_arg=""
# Parse command-line options
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
show_help
exit
;;
-v | --version)
show_version
exit
;;
--list)
list_entries
exit
;;
--set-journal-dir)
if [[ -n "$2" ]]; then
JOURNAL_DIR="$2"
save_config
echo "Journal directory set to: $JOURNAL_DIR"
exit
else
echo "Error: --set-journal-dir requires a directory path." >&2
show_help
exit 1
fi
;;
--set-editor)
if [[ -n "$2" ]]; then
EDITOR="$2"
save_config
echo "Editor set to: $EDITOR"
exit
else
echo "Error: --set-editor requires an editor name." >&2
show_help
exit 1
fi
;;
--edit-config)
edit_config
exit
;;
--list-config)
list_config
exit
;;
--*)
echo "Error: Unknown option $1" >&2
show_help
exit 1
;;
*)
date_arg=$1
break
;;
esac
shift
done
# Get the date and create the journal file path
DATE=$(get_date "$date_arg")
JOURNAL_FILE="$JOURNAL_DIR/$DATE.md"
# Create the journal directory if it doesn't exist
mkdir -p "$JOURNAL_DIR"
# Create or load the journal entry
if [ ! -f "$JOURNAL_FILE" ]; then
create_journal_entry "$JOURNAL_FILE" "$(format_date_for_display "$DATE")"
else
echo "Loading journal entry for $(format_date_for_display "$DATE")."
add_filename_to_entry "$JOURNAL_FILE"
fi
# Open the file with the specified editor
"$EDITOR" "$JOURNAL_FILE"
}
main "$@"