Compare commits

...

4 Commits

Author SHA1 Message Date
Emre AKYÜZ
787ae2c4b3
Decrease 3 more lines 2024-06-30 20:24:05 +03:00
Emre AKYÜZ
635ac64a8a
Decrease lines 2024-06-30 20:22:58 +03:00
Emre AKYÜZ
7d74a86a6b
Small clean-up 2024-06-30 20:20:11 +03:00
Emre AKYÜZ
fe2747d30d
Minimize | Streamline | Clean-up 2024-06-30 20:17:52 +03:00

View File

@ -1,144 +1,71 @@
#!/bin/dash #!/bin/sh
f="${XDG_DATA_HOME}/urls"
URLQUERY_FILE="${HOME}/.local/share/urlquery" am="@@"
ACTION_MENU="@@" d() { dmenu -i -l "${1}" -p "${2}"; }
no() { notify-send "Bookmarks" "${1}"; }
# For additional search function, see information on open_bookmark function below eno() { no "${1}"; exit; }
pr() { printf "%s\n" "${@}"; }
CLIPBOARD() { ch() {
xclip -o [ -f "${f}" ] || {
no "${f} does not exist. Creating it now."
pr "SearXNG=https://searx.tiekoetter.com/search?q=" > "${f}"
pr "YouTube=https://www.youtube.com/results?search_query=" >> "${f}"
}
} }
gs() { cut -d= -f1 "${f}" | d "${l}" "Bookmarks"; }
DMENU() { up() { sed -i "s|${1}.*$|${2}|" "${f}"; }
dmenu -i -l "${1}" -p "${2}" val() { pr "${1}" | grep -qE '^https?://[^\s/$.?#].[^\s]*$'; }
add() {
u="$(xclip -o)"
val "${u}" || eno "Clipboard not valid"
grep -q "=${u}$" "${f}" && eno "URL already saved"
n="$(pr "" | d "0" "Name")"
[ "${n}" ] && pr "${n}=${u}" >> "${f}" && no "'${n}' bookmarked"
} }
del() {
error_notify() { n="$(gs)"
notify-send "${1}" [ "${n}" ] || eno "Failed to delete bookmark"
exit "1" sed -i "/^${n}=.*/d" "${f}"
[ -s "${f}" ] && grep -q "\S" "${f}" || rm "${f}"
no "'${n}' is deleted."
} }
en() {
ensure_file_exists() { on="${1}"
[ -f "${URLQUERY_FILE}" ] || { nn="$(pr "" | d "0" "New Name")"
notify-send "${URLQUERY_FILE} does not exist. Creating it now." [ "${nn}" ] || exit
printf "SearXNG=https://searx.tiekoetter.com/search?q=\n" > "${URLQUERY_FILE}" u="$(grep "^${on}=" "${f}" | cut -d= -f2)"
} up "^${on}=" "${nn}=${u}"
} }
eu() {
get_selection() { n="${1}"
cut -d= -f1 "${URLQUERY_FILE}" | DMENU "${LINE_COUNT}" "Bookmarks" nu="$(pr "" | d "0" "New URL")"
[ "${nu}" ] || exit
up "${n}=" "${n}=${nu}"
} }
eb() {
update_file() { n="$(gs)"
pattern="${1}" [ "${n}" ] || eno "Failed to edit bookmark"
replacement="${2}" fi="$(pr "NAME" "URL" | d "2" "Edit")"
case "${fi}" in "NAME") en "${n}" ;; "URL") eu "${n}" ;; esac
sed "/${pattern}/c\\${replacement}" "${URLQUERY_FILE}" > "${URLQUERY_FILE}.tmp" && no "'${n}' is updated."
mv "${URLQUERY_FILE}.tmp" "${URLQUERY_FILE}" ||
error_notify "Failed to update the file."
} }
o() {
is_valid_url() { u="$(grep "^${s}=" "${f}" | cut -d= -f2-)"
printf "%s\n" "${1}" | grep -qE "^https?://[^[:space:]/?#][^[:space:]]+$" [ "${u}" ] || eno "Bookmark not found"
} case "${u}" in *"search"* | *"wiki"* | *"packages"*)
q="$(pr "" | d "0" "Search")"; u="${u}${q}" ;;
add_bookmark() {
URL="$(CLIPBOARD)"
is_valid_url "${URL}" || error_notify "The clipboard content is not a valid URL."
grep -q "=${URL}$" "${URLQUERY_FILE}" &&
notify-send "The URL is already in the list." && return
NAME="$(printf "" | DMENU "0" "Name")"
[ -n "${NAME}" ] && printf "%s\n" "${NAME}=${URL}" >> "${URLQUERY_FILE}" &&
notify-send "'${NAME}' is bookmarked."
}
delete_bookmark() {
NAME="$(get_selection)"
[ -z "${NAME}" ] && error_notify "Failed to delete the bookmark." && return
sed "/^${NAME}=/d" "${URLQUERY_FILE}" > "${URLQUERY_FILE}.tmp"
mv "${URLQUERY_FILE}.tmp" "${URLQUERY_FILE}"
[ -s "${URLQUERY_FILE}" ] && grep -qE "\S" "${URLQUERY_FILE}" || rm "${URLQUERY_FILE}"
notify-send "'${NAME}' is deleted."
}
edit_name() {
OLD_NAME="${1}"
NEW_NAME="$(printf "" | DMENU "0" "New Name")"
[ -z "${NEW_NAME}" ] && return
URL="$(grep "^${OLD_NAME}=" "${URLQUERY_FILE}" | cut -d= -f2)"
update_file "^${OLD_NAME}=" "${NEW_NAME}=${URL}"
}
edit_url() {
NAME="${1}"
NEW_URL="$(echo "" | DMENU "0" "New URL")"
[ -z "${NEW_URL}" ] && return
update_file "^${NAME}=.*" "${NAME}=${NEW_URL}"
}
edit_bookmark() {
NAME="$(get_selection)"
[ -z "${NAME}" ] && error_notify "Failed to edit the bookmark." && return
FIELD="$(printf "Name\nURL\n" | DMENU "2" "Edit")"
case "${FIELD}" in
"Name")edit_name "${NAME}";;
"URL")edit_url "${NAME}"
esac esac
"${BROWSER}" "${u}" || eno "Failed to open: ${u}"
notify-send "'${NAME}' is updated."
} }
ch
open_bookmark() { l="$(wc -l < "${f}")"
URL="$(grep "^${SELECTION}=" "${URLQUERY_FILE}" | cut -d= -f2-)" [ "${l}" -gt "21" ] && l="21"
s="$(gs)"
[ -z "${URL}" ] && notify-send "Bookmark not found." && exit "1" case "${s}" in
"${am}")
case "${URL}" in a="$(pr "Add" "Delete" "Edit" | d "3" "Action")"
*"search"* | *"wiki"* | *"packages"*) QUERY="$(echo "" | DMENU "0" "Search")" case "${a}" in "Add") add ;; "Delete") del ;; "Edit") eb ;; esac
URL="${URL}${QUERY}"
;;
esac
"${BROWSER}" "${URL}" || notify-send "Failed to open the URL."
}
ensure_file_exists
LINE_COUNT="$(wc -l < "${URLQUERY_FILE}")"
[ "${LINE_COUNT}" -ge "15" ] && LINE_COUNT="15"
SELECTION="$(get_selection)"
[ -z "${SELECTION}" ] && exit
case "${SELECTION}" in
"${ACTION_MENU}")
ACTION="$(printf "Add\nDelete\nEdit\n" | DMENU "3" "Action")"
case "${ACTION}" in
"Add") add_bookmark ;;
"Delete") delete_bookmark ;;
"Edit") edit_bookmark ;;
esac
;;
*)
open_bookmark
;; ;;
"") exit ;;
*) o ;;
esac esac