|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +#This file is part of the TinyTools distribution (https://github.com/Calebe94/TinyTools). |
| 4 | +#Copyright (C) 2021 TinyTools |
| 5 | +# |
| 6 | +#This program is free software: you can redistribute it and/or modify |
| 7 | +#it under the terms of the GNU General Public License as published by |
| 8 | +#the Free Software Foundation, either version 3 of the License, or |
| 9 | +#(at your option) any later version. |
| 10 | +# |
| 11 | +#This program is distributed in the hope that it will be useful, |
| 12 | +#but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +#GNU General Public License for more details. |
| 15 | +# |
| 16 | +#You should have received a copy of the GNU General Public License |
| 17 | +#along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | + |
| 20 | +#################################################################################################### |
| 21 | +# Variables used in the script. |
| 22 | +#################################################################################################### |
| 23 | +DMENU_ARGS=$@ |
| 24 | +TBM=tbm |
| 25 | +[ -z "$TBM_FILE" ] || [ "$TBM_FILE" = "/dev/stdout" ] && export TBM_FILE=$HOME/.tbookmarks |
| 26 | + |
| 27 | +get_bookmark_url() |
| 28 | +{ |
| 29 | + $TBM -l | grep "$1" | awk '{print $1}' |
| 30 | +} |
| 31 | + |
| 32 | +get_bookmark_id() |
| 33 | +{ |
| 34 | + $TBM -li | grep "$1" | awk '{print $1}' | rev | cut -c 2- | rev |
| 35 | +} |
| 36 | + |
| 37 | +bookmark_action() |
| 38 | +{ |
| 39 | + bookmark="$1" |
| 40 | + actions="[open]\n[copy]\n[delete]\n[back]\n[close]" |
| 41 | + selection=$(echo "$actions" | dmenu -p "$bookmark" $DMENU_ARGS) |
| 42 | + if [ ! -z "$selection" ]; then |
| 43 | + case "$selection" in |
| 44 | + "[open]") |
| 45 | + url=$(get_bookmark_url "$bookmark") |
| 46 | + [ -z "$url" ] && echo "No url given!" | dmenu -p "Error" $DMENU_ARGS || xdg-open "$url" |
| 47 | + ;; |
| 48 | + "[copy]") |
| 49 | + url=$(get_bookmark_url "$bookmark") |
| 50 | + echo "$url" | xclip -selection clipboard |
| 51 | + ;; |
| 52 | + "[delete]") |
| 53 | + choice=$(echo "no\nyes" | dmenu -p "Are you sure?" $DMENU_ARGS) |
| 54 | + if [ "$choice" = "yes" ]; then |
| 55 | + id=$(get_bookmark_id "$bookmark") |
| 56 | + [ -z "$id" ] && echo "No id given!" | dmenu -p "Error" $DMENU_ARGS || tbm -d "$id" |
| 57 | + else |
| 58 | + bookmark_action "$bookmark" |
| 59 | + fi |
| 60 | + ;; |
| 61 | + "[back]") show_bookmarks ;; |
| 62 | + *) exit 0 ;; |
| 63 | + esac |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +list_bookmarks_titles() |
| 68 | +{ |
| 69 | + $TBM -lt |
| 70 | +} |
| 71 | + |
| 72 | +show_bookmarks() |
| 73 | +{ |
| 74 | + actions="[new]\n[close]\n" |
| 75 | + bookmarks="$actions$(list_bookmarks_titles)" |
| 76 | + selection=$(echo "$bookmarks" | dmenu -p "Bookmarks" $DMENU_ARGS) |
| 77 | + if [ ! -z "$selection" ]; then |
| 78 | + case "$selection" in |
| 79 | + "[new]") |
| 80 | + status=$($TBM "$(xclip -o -selection clipboard)") |
| 81 | + [ -z "$status" ] && echo "No url on clipboard!" | dmenu -p "Error" $DMENU_ARGS || echo "$status" |
| 82 | + ;; |
| 83 | + "[close]") exit 0 ;; |
| 84 | + *) bookmark_action "$selection" ;; |
| 85 | + esac |
| 86 | + fi |
| 87 | +} |
| 88 | + |
| 89 | +################################################################################################### |
| 90 | +############################################## MAIN ############################################### |
| 91 | +################################################################################################### |
| 92 | + |
| 93 | +show_bookmarks |
0 commit comments