133 lines
4.6 KiB
Bash
133 lines
4.6 KiB
Bash
|
#!/usr/bin/env bash
|
||
|
|
||
|
add_bookmark() {
|
||
|
while : ; do
|
||
|
if [[ -r ~/.config/surfraw/bookmarks ]] && [[ -f ~/.config/i3/bookmarks ]]; then
|
||
|
# 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
|
||
|
if [[ "$title" =~ \ ]] && [[ "$bookmarkFile" =~ surfraw ]]; then
|
||
|
newTitle="${title//[[:space:]]/_}"
|
||
|
yad --question --title="I38 - Bookmarks" --text="The title contains spaces, which have been converted to underscores: \"$new_title\". Is this okay?"
|
||
|
yadCode="$?"
|
||
|
if [[ $yadCode -ne 0 ]]; then
|
||
|
continue
|
||
|
else
|
||
|
title="$newTitle"
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
# Determine the bookmarks file based on radio button selection
|
||
|
if [ -f ~/.config/surfraw/bookmarks ] && [ -f ~/.config/i3/bookmarks ]; then
|
||
|
if [[ "$bookmark_file" == "Surfraw Bookmark" ]]; then
|
||
|
bookmarks_file=~/.config/surfraw/bookmarks
|
||
|
else
|
||
|
bookmarks_file=~/.config/i3/bookmarks
|
||
|
fi
|
||
|
else
|
||
|
bookmarks_file=~/.config/surfraw/bookmarks
|
||
|
[ -f ~/.config/i3/bookmarks ] && bookmarks_file=~/.config/i3/bookmarks
|
||
|
fi
|
||
|
|
||
|
# Check for duplicates
|
||
|
if grep -q "$url" "$bookmarksFile" ; then
|
||
|
existingTitle=$(grep "$url" "$bookmarks_file" | cut -d' ' -f1)
|
||
|
yad --info --title="I38 - Bookmarks" --text="This site is already bookmarked as \"$existingTitle\"."
|
||
|
return
|
||
|
elif grep -qi "^$title " "$bookmarksFile"; then
|
||
|
yad --info --title="I38 Bookmarks" --text="A bookmark with the title \"$title\" already exists."
|
||
|
return
|
||
|
fi
|
||
|
|
||
|
# Add the new bookmark
|
||
|
echo "$title $url" >> "$bookmarks_file"
|
||
|
|
||
|
# Ensure no duplicates with sort -u
|
||
|
sort -u "$bookmarks_file" -o "$bookmarks_file"
|
||
|
yad --info --title="I38 - Bookmarks" --selectable-lables --text="Bookmark \"$title\" added successfully."
|
||
|
return
|
||
|
done
|
||
|
}
|
||
|
|
||
|
|
||
|
declare -a bookmarks
|
||
|
export config="${XDG_CONFIG_HOME:-${HOME}/.config}"
|
||
|
if [[ -r "${config}/surfraw/bookmarks" ]]; then
|
||
|
mapfile -t contents < <(cat "${config}/surfraw/bookmarks")
|
||
|
for i in "${contents[@]}" ; do
|
||
|
title="${i% *}"
|
||
|
title="${title//_/ }"
|
||
|
bookmarks+=("${title}")
|
||
|
bookmarks+=("${i##* }")
|
||
|
done
|
||
|
fi
|
||
|
if [[ -r "${config}/i3/bookmarks" ]]; then
|
||
|
mapfile -t contents < <(cat "${config}/i3/bookmarks")
|
||
|
for i in "${contents[@]}" ; do
|
||
|
bookmarks+=("${i% *}" "${i##* }")
|
||
|
done
|
||
|
fi
|
||
|
declare -a menuList
|
||
|
|
||
|
|
||
|
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
|