feat(web-search): add reddit engine 2.0

This commit is contained in:
Francesco Cataldo 2024-09-25 20:33:09 +02:00
parent 61bacd95b2
commit f85e1bce67
No known key found for this signature in database
GPG key ID: C2AFF61F3036CDB6

View file

@ -32,7 +32,6 @@ function web_search() {
packagist "https://packagist.org/?query=" packagist "https://packagist.org/?query="
gopkg "https://pkg.go.dev/search?m=package&q=" gopkg "https://pkg.go.dev/search?m=package&q="
chatgpt "https://chatgpt.com/?q=" chatgpt "https://chatgpt.com/?q="
reddit "https://www.reddit.com/search/?q="
) )
# check whether the search engine is supported # check whether the search engine is supported
@ -60,6 +59,50 @@ function web_search() {
open_command "$url" open_command "$url"
} }
# example usage: reddit swaywm (search all subreddits for swaywm)
# example usage: reddit --sr unixporn swaywm (search only unixporn subreddit for swaywm) -> https://www.reddit.com/r/unixporn/search?q=swaywm
function reddit_search() {
emulate -L zsh
local url="https://www.reddit.com"
local query=""
local subreddit=""
# if the user pass "--sr <subreddit name>" as first argument
# then the second argument is the subreddit name
if [[ "$1" == "--sr" ]]; then
subreddit="$2"
query="${@[3,-1]}"
else
query="${@[1,-1]}"
fi
# if query is empty, remove the search query
if [[ -z "$query" ]]; then
query=""
fi
# if subreddit is empty, search all subreddits
# but if query is empty, remove the search query
if [[ -z "$subreddit" ]]; then
if [[ -z "$query" ]]; then
url="$url"
else
url="$url/search?q=$(omz_urlencode -P $query)"
fi
else
# if subreddit is not empty, search only that subreddit
if [[ -z "$query" ]]; then
url="$url/r/$subreddit"
else
url="$url/r/$subreddit/search?q=$(omz_urlencode -P $query)"
fi
fi
open_command "$url"
}
alias bing='web_search bing' alias bing='web_search bing'
alias brs='web_search brave' alias brs='web_search brave'
@ -86,7 +129,7 @@ alias npmpkg='web_search npmpkg'
alias packagist='web_search packagist' alias packagist='web_search packagist'
alias gopkg='web_search gopkg' alias gopkg='web_search gopkg'
alias chatgpt='web_search chatgpt' alias chatgpt='web_search chatgpt'
alias reddit='web_search reddit' alias reddit='reddit_search'
#add your own !bang searches here #add your own !bang searches here
alias wiki='web_search duckduckgo \!w' alias wiki='web_search duckduckgo \!w'