URI: 
       dsearch - dsearch - Simple script to launch and remember search queries using dmenu.
   DIR Log
   DIR Files
   DIR Refs
       ---
       dsearch (2912B)
       ---
            1 #!/bin/bash
            2 #
            3 # Title: Untitled
            4 # Author: Roygbyte
            5 # Description: Smol script to easily and quickly launch a search query.
            6 # Date: April, 2023
            7 # Version: 0.2
            8 #
            9 # Installation
           10 # =========
           11 # Place file inside user's bin folder, i.e.: /home/someone/bin. Make the file executable with
           12 # `chmod +x search-dmenu.sh`. Then, bind a key to the file in your i3 conf, e.g.:
           13 # `bindsym $mod+q exec search-dmenu.sh`.
           14 #
           15 # Configuration
           16 # ===========
           17 # Variables of interest are `SEARCH_URL`, which defines the URL used for conduct the
           18 # search query. By default, it's DuckDuckGo. `SEARCH_HANDLER`, which contains a string
           19 # of new-line separated search handlers. By default, it includes FireFox, W3M, Lynx,
           20 # and Suckless' Surf. If you change the search handlers, you may also need to change
           21 # the case block to match. Oh: if you're running with the defaults, at least double-check
           22 # that the case block dispatcher for the search handlers is using the right terminal for you.
           23 # By default, it uses urxvt to dispatch W3M and Lynx.
           24 
           25 HISTORY="/home/$USER/bin/.search-history"
           26 HISTORY_TO_KEEP=10000
           27 HISTORY_TO_SHOW=10
           28 DMENU_OPTIONS=" -b -fn Iosevka-18 -nb #000000 -nf #FFFFFF -sb #0000FF"
           29 
           30 function add_history_item() {
           31         SEARCH_TERM="$1" # Use $1, which is argument.
           32         if [ ! -a "$HISTORY" ]; then
           33                 touch "$HISTORY"
           34         fi
           35         # Use awk to print out a new history file. Current search is added to top of list.
           36         # Then the current search term is omitted in any other occurances in the list.
           37         # Awk, we love it. It makes using more complicated software feel awk-ward.
           38         cat $HISTORY | head -n $HISTORY_TO_KEEP| awk -v search_term="$SEARCH_TERM" 'BEGIN {print search_term;} !match($0, search_term) { print $0; }' > "$HISTORY.temp"
           39         mv "$HISTORY.temp" $HISTORY
           40 }
           41 
           42 SEARCH_TERM=$(cat $HISTORY | dmenu -p "Search" -l $HISTORY_TO_SHOW $DMENU_OPTIONS)
           43 
           44 if [ -z $SEARCH_TERM ]; then
           45         # Exit on ESC, producing empty string
           46         exit;
           47 fi
           48 
           49 add_history_item "$SEARCH_TERM"
           50 
           51 WEB_SEARCH_URL="https://duckduckgo.com/?q=$SEARCH_TERM"
           52 GOPHER_SEARCH_URL="gopher://gopher.icu/7/quarry?$SEARCH_TERM"
           53 SEARCH_HANDLER=$(echo -e "Gopher\nFirefox\nW3M\nLynx\nSurf\nYewTube" | dmenu -p "With" $DMENU_OPTIONS)
           54 
           55 if [ -z $SEARCH_HANDLER ]; then
           56         exit;
           57 fi
           58 
           59 case $SEARCH_HANDLER in
           60         Gopher) urxvt -e lynx "$GOPHER_SEARCH_URL" ;;
           61         Firefox) firefox "$WEB_SEARCH_URL" ;;
           62         W3M) urxvt -e w3m "$WEB_SEARCH_URL" ;;
           63         Lynx) urxvt -e lynx "$WEB_SEARCH_URL" ;;
           64         #Lynx) urxvt -e tmux -c "lynx \"$WEB_SEARCH_URL"\" ;;
           65         Surf) surf -B -g -f -a @ "$WEB_SEARCH_URL" ;;
           66         YewTube) firefox "https://yewtu.be/search?q=$SEARCH_TERM" ;;
           67 esac
           68 
           69          # Ugh, I would like to use something better for settingup the search handlers, like an array. But Idk.
           70          # declare -A SEARCH_HANDLERS
           71          # SEARCH_HANDLERS[0]="Firefox|firefox"
           72          # SEARCH_HANDLERS[1]="W3M|urxvt -e w3m"
           73 
           74          # SEARCH_HANDLERS=([firefox]=firefox
           75          #                                 [w3m]="urxvt -e w3m") # Colud also declare the array with `declare -A SEARCH_HANDLERS`