X-Git-Url: https://git.danix.xyz/?a=blobdiff_plain;f=notes.sh;h=eba388f05287913e7ff31d484a87d4def21995b9;hb=6c152f7e8cedde83ec98d784fc639f41990c60f7;hp=edec94d5f54d50d6951af2f703f6829cc5b3c8b8;hpb=44abbfe7d30b1a3460500aef77bc6f4b7b3b95fa;p=bash-notes.git diff --git a/notes.sh b/notes.sh index edec94d..eba388f 100644 --- a/notes.sh +++ b/notes.sh @@ -1,5 +1,8 @@ #! /bin/bash +# bash-notes © 2023 by danix is licensed under CC BY-NC 4.0. +# To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/ + # set -ex PID=$$ @@ -9,6 +12,10 @@ set_defaults() { # 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 @@ -56,9 +63,22 @@ fi # 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: @@ -67,68 +87,126 @@ base directory: ${BASEDIR}/ 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 : Add new note" - echo " -m | --modify <note> : Modify note" - echo " -d | -date <note> : Modify date for note" - echo " -r | --remove <note> : Remove note" + echo " -a | --add [\"<title>\"] : Add new note" + echo " -e | --edit [<note>] : Edit note" + echo " -d | --delete [<note> | all] : Delete single note or all notes at once" echo " -v | --version : Print version" echo " --userconf : Export User config file" + echo "" } function addnote() { + # remove eventually existing temp DB file + if [[ -f $TMPDB ]]; then + rm $TMPDB + fi + 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() { + # remove eventually existing temp DB file + if [[ -f $TMPDB ]]; then + rm $TMPDB + fi + NOTE=$1 - echo "removing note $NOTE" + if [ "all" == $NOTE ]; then + echo "You're going to delete all notes." + read -r -p "Do you wish to continue? (y/N) " ANSWER + case $ANSWER in + y|Y ) + $JQ 'del(.notes[])' $DB > $TMPDB + mv $TMPDB $DB + rm $NOTESDIR/* + echo "Deleted all notes" + ;; + * ) + echo "Aborting, no notes were deleted." + exit 1 + ;; + esac + else + 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 + fi } function export_config() { @@ -197,7 +275,7 @@ fi # 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:e:d: --long help,version,list,plain,userconf,add:,edit:,delete:,editor:,storage: \ -n 'bash-notes' -- "$@"` if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi @@ -205,6 +283,8 @@ 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 ) @@ -215,6 +295,10 @@ while true; do echo $BASENAME v${VERSION} exit ;; + -p | --plain ) + PLAIN=true + shift + ;; -l | --list ) listnotes exit @@ -224,17 +308,12 @@ while true; do shift 2 addnote "$TITLE" ;; - -m | --modify ) + -e | --edit ) NOTE="$2" shift 2 editnote "$NOTE" ;; - -d | --date ) - NOTE="$2" - shift 2 - datenote "$NOTE" - ;; - -r | --remove ) + -d | --delete ) NOTE="$2" shift 2 rmnote "$NOTE" @@ -245,7 +324,8 @@ while true; do exit ;; -- ) - shift; break + shift + break ;; * ) break