2024-06-12 22:56:44 -04:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
add_bookmark() {
|
|
|
|
while : ; do
|
2024-06-12 23:36:03 -04:00
|
|
|
if [[ -r "${config}/surfraw/bookmarks" ]] && [[ -r "${config}/i3/bookmarks" ]]; then
|
2024-06-12 22:56:44 -04:00
|
|
|
# Ask for the Bookmark Title, URL, and file selection using yad --form with radio buttons
|
|
|
|
input=$(yad --form \
|
|
|
|
--title="I38 - Add Bookmark" \
|
|
|
|
--text="New Bookmark:" \
|
|
|
|
--field="Bookmark Title" \
|
|
|
|
--field="URL" \
|
|
|
|
--field="!Save As:CB" \
|
|
|
|
--field="Surfraw Bookmark":CHK \
|
|
|
|
--field="I38 Bookmark":CHK \
|
|
|
|
--button="OK:0" \
|
|
|
|
--button="Cancel:1" \
|
|
|
|
--selectable-labels \
|
|
|
|
--separator="|")
|
|
|
|
else
|
|
|
|
# Ask for the Bookmark Title and URL using yad --form without radio buttons
|
|
|
|
input=$(yad --form \
|
|
|
|
--title="I38 - Add Bookmark" \
|
|
|
|
--text="New Bookmark:" \
|
|
|
|
--field="Bookmark Title" \
|
|
|
|
--field="URL" \
|
|
|
|
--button="OK:0" \
|
|
|
|
--button="Cancel:1" \
|
|
|
|
--selectable-labels \
|
|
|
|
--separator="|")
|
|
|
|
fi
|
|
|
|
yadCode="$?"
|
|
|
|
|
|
|
|
# Check if the user pressed Cancel
|
|
|
|
if [[ $yadCode -eq 1 ]]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Split the input into components
|
|
|
|
IFS='|' read -r title url bookmarkFile <<< "$input"
|
|
|
|
|
|
|
|
# Check if title or URL is empty
|
|
|
|
if [[ -z "$title" || -z "$url" ]]; then
|
|
|
|
yad --info --title="Add Bookmark" --text="Both Title and URL cannot be empty. Please try again."
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check for spaces in title and prompt user
|
2024-06-12 23:36:03 -04:00
|
|
|
if [[ "$title" =~ \ ]] && [[ "$bookmarkFile" == "Surfraw Bookmark" ]]; then
|
2024-06-12 22:56:44 -04:00
|
|
|
newTitle="${title//[[:space:]]/_}"
|
2024-06-12 23:36:03 -04:00
|
|
|
yad --question --title="I38 - Bookmarks" --text="The title contains spaces, which have been converted to underscores: \"$newTitle\". Is this okay?"
|
2024-06-12 22:56:44 -04:00
|
|
|
yadCode="$?"
|
|
|
|
if [[ $yadCode -ne 0 ]]; then
|
|
|
|
continue
|
|
|
|
else
|
|
|
|
title="$newTitle"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Determine the bookmarks file based on radio button selection
|
2024-06-12 23:36:03 -04:00
|
|
|
if [[ -f "${config}/surfraw/bookmarks" ]] && [[ -f "${config}/i3/bookmarks" ]]; then
|
|
|
|
if [[ "$bookmarkFile" == "Surfraw Bookmark" ]]; then
|
|
|
|
bookmarksFile="${config}/surfraw/bookmarks"
|
2024-06-12 22:56:44 -04:00
|
|
|
else
|
2024-06-12 23:36:03 -04:00
|
|
|
bookmarksFile="${config}/i3/bookmarks"
|
2024-06-12 22:56:44 -04:00
|
|
|
fi
|
|
|
|
else
|
2024-06-12 23:36:03 -04:00
|
|
|
bookmarksFile="${config}/surfraw/bookmarks"
|
|
|
|
[[ -f "${config}/i3/bookmarks" ]] && bookmarksFile="${config}/i3/bookmarks"
|
2024-06-12 22:56:44 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Check for duplicates
|
|
|
|
if grep -q "$url" "$bookmarksFile" ; then
|
2024-06-12 23:36:03 -04:00
|
|
|
existingTitle=$(grep "$url" "$bookmarksFile" | cut -d' ' -f1)
|
|
|
|
yad --form --title="I38 - Bookmarks" --selectable-labels --field="This site is already bookmarked as \"$existingTitle\".":LBL --button="OK:0"
|
2024-06-12 22:56:44 -04:00
|
|
|
return
|
|
|
|
elif grep -qi "^$title " "$bookmarksFile"; then
|
2024-06-12 23:36:03 -04:00
|
|
|
yad --form --title="I38 - Bookmarks" --selectable-labels --field="A bookmark with the title \"$title\" already exists.":LBL --button="OK:0"
|
2024-06-12 22:56:44 -04:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Add the new bookmark
|
2024-06-12 23:36:03 -04:00
|
|
|
echo "$title $url" >> "$bookmarksFile"
|
2024-06-12 22:56:44 -04:00
|
|
|
|
|
|
|
# Ensure no duplicates with sort -u
|
2024-06-12 23:36:03 -04:00
|
|
|
sort -u "$bookmarksFile" -o "$bookmarksFile"
|
|
|
|
yad --form --title="I38 - Bookmarks" --selectable-labels --field="Bookmark \"$title\" added successfully.":LBL --button="OK:0"
|
2024-06-12 22:56:44 -04:00
|
|
|
return
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-06-12 23:36:03 -04:00
|
|
|
# Prepare environment and read bookmarks
|
|
|
|
declare -a bookmarks
|
|
|
|
config="${XDG_CONFIG_HOME:-${HOME}/.config}"
|
2024-06-12 22:56:44 -04:00
|
|
|
if [[ -r "${config}/surfraw/bookmarks" ]]; then
|
2024-06-12 23:36:03 -04:00
|
|
|
mapfile -t contents < "${config}/surfraw/bookmarks"
|
2024-06-12 22:56:44 -04:00
|
|
|
for i in "${contents[@]}" ; do
|
2024-06-12 23:36:03 -04:00
|
|
|
title="${i% *}"
|
|
|
|
title="${title//_/ }"
|
|
|
|
bookmarks+=("${title}")
|
|
|
|
bookmarks+=("${i##* }")
|
2024-06-12 22:56:44 -04:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
if [[ -r "${config}/i3/bookmarks" ]]; then
|
2024-06-12 23:36:03 -04:00
|
|
|
mapfile -t contents < "${config}/i3/bookmarks"
|
2024-06-12 22:56:44 -04:00
|
|
|
for i in "${contents[@]}" ; do
|
2024-06-12 23:36:03 -04:00
|
|
|
bookmarks+=("${i% *}" "${i##* }")
|
2024-06-12 22:56:44 -04:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2024-06-12 23:36:03 -04:00
|
|
|
# Run yad to display the dialog
|
2024-06-12 22:56:44 -04:00
|
|
|
url=$(yad --list \
|
|
|
|
--title="I38 - Bookmarks" \
|
|
|
|
--text="Select a bookmark to open" \
|
|
|
|
--column="Title" \
|
|
|
|
--column="URL" \
|
|
|
|
--button="Open Bookmark:0" \
|
|
|
|
--button="Add Bookmark:1" \
|
|
|
|
--button="Cancel:2" \
|
|
|
|
--close-on-unfocus \
|
|
|
|
--hide-column=2 \
|
|
|
|
--search-column=1 \
|
|
|
|
--skip-taskbar \
|
|
|
|
"${bookmarks[@]}")
|
|
|
|
yadCode="$?"
|
|
|
|
url="${url%|*}"
|
|
|
|
url="${url##*|}"
|
|
|
|
case ${yadCode} in
|
|
|
|
0) xdg-open "${url}";;
|
|
|
|
1) add_bookmark;;
|
|
|
|
2) exit 0;;
|
|
|
|
esac
|
2024-06-12 23:36:03 -04:00
|
|
|
|