#!/bin/sh # Name : buku-swift # About : This is a rofi script to swiftly manage bookmarks with buku. # Version : 2024-05-27 # License : MIT; # Author : Schimon Zackary Jehudah; # TODO Add an invisible character to options. # NOTE This is useful to prevent conflict when title is set to Cancel, when option Cancel is not desired). case $XDG_SESSION_TYPE in # x11) /dev/pts/1) if [ -x "$(command -v xsel)" ]; then CLP=$(xsel) elif [ -x "$(command -v xclip)" ]; then CLP=$(xclip -out) else echo "Need xclip or xsel to use clipboard." fi ;; wayland) if [ -x "$(command -v wl-paste)" ]; then CLP=$(wl-paste --primary) else echo "Need wl-paste to use clipboard." fi ;; *) echo "Could not determine display server." echo "Defaulting to xclip." if [ -x "$(command -v xclip)" ]; then CLP=$(xclip -out) else echo "Need wl-paste to use clipboard." fi ;; esac # TODO encode URL (e.g. turn space into %20) # Remove everything after space #url=${CLP%% *}; url=$(echo -e "${CLP%% *}" | rofi -dmenu -i -p "buku" -mesg "Enter a URL"); database=$HOME/.local/share/buku/bookmarks.db; buku_args=''; ix=$(sqlite3 -noheader -list $database "SELECT id FROM bookmarks WHERE URL='$url'"); if [ -z "$ix" ]; then chosen_option=$(echo -e "Offline\nProceed\nCancel" | rofi -dmenu -i -p "buku" -mesg "Add URL $url"); if [ "$chosen_option" == "Cancel" ]; then exit; else if [ "$chosen_option" == "Offline" ]; then buku_args="--offline"; fi chosen_option=$(echo -e "Automatic\nProceed\nCancel" | rofi -dmenu -i -p "buku" -mesg "Set title. Proceed to leave title blanked."); fi if [ "$chosen_option" == "Cancel" ]; then exit; else case $chosen_option in "Automatic") echo "Title be fetched automatically."; ;; "Proceed") buku_args="${buku_args} --title"; ;; *) buku_args="${buku_args} --title $chosen_option"; ;; esac fi chosen_option=$(echo -e "Automatic\nProceed\nCancel" | rofi -dmenu -i -p "buku" -mesg "Set description. Proceed to leave comment blanked."); if [ "$chosen_option" == "Cancel" ]; then exit; else case $chosen_option in "Automatic") echo "Comment be fetched automatically."; ;; "Proceed") buku_args="${buku_args} --comment"; ;; *) buku_args="${buku_args} --comment $chosen_option"; ;; esac fi chosen_option=$(echo -e "Proceed\nCancel" | rofi -dmenu -i -p "buku" -mesg "Add tags (comma-separated)."); if [ "$chosen_option" == "Cancel" ]; then exit; else if [ "$chosen_option" == "Proceed" ]; then unset chosen_option fi tags=$chosen_option buku_args="${buku_args} --tag $chosen_option"; buku --nc --np --add $url $buku_args ix=$(sqlite3 -noheader -list $database "SELECT id FROM bookmarks WHERE URL='$url'"); bookmark=$(buku --nc --print $ix) chosen_option=$(echo -e "Cancel\nEdit\nDelete" | rofi -dmenu -i -mesg "A new URL has been added to index $ix"); fi else chosen_option=$(echo -e "Cancel\nEdit\nUpdate\nDelete" | rofi -dmenu -i -p "buku" -mesg "URL already exists at index $ix"); fi edit_bookmark() { title=$(sqlite3 -noheader -list $database "SELECT metadata FROM bookmarks WHERE id='$ix'"); desc=$(sqlite3 -noheader -list $database "SELECT desc FROM bookmarks WHERE id='$ix'"); tags=$(sqlite3 -noheader -list $database "SELECT tags FROM bookmarks WHERE id='$ix'"); url=$(sqlite3 -noheader -list $database "SELECT URL FROM bookmarks WHERE id='$ix'"); chosen_option=$(echo -e "Cancel\n* $title\n> $url\n+ $desc\n# $tags" | rofi -dmenu -i -p "buku" -mesg "Bookmark #$ix. Choose a property to edit."); case $chosen_option in "*"*) chosen_option=$(echo -e "Cancel" | rofi -dmenu -i -p "buku" -mesg "Enter a new title for #$ix. Current title: $title"); if [ "$chosen_option" != "Cancel" ]; then buku --update $ix --title $chosen_option; fi edit_bookmark ;; "+"*) desc=$(sqlite3 -noheader -list $database "SELECT desc FROM bookmarks WHERE id='$ix'") chosen_option=$(echo -e "Cancel" | rofi -dmenu -i -p "buku" -mesg "Enter a new description for #$ix. Current description: $desc"); if [ "$chosen_option" != "Cancel" ]; then buku --update $ix --comment $chosen_option; fi edit_bookmark ;; "#"*) tags=$(sqlite3 -noheader -list $database "SELECT tags FROM bookmarks WHERE id='$ix'") chosen_option=$(echo -e "Add\nRemove\nReplace\nCancel" | rofi -dmenu -i -mesg "Select an action for tags."); case $chosen_option in "Add") chosen_option=$(echo -e "Cancel" | rofi -dmenu -i -p "buku" -mesg "Add new tags to #$ix. Current tags: $tags"); if [ "$chosen_option" != "Cancel" ]; then buku --update $ix --tag + $chosen_option; fi ;; "Remove") chosen_option=$(echo -e "Cancel" | rofi -dmenu -i -p "buku" -mesg "Remove tags from #$ix. Current tags: $tags"); if [ "$chosen_option" != "Cancel" ]; then buku --update $ix --tag - $chosen_option; fi ;; "Replace") chosen_option=$(echo -e "Cancel" | rofi -dmenu -i -p "buku" -mesg "Enter a new set of tags for #$ix. Current tags: $tags"); if [ "$chosen_option" != "Cancel" ]; then buku --update $ix --tag $chosen_option; fi ;; *) exit; ;; esac edit_bookmark ;; ">"*) # TODO Handle white-space url=$(sqlite3 -noheader -list $database "SELECT URL FROM bookmarks WHERE id='$ix'") chosen_option=$(echo -e "Cancel" | rofi -dmenu -i -p "buku" -mesg "Enter a new URL for #$ix. Current URL: $url"); if [ "$chosen_option" != "Cancel" ]; then # FIXME buku: Argument --offline is not respected buku --update $ix --url $chosen_option --offline; fi edit_bookmark ;; *) exit ;; esac } case $chosen_option in "Delete") chosen_option=$(echo -e "Cancel\nDelete" | rofi -dmenu -i -p "buku" -mesg "Confirm deletion of URL $url ($title)"); if [ "$chosen_option" == "Delete" ]; then # FIXME buku: Argument --np is not respected echo -e "Cancel" | rofi -dmenu -i -p "WARNING! " -mesg "Functionality \"Delete\" is not supported yet. Please execute \"buku --delete $ix\" instead." # buku --nc --delete $ix # NOTE This does not work with --np TODO Ask to add force option. # sqlite3 -noheader -list $database "DELETE FROM bookmarks WHERE id='$ix'" fi ;; "Edit") edit_bookmark ;; "Update") # FIXME buku: This appears not to work as expected buku --update $ix ;; *) exit; ;; esac