LAUNCHING AN INTERNET SEARCH QUERY WITH I3 AND DMENU
       
       
       
       
       Overview
       ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       I wrote a tiny script to launch internet search queries from my i3
       window manager. It's dead simple: invoke the script with a key command
       and you are prompted to enter your search terms. If you've already
       used the script before, a history of the last 10 queries are
       shown. Select a query or enter a new one and then you're prompted to
       select the program that should conduct the query: Firefox, W3M, Lynx,
       or Surf. Then the query is launched and you're riding the sweet, sweet
       waves of internet knowledge!
       
       
       The Code
       ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       The script is written in Bash (a favourite!) because for me that's fun
       and dirty. Awk is sprinkled in for easy pattern matching
       magic. There's really nothing else too surprising about this tiny
       prog, although if you want to use it you made need to make some
       tweaks. For instance, I'm using urxvt as the terminal to launch Lynx
       and W3M. If you don't use urxvt replace that bit with the terminal of
       your choice.
       
       ,----
       | #!/bin/bash
       | #
       | # Title: Untitled
       | # Author: Roygbyte
       | # Description: Smol script to easily and quickly launch a search query.
       | # Date: April, 2023
       | # Version: 0.1
       | #
       | # Installation
       | # =========
       | # Place file inside user's bin folder, i.e.: /home/someone/bin. Make the file executable with
       | # `chmod +x search-dmenu.sh`. Then, bind a key to the file in your i3 conf, e.g.:
       | # `bindsym $mod+q exec search-dmenu.sh`.
       | #
       | # Configuration
       | # ===========
       | # Variables of interest are `SEARCH_URL`, which defines the URL used for conduct the
       | # search query. By default, it's DuckDuckGo. `SEARCH_HANDLER`, which contains a string
       | # of new-line separated search handlers. By default, it includes FireFox, W3M, Lynx,
       | # and Suckless' Surf. If you change the search handlers, you may also need to change
       | # the case block to match. Oh: if you're running with the defaults, at least double-check
       | # that the case block dispatcher for the search handlers is using the right terminal for you.
       | # By default, it uses urxvt to dispatch W3M and Lynx.
       | 
       | HISTORY="/home/$USER/bin/.search-history"
       | HISTORY_COUNT=10
       | DMENU_OPTIONS=" -b -fn Iosevka-18 -nb #000000 -nf #FFFFFF -sb #0000FF"
       | 
       | function add_history_item() {
       |         SEARCH_TERM="$1" # Use $1, which is argument.
       |         if [ ! -a "$HISTORY" ]; then
       |                 touch "$HISTORY"
       |         fi
       |         # Use awk to print out a new history file. Current search is added to top of list.
       |         # Then the current search term is omitted in any other occurances in the list.
       |         # Awk, we love it. It makes using more complicated software feel awk-ward.
       |         cat $HISTORY | head -n $HISTORY_COUNT| awk -v search_term="$SEARCH_TERM" 'BEGIN {print search_term;} !match($0, search_term) { print $0; }' > "$HISTORY.temp"
       |         mv "$HISTORY.temp" $HISTORY
       | }
       | 
       | SEARCH_TERM=$(cat $HISTORY | dmenu -p "Search" -l $HISTORY_COUNT $DMENU_OPTIONS)
       | 
       | if [ -z $SEARCH_TERM ]; then
       |         # Exit on ESC, producing empty string
       |         exit;
       | fi
       | 
       | add_history_item "$SEARCH_TERM"
       | 
       | SEARCH_URL="https://duckduckgo.com/?q=$SEARCH_TERM"
       | SEARCH_HANDLER=$(echo -e "Firefox\nW3M\nLynx\nSurf" | dmenu -p "With" $DMENU_OPTIONS)
       | 
       | if [ -z $SEARCH_HANDLER ]; then
       |         exit;
       | fi
       | 
       | case $SEARCH_HANDLER in
       |         Firefox) firefox "$SEARCH_URL" ;;
       |         Lynx) urxvt -e lynx "$SEARCH_URL" ;;
       |         W3M) urxvt -e w3m "$SEARCH_URL" ;;
       |         Surf) surf -B -g -f -a @ "$SEARCH_URL" ;;
       | esac
       `----
       
       
       Installation
       ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       Download the script and stash it somewhere handy. A good place could
       be `~/.config/i3/'. Then bind a keystroke to the command like this:
       
       ,----
       | bindsym $mod+q exec search-dmenu.sh
       `----
       
       Restart i3 with `$mod+Shift+P' (or however you've got it set), and
       then you're good to go!