#!/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 (google, bing, yahoo, duckduckgo, 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 | 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=" ;; 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