| 1 | #! /bin/bash |
| 2 | |
| 3 | # bash-notes © 2023 by danix is licensed under CC BY-NC 4.0. |
| 4 | # To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/ |
| 5 | |
| 6 | # set -ex |
| 7 | |
| 8 | PID=$$ |
| 9 | VERSION="0.1" |
| 10 | |
| 11 | set_defaults() { |
| 12 | # Binaries to use |
| 13 | EDITOR=${EDITOR:-/usr/bin/vim} |
| 14 | TERMINAL=${TERMINAL:-/usr/bin/alacritty} |
| 15 | # add options for your terminal. Remember to add the last option to execute |
| 16 | # your editor program, otherwise the script will fail. |
| 17 | # see example in the addnote function |
| 18 | TERM_OPTS="--class notes --title notes -e " |
| 19 | JQ=${JQ:-/usr/bin/jq} |
| 20 | |
| 21 | # base directory for program files |
| 22 | BASEDIR=${BASEDIR:-~/.local/share/bash-notes} |
| 23 | # notes database in json format |
| 24 | DB=${BASEDIR}/db.json |
| 25 | # directory containing the actual notes |
| 26 | NOTESDIR=${BASEDIR}/notes |
| 27 | |
| 28 | } # end set_defaults, do not change this line. |
| 29 | |
| 30 | set_defaults |
| 31 | |
| 32 | # Do not edit below this point |
| 33 | RCFILE=${RCFILE:-~/.config/bash-notes.rc} |
| 34 | TMPDB=/tmp/db.json |
| 35 | BASENAME=$( basename $0 ) |
| 36 | NOW=$(date +%s) |
| 37 | |
| 38 | if [ ! -x $JQ ]; then |
| 39 | echo "jq not found in your PATH" |
| 40 | echo "install jq to continue" |
| 41 | exit 1 |
| 42 | fi |
| 43 | |
| 44 | # IMPORT USER DEFINED OPTIONS IF ANY |
| 45 | if [[ -f $RCFILE ]]; then |
| 46 | source $RCFILE |
| 47 | fi |
| 48 | |
| 49 | # We prevent the program from running more than one instance: |
| 50 | PIDFILE=/var/tmp/$(basename $0 .sh).pid |
| 51 | |
| 52 | # Make sure the PID file is removed when we kill the process |
| 53 | trap 'rm -f $PIDFILE; exit 1' TERM INT |
| 54 | |
| 55 | if [[ -r $PIDFILE ]]; then |
| 56 | # PIDFILE exists, so I guess there's already an instance running |
| 57 | # let's kill it and run again |
| 58 | kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1 |
| 59 | # should already be deleted by trap, but just to be sure |
| 60 | rm $PIDFILE |
| 61 | fi |
| 62 | |
| 63 | # create PIDFILE |
| 64 | echo $PID > $PIDFILE |
| 65 | |
| 66 | # check if input is a number, returns false or the number itself |
| 67 | function check_noteID() { |
| 68 | IN=$1 |
| 69 | case $IN in |
| 70 | ''|*[!0-9]*) |
| 71 | return 1 |
| 72 | ;; |
| 73 | *) |
| 74 | echo $IN |
| 75 | ;; |
| 76 | esac |
| 77 | } |
| 78 | |
| 79 | function helptext() { |
| 80 | echo "Usage:" |
| 81 | echo " $0 [PARAMS] ..." |
| 82 | echo "" |
| 83 | cat << __NOWCONF__ |
| 84 | ${BASENAME} configuration is: |
| 85 | |
| 86 | base directory: ${BASEDIR}/ |
| 87 | notes archive: ${NOTESDIR}/ |
| 88 | notes database: ${DB} |
| 89 | rc file: $RCFILE |
| 90 | |
| 91 | text editor: ${EDITOR} |
| 92 | terminal: ${TERMINAL} |
| 93 | jq executable: ${JQ} |
| 94 | __NOWCONF__ |
| 95 | |
| 96 | echo "" |
| 97 | echo "${BASENAME} parameters are:" |
| 98 | echo " -h | --help : This help text" |
| 99 | echo " -p | --plain : Output is in plain text" |
| 100 | echo " (without this option the output is formatted)" |
| 101 | echo " (this option must precede all others)" |
| 102 | echo " -l | --list : List existing notes" |
| 103 | echo " -a | --add [\"<title>\"] : Add new note" |
| 104 | echo " -e | --edit [<note>] : Edit note" |
| 105 | echo " -d | --delete [<note> | all] : Delete single note or all notes at once" |
| 106 | echo " -v | --version : Print version" |
| 107 | echo " --userconf : Export User config file" |
| 108 | echo "" |
| 109 | } |
| 110 | |
| 111 | function addnote() { |
| 112 | # remove eventually existing temp DB file |
| 113 | if [[ -f $TMPDB ]]; then |
| 114 | rm $TMPDB |
| 115 | fi |
| 116 | |
| 117 | NOTETITLE="$1" |
| 118 | echo "adding new note - \"$NOTETITLE\"" |
| 119 | LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB) |
| 120 | # [ "" == $LASTID ] && LASTID=0 |
| 121 | NOTEID=$(( $LASTID + 1 )) |
| 122 | touch ${NOTESDIR}/${NOW} |
| 123 | $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB |
| 124 | mv $TMPDB $DB |
| 125 | # example for alacritty: |
| 126 | # alacritty --class notes --title notes -e /usr/bin/vim ... |
| 127 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW}) |
| 128 | } |
| 129 | |
| 130 | function listnotes() { |
| 131 | # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored" |
| 132 | if [[ $(ls -A $NOTESDIR) ]]; then |
| 133 | if [ $PLAIN == false ]; then |
| 134 | echo "listing all notes" |
| 135 | echo "" |
| 136 | fi |
| 137 | [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]" |
| 138 | for i in ${NOTESDIR}/*; do |
| 139 | local fname=$(basename $i) |
| 140 | DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z") |
| 141 | TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB) |
| 142 | ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB) |
| 143 | [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}" |
| 144 | done |
| 145 | else |
| 146 | echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\"" |
| 147 | fi |
| 148 | } |
| 149 | |
| 150 | function editnote() { |
| 151 | NOTE=$1 |
| 152 | local OK=$(check_noteID $NOTE) |
| 153 | if [ ! $OK ]; then |
| 154 | echo "invalid note \"$NOTE\"" |
| 155 | exit 1 |
| 156 | fi |
| 157 | |
| 158 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
| 159 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
| 160 | if [ "$TITLE" ]; then |
| 161 | echo "editing note $TITLE" |
| 162 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE}) |
| 163 | else |
| 164 | echo "note not found" |
| 165 | exit 1 |
| 166 | fi |
| 167 | } |
| 168 | |
| 169 | function rmnote() { |
| 170 | # remove eventually existing temp DB file |
| 171 | if [[ -f $TMPDB ]]; then |
| 172 | rm $TMPDB |
| 173 | fi |
| 174 | |
| 175 | NOTE=$1 |
| 176 | if [ "all" == $NOTE ]; then |
| 177 | echo "You're going to delete all notes." |
| 178 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
| 179 | case $ANSWER in |
| 180 | y|Y ) |
| 181 | $JQ 'del(.notes[])' $DB > $TMPDB |
| 182 | mv $TMPDB $DB |
| 183 | rm $NOTESDIR/* |
| 184 | echo "Deleted all notes" |
| 185 | ;; |
| 186 | * ) |
| 187 | echo "Aborting, no notes were deleted." |
| 188 | exit 1 |
| 189 | ;; |
| 190 | esac |
| 191 | else |
| 192 | local OK=$(check_noteID $NOTE) |
| 193 | if [ ! $OK ]; then |
| 194 | echo "invalid note \"$NOTE\"" |
| 195 | exit 1 |
| 196 | fi |
| 197 | |
| 198 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
| 199 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
| 200 | if [ "$TITLE" ]; then |
| 201 | $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB |
| 202 | mv $TMPDB $DB |
| 203 | rm $NOTESDIR/$FILE |
| 204 | echo "Deleted note $TITLE" |
| 205 | else |
| 206 | echo "note not found" |
| 207 | exit 1 |
| 208 | fi |
| 209 | fi |
| 210 | } |
| 211 | |
| 212 | function export_config() { |
| 213 | if [ -r ${RCFILE} ]; then |
| 214 | echo "Backing up current '${RCFILE}'...." |
| 215 | mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M) |
| 216 | fi |
| 217 | echo "Writing '${RCFILE}'...." |
| 218 | sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \ |
| 219 | | grep -v set_defaults \ |
| 220 | | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \ |
| 221 | > ${RCFILE} |
| 222 | if [ -r ${RCFILE} ]; then |
| 223 | echo "Taking no further action." |
| 224 | exit 0 |
| 225 | else |
| 226 | echo "Could not write '${RCFILE}'...!" |
| 227 | exit 1 |
| 228 | fi |
| 229 | } |
| 230 | |
| 231 | # we should expand on this function to add a sample note and explain a little bit |
| 232 | # how the program works. |
| 233 | function firstrun() { |
| 234 | [ -f $RCFILE ] && RC=$RCFILE || RC="none" |
| 235 | |
| 236 | clear |
| 237 | echo "${BASENAME} configuration: |
| 238 | |
| 239 | base directory: ${BASEDIR}/ |
| 240 | notes archive: ${NOTESDIR}/ |
| 241 | notes database: ${DB} |
| 242 | rc file: $RC |
| 243 | text editor: ${EDITOR} |
| 244 | terminal: ${TERMINAL} |
| 245 | jq executable: ${JQ} |
| 246 | " |
| 247 | |
| 248 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
| 249 | case $ANSWER in |
| 250 | y|Y ) |
| 251 | mkdir -p $NOTESDIR |
| 252 | cat << __EOL__ > ${DB} |
| 253 | { |
| 254 | "params": { |
| 255 | "version": "${VERSION}", |
| 256 | "dbversion": "${NOW}" |
| 257 | }, |
| 258 | "notes": [] |
| 259 | } |
| 260 | __EOL__ |
| 261 | echo; echo "All done, you can now write your first note." |
| 262 | ;; |
| 263 | * ) |
| 264 | echo "No changes made. Exiting" |
| 265 | exit |
| 266 | ;; |
| 267 | esac |
| 268 | } |
| 269 | |
| 270 | # check for notes dir existance and create it in case it doesn't exists |
| 271 | if [[ ! -d $NOTESDIR ]]; then |
| 272 | # we don't have a directory. FIRST RUN? |
| 273 | firstrun |
| 274 | fi |
| 275 | |
| 276 | # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this |
| 277 | # separately; see below. |
| 278 | GOPT=`getopt -o hvpla:e:d: --long help,version,list,plain,userconf,add:,edit:,delete:,editor:,storage: \ |
| 279 | -n 'bash-notes' -- "$@"` |
| 280 | |
| 281 | if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi |
| 282 | |
| 283 | # Note the quotes around `$GOPT': they are essential! |
| 284 | eval set -- "$GOPT" |
| 285 | |
| 286 | PLAIN=false |
| 287 | |
| 288 | while true; do |
| 289 | case "$1" in |
| 290 | -h | --help ) |
| 291 | helptext |
| 292 | exit |
| 293 | ;; |
| 294 | -v | --version ) |
| 295 | echo $BASENAME v${VERSION} |
| 296 | exit |
| 297 | ;; |
| 298 | -p | --plain ) |
| 299 | PLAIN=true |
| 300 | shift |
| 301 | ;; |
| 302 | -l | --list ) |
| 303 | listnotes |
| 304 | exit |
| 305 | ;; |
| 306 | -a | --add ) |
| 307 | TITLE="$2" |
| 308 | shift 2 |
| 309 | addnote "$TITLE" |
| 310 | ;; |
| 311 | -e | --edit ) |
| 312 | NOTE="$2" |
| 313 | shift 2 |
| 314 | editnote "$NOTE" |
| 315 | ;; |
| 316 | -d | --delete ) |
| 317 | NOTE="$2" |
| 318 | shift 2 |
| 319 | rmnote "$NOTE" |
| 320 | ;; |
| 321 | --userconf ) |
| 322 | export_config |
| 323 | echo "config exported to \"$RCFILE\"" |
| 324 | exit |
| 325 | ;; |
| 326 | -- ) |
| 327 | shift |
| 328 | break |
| 329 | ;; |
| 330 | * ) |
| 331 | break |
| 332 | ;; |
| 333 | esac |
| 334 | done |
| 335 | |