-
Notifications
You must be signed in to change notification settings - Fork 15
/
jrnl
executable file
·90 lines (82 loc) · 2.78 KB
/
jrnl
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
#!/usr/bin/env bash
# This is a shell script to automate the process of creating day-based
# journal/diary entries
# Set some variables for use later
JRNLUID="$(date +%F)"
JRNLPATH="${HOME}/Sync/Journal"
FULLDATE="$(date '+%A, %B %d %Y')"
ENTRYTIME="$(date '+%I:%M %p')"
# Define some help text
# TODO: Expand the help text
HELP="Usage: jrnl new|edit"
# If the user supplied "--help", then provide help text
if [ "$1" = "--help" ]; then
echo -e "${HELP}\n"
exit 0
fi
# Set certain OS-specific variables
# TODO: Add support for other editors
case "${OSTYPE}" in
darwin*)
CODE="/usr/local/bin/code"
;;
linux*)
CODE="/usr/bin/code"
;;
*)
echo -e "This script is not supported on this operating system\n"
exit 1
;;
esac
# Test to ensure the user passed at least one parameter (the subcommand)
if [ ! "$#" -ge 1 ]; then
echo -e "No valid subcommand was provided, exiting\n"
echo -e "${HELP}\n"
exit 1
fi
# Parse the command line to get the subcommand
SUBCMD="$1"
# Switch into the journal directory for the remainder of the script
cd "${JRNLPATH}"
# Use the subcommand to either create a new entry or edit an existing entry
case "${SUBCMD}" in
new)
# Check to see if a file already exists for today
# If not, create new file and populate with initial content
# If so, append time as new heading for new entry
JRNLNAME="${JRNLUID}.md"
if [ -f "${JRNLPATH}/${JRNLNAME}" ]; then
# Echo the current time to the existing file, and open it in Code
echo -e "\n" >> "${JRNLNAME}"
echo -e "\n## ${ENTRYTIME}" >> "${JRNLNAME}"
${CODE} "${JRNLPATH}" -g "${JRNLNAME}"
else
# Create the file, populate with initial info, and open it in Code
touch "${JRNLNAME}"
echo -e "# Journal Entries for ${FULLDATE}\n" >> "${JRNLNAME}"
echo -e "## ${ENTRYTIME}\n" >> "${JRNLNAME}"
${CODE} "${JRNLPATH}" -g "${JRNLNAME}":5
fi
;;
edit)
${CODE} "${JRNLPATH}"
;;
old)
USERDATE="$2"
FILEDATE="$(date '+%F' -d ${USERDATE})"
OLDENTRYDATE="$(date '+%A, %B %d %Y' -d ${USERDATE})"
USERTIME="$3"
OLDENTRYTIME="$(date '+%I:%M %p' -d ${USERTIME})"
JRNLNAME="${FILEDATE}.md"
if [ -f "${JRNLPATH}/${JRNLNAME}" ]; then
# Echo an error; an entry already exists for this date
echo -e "An entry already exists for this date, exiting\n"
exit 1
else
touch "${JRNLNAME}"
echo -e "# Journal Entries for ${OLDENTRYDATE}\n" >> "${JRNLNAME}"
echo -e "## ${OLDENTRYTIME}\n" >> "${JRNLNAME}"
${CODE} "${JRNLPATH}" -g "${JRNLNAME}":5
fi
;;
esac