#!/bin/sh # Usage message usage() { echo "Searches query in a terminal or browser" echo "" echo "Usage: ${0##*/} [-d | --ddgr | ddgr] [-h | --help | help] [] " echo "" echo "Options:" echo " -d, --ddgr, ddgr : Use ddgr to search and open the result in a terminal" echo " -h, --help, help : Display this help message" echo " : (Optional) Search engine to use (bing, duckduckgo, google, naver, yahoo, youtube)" echo " : The search term or query to use" } # Set default values search_tool="web" search_engine="searx" # Determine the open command based on the operating system case "$(uname -s)" in Darwin) open_cmd='open' ;; *) open_cmd='xdg-open' ;; esac # Check the first argument for flags or help using case case "$1" in -d | --ddgr | ddgr) # Check if ddgr is installed (only needed for ddgr options) if ! command -v ddgr >/dev/null 2>&1; then echo "Error: ddgr is not installed." >&2 exit 1 fi search_tool="ddgr" shift # Remove this argument from the list ;; -h | --help | help) usage && exit 0 ;; bing | duckduckgo | google | naver | yahoo | youtube) search_engine="$1" shift # Remove the search engine from the list ;; esac # Store the remaining arguments as the search query search_query="$*" # Ensure a search query is provided; if not, show usage [ -z "$search_query" ] && usage && exit 1 # Execute the corresponding search tool using case case $search_tool in ddgr) # Run DuckDuckGo search in the terminal setsid -f "$TERMINAL" -e ddgr "$search_query" ;; web) # Construct the URL based on the search engine case "$search_engine" in bing | google | yahoo | youtube) base_url="https://www.${search_engine}.com/search?q=" ;; duckduckgo) base_url="https://duckduckgo.com/?q=" ;; naver) base_url="https://search.naver.com/search.naver?query=" ;; searx | *) base_url="https://www.searx.thesiah.xyz/search?q=" ;; esac # Encode the search query search_query_encoded=$(echo "$search_query" | sed 's/ /+/g') # Open the search URL in the default browser $open_cmd "${base_url}${search_query_encoded}" ;; *) usage && exit 1 ;; esac