From: danix Date: Fri, 24 Mar 2023 12:41:44 +0000 (+0100) Subject: add function mostly working. Fresh install now sets up the directories correctly X-Git-Tag: v0.2~15 X-Git-Url: https://git.danix.xyz/?p=bash-notes.git;a=commitdiff_plain;h=53f2ed579b9f9f4883a143b272d43e1479fdb9b3 add function mostly working. Fresh install now sets up the directories correctly --- diff --git a/notes.sh b/notes.sh index a67ec0a..84e06b7 100644 --- a/notes.sh +++ b/notes.sh @@ -4,14 +4,18 @@ PID=$$ VERSION="0.1" + EDITOR=${EDITOR:-/usr/bin/vim} -BASEDIR=${BASEDIR:-~/.bash-notes} +TERMINAL=${TERMINAL:-/usr/bin/alacritty} +JQ=$(which jq) + +BASEDIR=${BASEDIR:-~/.local/share/bash-notes} +RCFILE=${RCFILE:-~/.bash-notes.rc} DB=${BASEDIR}/db.json -TMPDB=/tmp/db.json NOTESDIR=${BASEDIR}/notes + +TMPDB=/tmp/db.json BASENAME=$( basename $0 ) -TERMINAL=${TERMINAL:-/usr/bin/alacritty} -JQ=$(which jq) if [ ! -x $JQ ]; then echo "jq not found in your PATH" @@ -19,6 +23,11 @@ if [ ! -x $JQ ]; then exit 1 fi +# IMPORT USER DEFINED OPTIONS IF ANY +if [[ -f $RCFILE ]]; then + source $RCFILE +fi + # We prevent the program from running more than one instance: PIDFILE=/var/tmp/$(basename $0 .sh).pid @@ -50,20 +59,16 @@ function helptext() { } function addnote() { - NOTETITLE=$1 - echo "add new note" + NOTETITLE="$1" + echo "adding new note - \"$NOTETITLE\"" NOW=$(date +%s) - FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T) - LASTID=$($JQ '.notes[-1].id' $DB) + LASTID=$($JQ '.notes[-1].id | tonumber' $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 + $(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${NOW}) } function listnotes() { @@ -76,19 +81,51 @@ function editnote() { function datenote() { echo "edit date for note \"${1}\"" + # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T) + } function rmnote() { - echo "remove note" + NOTE=$1 + echo "removing note $NOTE" } +# we should expand on this function to add a sample note and explain a little bit +# how the program works. function firstrun() { - mkdir -p $NOTESDIR - cat << "__EOL__" > $DB + [ -f $RCFILE ] && RC=$RCFILE || RC="none" + + clear + echo "${BASENAME} configuration: + +base directory: ${BASEDIR}/ +notes archive: ${NOTESDIR}/ +notes database: ${DB} +rc file: $RC +text editor: ${EDITOR} +terminal: ${TERMINAL} +jq executable: ${JQ} +" + + read -r -p "Do you wish to continue? (y/N) " ANSWER + case $ANSWER in + y|Y ) + mkdir -p $NOTESDIR + cat << "__EOL__" > ${DB} { "notes": [] } __EOL__ + ;; + n|N ) + echo "No changes made. Exiting" + exit + ;; + * ) + echo "No changes made. Exiting" + exit + ;; + esac } # check for notes dir existance and create it in case it doesn't exists @@ -97,43 +134,67 @@ if [[ ! -d $NOTESDIR ]]; then 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 +# 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,add:,modify:,date:,remove:,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" + +while true; do + case "$1" in + -h | --help ) + helptext + exit + ;; + -v | --version ) + echo $BASENAME v${VERSION} + exit + ;; + -l | --list ) + listnotes + exit + ;; + -a | --add ) + TITLE="$2" + shift 2 + addnote "$TITLE" + ;; + -m | --modify ) + NOTE="$2" + shift 2 + editnote "$NOTE" + ;; + -d | --date ) + NOTE="$2" + shift 2 + datenote "$NOTE" + ;; + -r | --remove ) + NOTE="$2" + shift 2 + rmnote "$NOTE" + ;; + --editor ) + EDITOR="$2" + shift 2 + echo "changed EDITOR TO \"$EDITOR\"" + ;; + --storage ) + BASEDIR="$2" + shift 2 + echo "changed BASEDIR TO \"$BASEDIR\"" + # firstrun + ;; + -- ) + shift; break + ;; + * ) + break + ;; + 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. -# --------------------------------------------------------------------------- -