-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqnote
executable file
·154 lines (123 loc) · 3.75 KB
/
qnote
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
#!/usr/bin/env bash
#
# ___ _ _ _
# / _ \| \ | | ___ | |_ ___
# | | | | \| |/ _ \| __/ _ \
# | |_| | |\ | (_) | || __/
# \__\_\_| \_|\___/ \__\___|
#
# A quick note taking app
# v0.0.5 - https://github.com/andresemartinez/qnote
#
# Configurations ------------------------------------------------------------------------
## Notes directory.
QNOTE_DIR="${QNOTE_DIR:-$HOME/notes}"
## Notes file extension.
QNOTE_EXTENSION="${QNOTE_EXTENSION:-md}"
## Notes editor of choice.
QNOTE_EDITOR="${QNOTE_EDITOR:-$EDITOR}"
## Notes reader of choice.
QNOTE_READER="${QNOTE_READER:-cat}"
# Helper functions ----------------------------------------------------------------------
normalize_note_name() {
NOTE_NAME="$1"
NOTE_NAME="${NOTE_NAME// /-}"
NOTE_NAME="${NOTE_NAME,,}"
echo "$NOTE_NAME"
}
validate_note_name() {
[ -z "$1" ] && { echo "Note name required :S"; exit 1; }
}
build_note_file_name() {
NOTE_NAME="$1"
NORMALIZED_NOTE_NAME="$(normalize_note_name "$NOTE_NAME")"
echo "${NORMALIZED_NOTE_NAME%*.$QNOTE_EXTENSION}.$QNOTE_EXTENSION"
}
build_note_path() {
NOTE_NAME="$1"
NOTE_FILE_NAME="$(build_note_file_name "$NOTE_NAME")"
echo "$QNOTE_DIR/$NOTE_FILE_NAME"
}
suffixed_note_name() {
NOTE_NAME="$1"
SUFFIX="$2"
[ "$SUFFIX" -gt 0 ] && NOTE_NAME="${NOTE_NAME//.$QNOTE_EXTENSION/_$SUFFIX.$QNOTE_EXTENSION}"
echo "$NOTE_NAME"
}
print_invalid_option_message() {
[ -z "$1" ] || echo "Invalid option ($1)"
}
# Action functions ----------------------------------------------------------------------
list_notes() {
FILTER_CRITERIA="${1:+-name '*$1*'}"
eval "find $QNOTE_DIR $FILTER_CRITERIA -type f -printf \"%TY-%Tm-%Td %TH:%TM - %P\\n\"" | sort -r
}
edit_note() {
NOTE_NAME="$1"
validate_note_name "$NOTE_NAME"
$QNOTE_EDITOR "$(build_note_path "$NOTE_NAME")"
}
delete_note() {
NOTE_NAME="$1"
validate_note_name "$NOTE_NAME"
rm "$(build_note_path "$NOTE_NAME")"
}
create_note() {
NOTE_NAME="$1"
NOTE_TITLE="$1"
NOTE_BODY="$2"
NOTE_NAME="$(build_note_path "$NOTE_NAME")"
SUFFIX=0
while [ -f "$(suffixed_note_name "$NOTE_NAME" "$SUFFIX")" ]
do
SUFFIX=$((SUFFIX + 1))
done
NOTE_NAME="$(suffixed_note_name "$NOTE_NAME" "$SUFFIX")"
echo -ne "# $NOTE_TITLE\n$NOTE_BODY" > "$NOTE_NAME"
}
search_in_notes() {
SEARCH_CRITERIA="$1"
ESCAPED_QNOTE_DIR="${QNOTE_DIR//\//\\\/}"
grep -iR "$SEARCH_CRITERIA" "$QNOTE_DIR" | sed -e "s/^$ESCAPED_QNOTE_DIR\\///"
}
copy_note() {
NOTE_NAME="$1"
validate_note_name "$NOTE_NAME"
wl-copy < "$(build_note_path "$NOTE_NAME")"
}
read_note() {
NOTE_PATH="$(build_note_path "$1")"
$QNOTE_READER "$NOTE_PATH"
}
print_help() {
echo "Help!"
echo "- l, list : List notes"
echo "- e, edit : Create/Edit note"
echo "- d, delete : Delete note"
echo "- i, inline : Create note inline"
echo "- c, cat : Print note"
echo "- s, search : Search in notes"
echo "- cp, copy : Copy note to clipboard"
echo "- h, help : Print help"
echo "- v, version : Print version"
}
print_version() {
echo "QNote - v0.0.5"
}
handle_invalid_option() {
print_invalid_option_message "$1"
print_help
exit 1
}
case "$1" in
l | list ) list_notes "$2" ;;
e | edit ) edit_note "$2" ;;
d | delete ) delete_note "$2" ;;
i | inline ) create_note "$2" "$3" ;;
c | cat ) read_note "$2" ;;
s | search ) search_in_notes "$2" ;;
cp | copy ) copy_note "$2" ;;
h | help ) print_help ;;
v | version ) print_version ;;
* ) handle_invalid_option "$1" ;;
esac