From: danix Date: Mon, 10 Apr 2023 16:15:36 +0000 (+0200) Subject: when creating a note, if the title is not specified, two random words will be assigne... X-Git-Tag: v0.4~9 X-Git-Url: https://git.danix.xyz/?a=commitdiff_plain;h=6a1930959e1cb0ec8739254bedc6b8de2bcc20fd;p=bash-notes.git when creating a note, if the title is not specified, two random words will be assigned as title. --- diff --git a/SOURCE/CORE/core-add.sh b/SOURCE/CORE/core-add.sh index aa9d451..ed6256c 100644 --- a/SOURCE/CORE/core-add.sh +++ b/SOURCE/CORE/core-add.sh @@ -4,7 +4,8 @@ function addnote() { rm $TMPDB fi - NOTETITLE="$1" + RTITLE=$(random_title) + [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1" echo "adding new note - \"$NOTETITLE\"" # shellcheck disable=SC2086 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB) diff --git a/SOURCE/CORE/helpers.sh b/SOURCE/CORE/helpers.sh index 565413d..567846b 100644 --- a/SOURCE/CORE/helpers.sh +++ b/SOURCE/CORE/helpers.sh @@ -51,3 +51,25 @@ __NOWCONF__ } +# this function returns a random 2 words title +function random_title() { + # Constants + X=0 + DICT=/usr/share/dict/words + OUTPUT="" + + # total number of non-random words available + COUNT=$(cat $DICT | wc -l) + + # while loop to generate random words + while [ "$X" -lt 2 ] + do + RAND=$(od -N3 -An -i /dev/urandom | awk -v f=0 -v r="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}') + OUTPUT+="$(sed `echo $RAND`"q;d" $DICT)" + (("X = X + 1")) + [[ $X -eq 1 ]] && OUTPUT+=" " + done + + echo $OUTPUT +} + diff --git a/notes.sh b/notes.sh index d0fd902..1a4b566 100755 --- a/notes.sh +++ b/notes.sh @@ -199,13 +199,36 @@ __NOWCONF__ } +# this function returns a random 2 words title +function random_title() { + # Constants + X=0 + DICT=/usr/share/dict/words + OUTPUT="" + + # total number of non-random words available + COUNT=$(cat $DICT | wc -l) + + # while loop to generate random words + while [ "$X" -lt 2 ] + do + RAND=$(od -N3 -An -i /dev/urandom | awk -v f=0 -v r="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}') + OUTPUT+="$(sed `echo $RAND`"q;d" $DICT)" + (("X = X + 1")) + [[ $X -eq 1 ]] && OUTPUT+=" " + done + + echo $OUTPUT +} + function addnote() { # remove eventually existing temp DB file if [[ -f $TMPDB ]]; then rm $TMPDB fi - NOTETITLE="$1" + RTITLE=$(random_title) + [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1" echo "adding new note - \"$NOTETITLE\"" # shellcheck disable=SC2086 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)