summaryrefslogtreecommitdiff
path: root/ar/.local/bin/browse
diff options
context:
space:
mode:
authorTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2025-01-24 20:35:27 +0900
committerTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2025-01-24 20:35:27 +0900
commitc80a54e42b52ce297f0f2f71af23c562832025c7 (patch)
treedcce8bb977a770f473325d48f6f70b21d9818a40 /ar/.local/bin/browse
init
Diffstat (limited to 'ar/.local/bin/browse')
-rwxr-xr-xar/.local/bin/browse85
1 files changed, 85 insertions, 0 deletions
diff --git a/ar/.local/bin/browse b/ar/.local/bin/browse
new file mode 100755
index 0000000..56c36d9
--- /dev/null
+++ b/ar/.local/bin/browse
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+# Usage message
+usage() {
+ echo "Searches query in a terminal or browser"
+ echo ""
+ echo "Usage: ${0##*/} [-d | --ddgr | ddgr] [-h | --help | help] [<search_engine>] <search_query>"
+ 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 " <search_engine> : (Optional) Search engine to use (google, bing, yahoo, duckduckgo, youtube)"
+ echo " <search_query> : 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