-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathnoted
executable file
·171 lines (145 loc) · 3.42 KB
/
noted
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
#!/bin/bash
set -e
#
# START FUNCTIONS (alphabetical order)
#
checkForEnvVariable() {
varval=${!1}
if [ ! "$(echo $varval)" ]; then
cat <<EOF
ERROR
A required environment variable is not available:
$1
EOF
exit 1
fi
}
create() {
newEntry() {
headerText="$@"
result="${TEMPLATE_TEXT}"
result="${result/HEADERTEXT/$headerText}"
result="${result/TIMESTAMP/$timestamp}"
echo -e "\n$result\n"
}
TEMPLATE_TEXT=""
if [ -f "${NOTED_TEMPLATE_FILE}" ]; then
# use template file
TEMPLATE_TEXT=$(<"${NOTED_TEMPLATE_FILE}")
else
read -r -d '' TEMPLATE_TEXT <<EOF || true
---
TIMESTAMP
# HEADERTEXT
---
EOF
fi
mkdir -p "${NOTED_MARKDOWN_HOME}"
markdownFile "${currDate}"
if [[ -n "${OPTIONAL_FILE}" ]]; then
markdownFile="${NOTED_MARKDOWN_HOME}/${OPTIONAL_FILE}"
fi
touch "${markdownFile}"
if [ -n "${OPTIONAL_NOTE}" ]; then
# Supplying a note means automatically add that to the file
newEntry "${OPTIONAL_NOTE}" >>"${markdownFile}"
else
# otherwise open it up with the template and let them edit
newEntry >>"${markdownFile}"
edit "${markdownFile}"
fi
}
edit() {
open "${markdownFile}"
}
loadProperties() {
if [[ -f "${CONFIG_FILE}" ]]; then
source "${CONFIG_FILE}"
fi
}
markdownFile() {
markdownFile="${NOTED_MARKDOWN_HOME}/${1}.md"
}
ngrep() {
grep --recursive "$@" "${NOTED_MARKDOWN_HOME}"
}
outputConfig() {
echo "The following configuration is currently being used:"
echo "NOTED_MARKDOWN_HOME=${NOTED_MARKDOWN_HOME}"
echo "NOTED_FILE_NAME_DATE_FORMAT=${NOTED_FILE_NAME_DATE_FORMAT}"
echo "NOTED_TIMESTAMP_FORMAT=${NOTED_TIMESTAMP_FORMAT}"
echo "NOTED_TEMPLATE_FILE=${NOTED_TEMPLATE_FILE:-<<DEFAULT>>}"
echo "NOTED_TODO_MARKER=${NOTED_TODO_MARKER}"
}
validate() {
if [ -z "$1" ]; then
echo "${2}"
exit 1
fi
}
version() {
echo "noted v${VERSION}"
}
view() {
open "${htmlFile}"
}
#
# END FUNCTIONS
#
VERSION="0.0.3"
#
# Default Settings
#
# Settings that cannot be overridden
# Location of config file
CONFIG_FILE=$HOME/.notedconfig
# Settings that can be overridden from properties
# name files like this
NOTED_FILE_NAME_DATE_FORMAT="+%Y-%m-%d"
# timestamp entries within the files like this
NOTED_TIMESTAMP_FORMAT="+%H:%M:%S UTC"
NOTED_TEMPLATE_FILE=""
# keep the directory where notes are stored here
NOTED_MARKDOWN_HOME=$HOME/Documents/notes
NOTED_TODO_MARKER="TODO"
#
# Override with any additional settings
#
loadProperties
currDate=$(date "${NOTED_FILE_NAME_DATE_FORMAT}")
timestamp=$(date "${NOTED_TIMESTAMP_FORMAT}")
todaysFile="${NOTED_MARKDOWN_HOME}/${currDate}.md"
subcommand="$1"
if [[ "${subcommand}" == "config" ]]; then
outputConfig
elif [[ "${subcommand}" == "version" ]]; then
version
elif [[ "${subcommand}" == "edit" ]] || [[ "${subcommand}" == "view" ]]; then
arg="$2"
if [[ -z "${arg}" ]]; then
arg="${currDate}"
fi
markdownFile "${arg}"
edit "${markdownFile}"
elif [[ "${subcommand}" == "grep" ]]; then
shift
ngrep "$@"
elif [[ "${subcommand}" == 'todos' ]]; then
fgrep -rH "${NOTED_TODO_MARKER}" "${NOTED_MARKDOWN_HOME}" | fgrep -v ~~
if [ 1 == $? ]; then echo "Congratulations! No TODOs found!"; fi
echo
else
# create / default
OPTIONAL_FILE=""
OPTIONAL_NOTE=""
if [[ "${subcommand}" == "create" ]]; then
shift
fi
if [[ -n "$1" ]]; then
OPTIONAL_NOTE="$1"
fi
if [[ -n "$2" ]]; then
OPTIONAL_FILE="$2"
fi
create "${OPTIONAL_NOTE}" "${OPTIONAL_FILE}"
fi