From 757bef81f198c47c959efab6c3d8f1096915abed Mon Sep 17 00:00:00 2001 From: awy Date: Sun, 24 Nov 2024 17:41:16 +0300 Subject: local --- .local/bin/bookmarks | 142 +++++++++++++++++++++++++++++++++++++++++++++++++ .local/bin/linkhandler | 26 +++++++++ 2 files changed, 168 insertions(+) create mode 100755 .local/bin/bookmarks create mode 100755 .local/bin/linkhandler (limited to '.local/bin') diff --git a/.local/bin/bookmarks b/.local/bin/bookmarks new file mode 100755 index 0000000..cd8462b --- /dev/null +++ b/.local/bin/bookmarks @@ -0,0 +1,142 @@ +#!/bin/dash + +URLQUERY_FILE="${HOME}/.local/share/urlquery" +ACTION_MENU="@@" + +CLIPBOARD() { + wl-paste +} + +DMENU() { + wmenu -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}" + } +} + +get_selection() { + cut -d= -f1 "${URLQUERY_FILE}" | DMENU "${LINE_COUNT}" "Bookmarks" +} + +update_file() { + pattern="${1}" + replacement="${2}" + + sed "/${pattern}/c\\${replacement}" "${URLQUERY_FILE}" > "${URLQUERY_FILE}.tmp" && + mv "${URLQUERY_FILE}.tmp" "${URLQUERY_FILE}" || + error_notify "Failed to update the file." +} + +is_valid_url() { + printf "%s\n" "${1}" | grep -qE "^https?://[^[:space:]/?#][^[:space:]]+$" +} + +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 + + notify-send "'${NAME}' is updated." +} + +open_bookmark() { + URL="$(grep "^${SELECTION}=" "${URLQUERY_FILE}" | cut -d= -f2-)" + + [ -z "${URL}" ] && notify-send "Bookmark not found." && exit "1" + + case "${URL}" in + *"search"* | *"wiki"* | *"packages"* | *"chatgpt"*) QUERY="$(echo "" | DMENU "0" "Search")" + 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 + ;; +esac diff --git a/.local/bin/linkhandler b/.local/bin/linkhandler new file mode 100755 index 0000000..17adc30 --- /dev/null +++ b/.local/bin/linkhandler @@ -0,0 +1,26 @@ +#!/bin/sh + +# Feed script a url or file location. +# If an image, it will view in nsxiv, +# if a video or gif, it will view in mpv +# if a music file or pdf, it will download, +# otherwise it opens link in browser. + +if [ -z "$1" ]; then + url="$(xclip -o)" +else + url="$1" +fi + +case "$url" in + *mkv|*webm|*mp4|*youtube.com/watch*|*youtube.com/playlist*|*youtube.com/shorts*|*youtu.be*|*youtube.com/v/*|*hooktube.com*|*bitchute.com*|*videos.lukesmith.xyz*|*odysee.com*) + setsid -f mpv -quiet "$url" >/dev/null 2>&1 ;; + *png|*jpg|*jpe|*jpeg|*gif|*webp) + curl -sL "$url" > "/tmp/$(echo "$url" | sed "s/.*\///;s/%20/ /g")" && imv "/tmp/$(echo "$url" | sed "s/.*\///;s/%20/ /g")" >/dev/null 2>&1 & ;; + *pdf|*cbz|*cbr) + curl -sL "$url" > "/tmp/$(echo "$url" | sed "s/.*\///;s/%20/ /g")" && zathura "/tmp/$(echo "$url" | sed "s/.*\///;s/%20/ /g")" >/dev/null 2>&1 & ;; + *mp3|*flac|*opus|*mp3?source*) + qndl "$url" 'curl -LO' >/dev/null 2>&1 ;; + *) + [ -f "$url" ] && setsid -f "$TERMINAL" -e "$EDITOR" "$url" >/dev/null 2>&1 || setsid -f "$BROWSER" "$url" >/dev/null 2>&1 +esac -- cgit v1.2.3