]> danix's work - bash-notes.git/commitdiff
when creating a note, if the title is not specified, two random words will be assigne...
authordanix <redacted>
Mon, 10 Apr 2023 16:15:36 +0000 (18:15 +0200)
committerdanix <redacted>
Mon, 10 Apr 2023 16:15:36 +0000 (18:15 +0200)
SOURCE/CORE/core-add.sh
SOURCE/CORE/helpers.sh
notes.sh

index aa9d451b4998cc2c2b9438283ee3c0aa0dad50c6..ed6256c373286d32ce396ed5bcc84848cd96405a 100644 (file)
@@ -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)
index 565413df080320b63436cdff228a74613b4203d3..567846b04033d148b4f9065f71f6df5f34824cde 100644 (file)
@@ -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
+}
+
index d0fd902f3a9e0fc97c5d44f8dfc38ccc8f17f2bb..1a4b56683299015168a2f6edafb955c714c28ad5 100755 (executable)
--- 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)