# Binaries to use
EDITOR=${EDITOR:-/usr/bin/vim}
TERMINAL=${TERMINAL:-/usr/bin/alacritty}
+# add options for your terminal. Remember to add the last option to execute
+# your editor program, otherwise the script will fail.
+# see example in the addnote function
+TERM_OPTS="--class notes --title notes -e "
JQ=${JQ:-/usr/bin/jq}
# base directory for program files
# create PIDFILE
echo $PID > $PIDFILE
+# check if input is a number, returns false or the number itself
+function check_noteID() {
+ IN=$1
+ case $IN in
+ ''|*[!0-9]*)
+ return 1
+ ;;
+ *)
+ echo $IN
+ ;;
+ esac
+}
+
function helptext() {
echo "Usage:"
- echo " $0 [OPTION] ..."
+ echo " $0 [PARAMS] ..."
echo ""
cat << __NOWCONF__
${BASENAME} configuration is:
notes archive: ${NOTESDIR}/
notes database: ${DB}
rc file: $RCFILE
+
text editor: ${EDITOR}
terminal: ${TERMINAL}
jq executable: ${JQ}
__NOWCONF__
echo ""
- echo "The script's parameters are:"
+ echo "${BASENAME} parameters are:"
echo " -h | --help : This help text"
+ echo " -p | --plain : Output is in plain text"
+ echo " (without this option the output is formatted)"
+ echo " (this option must precede all others)"
echo " -l | --list : List existing notes"
echo " -a | --add <title> : Add new note"
echo " -m | --modify <note> : Modify note"
- echo " -d | -date <note> : Modify date for note"
- echo " -r | --remove <note> : Remove note"
+ echo " -d | --delete <note> : Delete note"
echo " -v | --version : Print version"
echo " --userconf : Export User config file"
+ echo ""
}
function addnote() {
NOTETITLE="$1"
echo "adding new note - \"$NOTETITLE\""
- LASTID=$($JQ '.notes[-1].id | tonumber' $DB)
- [ null == $LASTID ] && LASTID=0
+ LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
+ # [ "" == $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
- $(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${NOW})
+ # example for alacritty:
+ # alacritty --class notes --title notes -e /usr/bin/vim ...
+ $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
}
function listnotes() {
- echo "listing all notes"
- echo ""
- echo "[ID] [TITLE]"
- for i in ${NOTESDIR}/*; do
- TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
- ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
-
- echo "[${ID}] ${TITLE}"
- done
+ # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
+ if [[ $(ls -A $NOTESDIR) ]]; then
+ if [ $PLAIN == false ]; then
+ echo "listing all notes"
+ echo ""
+ fi
+ [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]"
+ for i in ${NOTESDIR}/*; do
+ local fname=$(basename $i)
+ DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
+ TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
+ ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
+ [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
+ done
+ else
+ echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
+ fi
}
function editnote() {
- TITLE=$($JQ --arg i $1 '.notes[] | select(.id == $i) | .title' $DB)
- FILE=$($JQ -r --arg i $1 '.notes[] | select(.id == $i) | .file' $DB)
+ NOTE=$1
+ local OK=$(check_noteID $NOTE)
+ if [ ! $OK ]; then
+ echo "invalid note \"$NOTE\""
+ exit 1
+ fi
+
+ TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
+ FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
if [ "$TITLE" ]; then
echo "editing note $TITLE"
- $(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${FILE})
+ $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
else
echo "note not found"
exit 1
fi
}
-function datenote() {
- echo "edit date for note \"${1}\""
- # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T)
-
-}
-
function rmnote() {
NOTE=$1
- echo "removing note $NOTE"
+ local OK=$(check_noteID $NOTE)
+ if [ ! $OK ]; then
+ echo "invalid note \"$NOTE\""
+ exit 1
+ fi
+
+ TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
+ FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
+ if [ "$TITLE" ]; then
+ $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
+ mv $TMPDB $DB
+ rm $NOTESDIR/$FILE
+ echo "Deleted note $TITLE"
+ else
+ echo "note not found"
+ exit 1
+ fi
}
function export_config() {
# NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
# separately; see below.
-GOPT=`getopt -o hvla:m:d:r: --long help,version,list,userconf,add:,modify:,date:,remove:,editor:,storage: \
+GOPT=`getopt -o hvpla:m:d: --long help,version,list,plain,userconf,add:,modify:,delete:,editor:,storage: \
-n 'bash-notes' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$GOPT': they are essential!
eval set -- "$GOPT"
+PLAIN=false
+
while true; do
case "$1" in
-h | --help )
echo $BASENAME v${VERSION}
exit
;;
+ -p | --plain )
+ PLAIN=true
+ shift
+ ;;
-l | --list )
listnotes
exit
shift 2
editnote "$NOTE"
;;
- -d | --date )
- NOTE="$2"
- shift 2
- datenote "$NOTE"
- ;;
- -r | --remove )
+ -d | --delete )
NOTE="$2"
shift 2
rmnote "$NOTE"
exit
;;
-- )
- shift; break
+ helptext
+ exit
;;
* )
break