bookmarks (3203B)
1 #!/bin/dash 2 3 URLQUERY_FILE="${HOME}/.local/share/urlquery" 4 ACTION_MENU="@@" 5 6 CLIPBOARD() { 7 wl-paste 8 } 9 10 DMENU() { 11 wmenu -i -l "${1}" -p "${2}" 12 } 13 14 error_notify() { 15 notify-send "${1}" 16 exit "1" 17 } 18 19 ensure_file_exists() { 20 [ -f "${URLQUERY_FILE}" ] || { 21 notify-send "${URLQUERY_FILE} does not exist. Creating it now." 22 printf "SearXNG=https://searx.tiekoetter.com/search?q=\n" > "${URLQUERY_FILE}" 23 } 24 } 25 26 get_selection() { 27 cut -d= -f1 "${URLQUERY_FILE}" | DMENU "${LINE_COUNT}" "Bookmarks" 28 } 29 30 update_file() { 31 pattern="${1}" 32 replacement="${2}" 33 34 sed "/${pattern}/c\\${replacement}" "${URLQUERY_FILE}" > "${URLQUERY_FILE}.tmp" && 35 mv "${URLQUERY_FILE}.tmp" "${URLQUERY_FILE}" || 36 error_notify "Failed to update the file." 37 } 38 39 is_valid_url() { 40 printf "%s\n" "${1}" | grep -qE "^https?://[^[:space:]/?#][^[:space:]]+$" 41 } 42 43 add_bookmark() { 44 URL="$(CLIPBOARD)" 45 46 is_valid_url "${URL}" || error_notify "The clipboard content is not a valid URL." 47 48 grep -q "=${URL}$" "${URLQUERY_FILE}" && 49 notify-send "The URL is already in the list." && return 50 51 NAME="$(printf "" | DMENU "0" "Name")" 52 53 [ -n "${NAME}" ] && printf "%s\n" "${NAME}=${URL}" >> "${URLQUERY_FILE}" && 54 notify-send "'${NAME}' is bookmarked." 55 } 56 57 delete_bookmark() { 58 NAME="$(get_selection)" 59 60 [ -z "${NAME}" ] && error_notify "Failed to delete the bookmark." && return 61 62 sed "/^${NAME}=/d" "${URLQUERY_FILE}" > "${URLQUERY_FILE}.tmp" 63 mv "${URLQUERY_FILE}.tmp" "${URLQUERY_FILE}" 64 65 [ -s "${URLQUERY_FILE}" ] && grep -qE "\S" "${URLQUERY_FILE}" || rm "${URLQUERY_FILE}" 66 67 notify-send "'${NAME}' is deleted." 68 } 69 70 edit_name() { 71 OLD_NAME="${1}" 72 NEW_NAME="$(printf "" | DMENU "0" "New Name")" 73 74 [ -z "${NEW_NAME}" ] && return 75 76 URL="$(grep "^${OLD_NAME}=" "${URLQUERY_FILE}" | cut -d= -f2)" 77 78 update_file "^${OLD_NAME}=" "${NEW_NAME}=${URL}" 79 } 80 81 edit_url() { 82 NAME="${1}" 83 NEW_URL="$(echo "" | DMENU "0" "New URL")" 84 85 [ -z "${NEW_URL}" ] && return 86 87 update_file "^${NAME}=.*" "${NAME}=${NEW_URL}" 88 } 89 90 edit_bookmark() { 91 NAME="$(get_selection)" 92 93 [ -z "${NAME}" ] && error_notify "Failed to edit the bookmark." && return 94 95 FIELD="$(printf "Name\nURL\n" | DMENU "2" "Edit")" 96 97 case "${FIELD}" in 98 "Name")edit_name "${NAME}";; 99 "URL")edit_url "${NAME}" 100 esac 101 102 notify-send "'${NAME}' is updated." 103 } 104 105 open_bookmark() { 106 URL="$(grep "^${SELECTION}=" "${URLQUERY_FILE}" | cut -d= -f2-)" 107 108 [ -z "${URL}" ] && notify-send "Bookmark not found." && exit "1" 109 110 case "${URL}" in 111 *"search"* | *"wiki"* | *"packages"* | *"chatgpt"*) QUERY="$(echo "" | DMENU "0" "Search")" 112 URL="${URL}${QUERY}" 113 ;; 114 esac 115 116 "${BROWSER}" "${URL}" || notify-send "Failed to open the URL." 117 } 118 119 ensure_file_exists 120 121 LINE_COUNT="$(wc -l < "${URLQUERY_FILE}")" 122 123 [ "${LINE_COUNT}" -ge "15" ] && LINE_COUNT="15" 124 125 SELECTION="$(get_selection)" 126 127 [ -z "${SELECTION}" ] && exit 128 129 case "${SELECTION}" in 130 "${ACTION_MENU}") 131 ACTION="$(printf "Add\nDelete\nEdit\n" | DMENU "3" "Action")" 132 133 case "${ACTION}" in 134 "Add") add_bookmark ;; 135 "Delete") delete_bookmark ;; 136 "Edit") edit_bookmark ;; 137 esac 138 ;; 139 *) 140 open_bookmark 141 ;; 142 esac