Minimize | Streamline | Clean-up

This commit is contained in:
Emre AKYÜZ 2024-06-30 20:17:52 +03:00 committed by GitHub
parent 0eaff5c245
commit fe2747d30d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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