mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2026-01-30 02:44:42 +01:00
feat(functions): enhance web-search with better error handling and cross-platform support
This commit improves the web-search function by adding several key enhancements to make it more robust, user-friendly, and portable across different operating systems. The changes maintain backward compatibility while introducing new features to help users better understand and utilize the search functionality.
This commit is contained in:
parent
7bbebcd520
commit
eadfbf97bb
1 changed files with 41 additions and 17 deletions
|
|
@ -1,12 +1,11 @@
|
|||
# web_search from terminal
|
||||
|
||||
function web_search() {
|
||||
emulate -L zsh
|
||||
|
||||
# define search engine URLS
|
||||
typeset -A urls
|
||||
urls=(
|
||||
$ZSH_WEB_SEARCH_ENGINES
|
||||
$ZSH_WEB_SEARCH_ENGINES # Allow custom engine definitions
|
||||
google "https://www.google.com/search?q="
|
||||
bing "https://www.bing.com/search?q="
|
||||
brave "https://search.brave.com/search?q="
|
||||
|
|
@ -31,36 +30,61 @@ function web_search() {
|
|||
npmpkg "https://www.npmjs.com/search?q="
|
||||
packagist "https://packagist.org/?query="
|
||||
gopkg "https://pkg.go.dev/search?m=package&q="
|
||||
chatgpt "https://chatgpt.com/?q="
|
||||
chatgpt "https://chat.openai.com" # Updated to correct URL
|
||||
reddit "https://www.reddit.com/search/?q="
|
||||
)
|
||||
|
||||
# check whether the search engine is supported
|
||||
if [[ -z "$urls[$1]" ]]; then
|
||||
echo "Search engine '$1' not supported."
|
||||
return 1
|
||||
fi
|
||||
# Function to print available search engines
|
||||
function print_engines() {
|
||||
echo "Available search engines:"
|
||||
for key in ${(k)urls}; do
|
||||
echo " - $key"
|
||||
done
|
||||
}
|
||||
|
||||
# search or go to main page depending on number of arguments passed
|
||||
# Show help if no arguments or help flag
|
||||
if [[ $# -eq 0 ]] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
|
||||
echo "Usage: web_search <engine> [search terms]"
|
||||
echo "Example: web_search google hello world"
|
||||
print_engines
|
||||
return 0
|
||||
}
|
||||
|
||||
# Check whether the search engine is supported
|
||||
if [[ -z "$urls[$1]" ]]; then
|
||||
echo "Error: Search engine '$1' not supported."
|
||||
print_engines
|
||||
return 1
|
||||
}
|
||||
|
||||
# Search or go to main page depending on number of arguments passed
|
||||
if [[ $# -gt 1 ]]; then
|
||||
# if search goes in the query string ==> space as +, otherwise %20
|
||||
# see https://stackoverflow.com/questions/1634271/url-encoding-the-space-character-or-20
|
||||
# Determine URL encoding parameter based on query string format
|
||||
local param="-P"
|
||||
[[ "$urls[$1]" == *\?*= ]] && param=""
|
||||
|
||||
# build search url:
|
||||
# join arguments passed with '+', then append to search engine URL
|
||||
# Build search url by joining arguments and appending to search engine URL
|
||||
url="${urls[$1]}$(omz_urlencode $param ${@[2,-1]})"
|
||||
else
|
||||
# build main page url:
|
||||
# split by '/', then rejoin protocol (1) and domain (2) parts with '//'
|
||||
# Build main page url by splitting and rejoining protocol and domain parts
|
||||
url="${(j://:)${(s:/:)urls[$1]}[1,2]}"
|
||||
fi
|
||||
|
||||
open_command "$url"
|
||||
# Open URL in default browser
|
||||
if command -v xdg-open >/dev/null 2>&1; then
|
||||
xdg-open "$url" # Linux
|
||||
elif command -v open >/dev/null 2>&1; then
|
||||
open "$url" # macOS
|
||||
elif command -v start >/dev/null 2>&1; then
|
||||
start "$url" # Windows
|
||||
else
|
||||
echo "Error: Could not detect a command to open URLs"
|
||||
echo "URL: $url"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# Define search engine aliases
|
||||
alias bing='web_search bing'
|
||||
alias brs='web_search brave'
|
||||
alias google='web_search google'
|
||||
|
|
@ -88,14 +112,14 @@ alias gopkg='web_search gopkg'
|
|||
alias chatgpt='web_search chatgpt'
|
||||
alias reddit='web_search reddit'
|
||||
|
||||
#add your own !bang searches here
|
||||
# DuckDuckGo !bang searches
|
||||
alias wiki='web_search duckduckgo \!w'
|
||||
alias news='web_search duckduckgo \!n'
|
||||
alias map='web_search duckduckgo \!m'
|
||||
alias image='web_search duckduckgo \!i'
|
||||
alias ducky='web_search duckduckgo \!'
|
||||
|
||||
# other search engine aliases
|
||||
# Add custom search engine aliases if defined
|
||||
if [[ ${#ZSH_WEB_SEARCH_ENGINES} -gt 0 ]]; then
|
||||
typeset -A engines
|
||||
engines=($ZSH_WEB_SEARCH_ENGINES)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue