Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- HISTORY="$HOME/.cache/search_history"
- save_history(){ { xargs <<< "$@"; cat "$HISTORY"; } | awk '!x[$0]++' | sponge "$HISTORY"; }
- # Get the query using the clipboard ──────────────────────────────────────────────────────────────
- if [ "$1" = "clipboard" ]; then
- RAW_SEARCH_ITEMS="$( \
- printf %q \
- "$( xsel --output \
- | sed --regexp-extended 's/([^[:print:]])//g;s#^\s*##;s#\s*$##' )" \
- | xargs )"
- SEARCH_ITEMS="${RAW_SEARCH_ITEMS//\\}"
- save_history "${SEARCH_ITEMS##;}"
- # Get the query asking for it ────────────────────────────────────────────────────────────────────
- elif [ "$1" = "askme" ]; then
- ASKME_HELP="<span font_family='monospace' size='x-small'> ctrl+space : edit previous search
- shift+enter : multiple selection
- ';' : (first chars) don't save
- ';' : (elsewhere) skip suggestion
- '+' : multiple queries</span>"
- RAW_SEARCH_ITEMS=$(printf %q \
- "$(rofi -dmenu -input "$HISTORY" -p "Search" \
- -mesg "$ASKME_HELP" -multi-select \
- | sed --regexp-extended \
- --expression='s/([^[:print:]])//g;s#^\s*##;s#\s*$##' \
- --expression=':a' --expression=N --expression='$!ba' \
- --expression='s/\n/+/g' \
- )")
- [ "${#RAW_SEARCH_ITEMS}" -le 2 ] && exit 1
- # Remove every ";" , the rofi suggestions override
- SEARCH_ITEMS="${RAW_SEARCH_ITEMS//;/ }"
- SEARCH_ITEMS="${SEARCH_ITEMS//\\}"
- # and save it.
- # Do not save queries with ";" in front of them
- [[ $RAW_SEARCH_ITEMS != ';'* ]] && save_history "$SEARCH_ITEMS"
- # No query? Then quit. Or collect the queries ────────────────────────────────────────────────────
- fi; [ "${#SEARCH_ITEMS}" -eq 0 ] && exit 1
- readarray -td '' QUERY_LIST < <(awk '{ gsub(/+/,"\0"); print; }' <<<"$SEARCH_ITEMS+"); unset 'QUERY_LIST[-1]'
- # Collect the engines ────────────────────────────────────────────────────────────────────────────
- ENGINES_HELP="<span font_family='monospace' size='x-small'> shift-enter : multiple selection</span>"
- mapfile -t ENGINES < <( \
- buku --nostdin --np --stag --tacit \
- | awk -F'.' '!/^$|cansearch/ {$1="";print}' \
- | rofi -dmenu -sorting-method fzf -sort -mesg "$ENGINES_HELP" \
- -multi-select -p "Search ${SEARCH_ITEMS:0:12}… " \
- | cut --fields=1 --delimiter="(" \
- )
- [ "${#ENGINES}" -eq 0 ] && exit 2
- # Do the searches ────────────────────────────────────────────────────────────────────────────────
- wmctrl -x -a Firefox || ( (nohup firefox &) && sleep 8)
- IFS=$'\n'
- for each_query_word in "${QUERY_LIST[@]}"; do
- for each_engine in "${ENGINES[@]}" ; do
- for each_url in $(buku --nostdin --np --format 10 --stag "cansearch + $each_engine" --tacit \
- | sed "s#\%s#$each_query_word#g"); do
- sleep .1$RANDOM && firefox --new-tab "$each_url"
- done; done; done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement