Recently Written · git

sbm-suckless

sbm before the rewrite: bookmarks in a plain file, picked with dmenu. Suckless style.

git clone https://github.com/equwal/sbm-suckless

Log | Files | Refs


commit ab1adc5c042505f9bed2d89eea81760bbf91bd50
Tom Schwindl <schwindl@posteo.de>
2023-08-30 20:41:50 +0200

Make add() more robust and less buggy

Additionally, prevent duplicate tags and empty entries in the $BOOKMARKS file.

 bm | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/bm b/bm
index edafe1d..42a42a1 100755
--- a/bm
+++ b/bm
@@ -3,12 +3,14 @@
 # Don't forget to set $BOOKMARKS and $USERTAGS to files somewhere.
 # TODO:
 # - prevent duplicate URLs
-# - prevent duplicate tags during tag session
-# - prevent empty entries
 # - make the top tag a null tag so a user accidentally pressing ENTER twice doesn't close out the tag dialogue
 
 DMENULINES=15
 
+check () {
+    [ -z "$1" ] && exit 1
+}
+
 help () {
     echo "Call without an argument to be prompted with dmenu."
     echo "Flags:"
@@ -21,14 +23,19 @@ copy () {
 
 add () {
     url="$(xclip -o | dmenu -p 'url: ')"
-    desc="$(dmenu -p 'description: ')"
+    check "$url"
+    desc="$(printf "" | dmenu -p 'description: ')"
     tag=""
     tags=""
     while [ "$tag" != "done" ] ; do
-      [ ! -e "$tag" ] && tags="$tags $tag"
-      tag="$(echo "done | (choose when done)\n$(cat $USERTAGS)" | dmenu -i -l 15 -p 'tags: ' | sed 's/ *|.*$//')"
+      tags="$tags $tag"
+      tag="$(printf "done | (choose when done)\n$(cat $USERTAGS)" \
+            | dmenu -i -l $DMENULINES -p 'tags: ' | sed 's/ *|.*$//')"
     done
-    [ ! -e "$url" ] && printf "%s %s | %s\\n" "$url" "$desc" "$tags">>"$BOOKMARKS"
+    tags="$(printf "$tags" | tr ' ' '\n' | sort | uniq | tr '\n' ' ' \
+           | sed 's/ $//')"
+
+    printf "%s %s | %s\\n" "$url" "$desc" "$tags" >> "$BOOKMARKS"
 }
 
 edit () {