Skip to content

Instantly share code, notes, and snippets.

@koonix
Created September 10, 2022 22:00
Show Gist options
  • Save koonix/d3ff046c4121a7cb9e9f33a50da43ef7 to your computer and use it in GitHub Desktop.
Save koonix/d3ff046c4121a7cb9e9f33a50da43ef7 to your computer and use it in GitHub Desktop.
like surfraw, but not a million fucking lines long.
#!/bin/bash
# like surfraw, but not a million fucking lines long.
# requires dmenu.
# the default search engine is shown at first.
# to select another search engine, press enter when
# the prompt is empty in dmenu.
# name of the default search engine
default='Searx'
# array of search engines
# if a search engine has multiple URLs, one of them is used at random
declare -A engines=(
[Searx]='
https://searx.tiekoetter.com/search?q=%s
https://searx.be/search?q=%s'
[1337x]='https://1337x.to/srch?search=%s'
[CloudTorrents]='https://cloudtorrents.com/search?query=%s'
[PB]='https://thepiratebay.org/search.php?q=%s'
[YTS]='https://yts.mx/search-movies?query=%s'
[Arch Wiki]='https://wiki.archlinux.org/index.php?search=%s'
[AUR]='https://aur.archlinux.org/packages/?K=%s'
[Arch Pkg]='https://archlinux.org/packages/?q=%s'
[Github]='https://github.com/search?q=%s'
[Chocolatey]='https://chocolatey.org/packages?q=%s'
[Firefox Addons]='https://addons.mozilla.org/search/?q=%s'
[Reddit]='https://libreddit.tiekoetter.com/search?q=%s'
[Stack Exchange]='https://stackexchange.com/search?q=%s'
[Stack Overflow]='https://stackoverflow.com/search?q=%s'
[AllDataSheet]='https://www.alldatasheet.com/view.jsp?Searchword=%s'
[WineHQ]='https://www.winehq.org/search?q=%s'
[Wikipedia]='https://en.wikipedia.org/w/index.php?search=%s'
[Artix Pkg]='https://gitea.artixlinux.org/explore/repos?q=%s'
[YouTube]='https://piped.kavin.rocks/results?search_query=%s'
[Gentoo Wiki]='https://wiki.gentoo.org/index.php?search=%s'
[Odysee]='https://odysee.com/$/search?q=%s'
[KnowYourMeme]='https://odysee.com/$/search?q=%s'
)
colors='-sb #423e17'
set -e
# start with the default search engine
sel=$default
query=$(dmenu -p "Search $sel" $colors <&-)
# if the query is empty, ask for another search engine from the user
if [[ -z $query ]]; then
sel=$(printf '%s\n' "${!engines[@]}" | dmenu -p 'Choose an Engine' $colors)
query=$(dmenu -p "Search $sel" $colors <&-)
fi
# URL-encode the query
query=$(
while IFS= read -rn1 c; do
[[ -z $c ]] && continue
case $c in
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done <<< "$query"
)
# if multiple engine URLs are specified, choose one randomly
engine=()
while read -r line; do
case $line in
*[a-zA-Z]*) engine+=("$line") ;;
esac
done <<< "${engines[$sel]}"
engine=${engine[RANDOM % ${#engine[@]}]}
# open the search URL
exec ${BROWSER:-xdg-open} "$(printf "${engine##* }\n" "$query")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment