blob: a67ec0a09fa5423595831d54fb37ac3ed73ead4b (
plain)
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
|
#! /bin/bash
# set -ex
PID=$$
VERSION="0.1"
EDITOR=${EDITOR:-/usr/bin/vim}
BASEDIR=${BASEDIR:-~/.bash-notes}
DB=${BASEDIR}/db.json
TMPDB=/tmp/db.json
NOTESDIR=${BASEDIR}/notes
BASENAME=$( basename $0 )
TERMINAL=${TERMINAL:-/usr/bin/alacritty}
JQ=$(which jq)
if [ ! -x $JQ ]; then
echo "jq not found in your PATH"
echo "install jq to continue"
exit 1
fi
# We prevent the program from running more than one instance:
PIDFILE=/var/tmp/$(basename $0 .sh).pid
# Make sure the PID file is removed when we kill the process
trap 'rm -f $PIDFILE; exit 1' TERM INT
if [[ -r $PIDFILE ]]; then
# PIDFILE exists, so I guess there's already an instance running
# let's kill it and run again
kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
# should already be deleted by trap, but just to be sure
rm $PIDFILE
fi
# create PIDFILE
echo $PID > $PIDFILE
function helptext() {
echo "Parameters are:"
echo " -h : This help text"
echo " -s <directory> : specify directory where to store all notes."
echo " -e <editor> : specify EDITOR for this session only."
echo " -l : List existing notes"
echo " -a : Add new note"
echo " -m <note> : Modify note"
echo " -d <note> : Modify date for note"
echo " -r <note> : Remove note"
echo " -v : Print version"
}
function addnote() {
NOTETITLE=$1
echo "add new note"
NOW=$(date +%s)
FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T)
LASTID=$($JQ '.notes[-1].id' $DB)
[ null == $LASTID ] && LASTID=0
NOTEID=$(( $LASTID + 1 ))
touch ${NOTESDIR}/${NOW}
$JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
mv $TMPDB $DB
NEWNOTE=$(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${NOW})
if [[ $NEWNOTE ]]; then
echo "New note saved!"
fi
}
function listnotes() {
echo "list all notes"
}
function editnote() {
echo "edit note \"${1}\""
}
function datenote() {
echo "edit date for note \"${1}\""
}
function rmnote() {
echo "remove note"
}
function firstrun() {
mkdir -p $NOTESDIR
cat << "__EOL__" > $DB
{
"notes": []
}
__EOL__
}
# check for notes dir existance and create it in case it doesn't exists
if [[ ! -d $NOTESDIR ]]; then
# we don't have a directory. FIRST RUN?
firstrun
fi
# Command line parameter processing:
while getopts ":a:hlvm:s:n:e:r:d:" Option
do
case $Option in
h ) helptext
exit
;;
a ) TITLE=${OPTARG}
addnote $TITLE
;;
l ) listnotes
;;
m ) NOTE=${OPTARG}
editnote "${NOTE}"
;;
d ) NOTE=${OPTARG}
datenote "${NOTE}"
;;
r ) NOTE=${OPTARG}
rmnote "${NOTE}"
;;
e ) EDITOR=${OPTARG}
;;
s ) NOTESDIR=${OPTARG}
;;
v ) echo $BASENAME v${VERSION}
;;
* ) echo "You passed an illegal switch to the program!"
echo "Run '$0 -h' for more help."
exit
;; # DEFAULT
esac
done
# End of option parsing.
shift $(($OPTIND - 1))
# $1 now references the first non option item supplied on the command line
# if one exists.
# ---------------------------------------------------------------------------
|