Advertisement
Guest User

sbuku

a guest
Sep 18th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.20 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. HISTORY="$HOME/.cache/search_history"
  4. save_history(){ { xargs <<< "$@"; cat "$HISTORY"; } | awk '!x[$0]++' | sponge "$HISTORY"; }
  5.  
  6. # Get the query using the clipboard ──────────────────────────────────────────────────────────────
  7. if   [ "$1" = "clipboard" ]; then
  8.   RAW_SEARCH_ITEMS="$(                                                    \
  9.  printf %q                                                               \
  10.    "$( xsel --output                                                     \
  11.     | sed --regexp-extended 's/([^[:print:]])//g;s#^\s*##;s#\s*$##' )"    \
  12.  | xargs                                                                )"
  13.  
  14.   SEARCH_ITEMS="${RAW_SEARCH_ITEMS//\\}"
  15.   save_history "${SEARCH_ITEMS##;}"
  16.  
  17. # Get the query asking for it ────────────────────────────────────────────────────────────────────
  18. elif [ "$1" = "askme"     ]; then
  19.  
  20.   ASKME_HELP="<span font_family='monospace' size='x-small'>  ctrl+space  : edit previous search
  21.  shift+enter : multiple selection
  22.  ';'         : (first chars) don't save
  23.  ';'         : (elsewhere)   skip suggestion
  24.  '+'         : multiple queries</span>"
  25.  
  26.   RAW_SEARCH_ITEMS=$(printf %q                                          \
  27.     "$(rofi  -dmenu -input "$HISTORY"    -p    "Search"                 \
  28.                    -mesg "$ASKME_HELP"  -multi-select                  \
  29.      | sed --regexp-extended                                           \
  30.            --expression='s/([^[:print:]])//g;s#^\s*##;s#\s*$##'        \
  31.            --expression=':a' --expression=N --expression='$!ba'        \
  32.            --expression='s/\n/+/g'                                     \
  33.    )")
  34.  
  35.   [ "${#RAW_SEARCH_ITEMS}" -le 2 ] && exit 1
  36.  
  37.   # Remove every ";" , the rofi suggestions override
  38.   SEARCH_ITEMS="${RAW_SEARCH_ITEMS//;/ }"
  39.   SEARCH_ITEMS="${SEARCH_ITEMS//\\}"
  40.  
  41.   # and save it.
  42.   # Do not save queries with ";" in front of them
  43.   [[ $RAW_SEARCH_ITEMS != ';'* ]] && save_history "$SEARCH_ITEMS"
  44.  
  45. # No query? Then quit. Or collect the queries ────────────────────────────────────────────────────
  46. fi; [ "${#SEARCH_ITEMS}" -eq 0 ] && exit 1
  47. readarray -td '' QUERY_LIST < <(awk '{ gsub(/+/,"\0"); print; }' <<<"$SEARCH_ITEMS+"); unset 'QUERY_LIST[-1]'
  48.  
  49. # Collect the engines ────────────────────────────────────────────────────────────────────────────
  50.  
  51. ENGINES_HELP="<span font_family='monospace' size='x-small'>  shift-enter : multiple selection</span>"
  52.  
  53. mapfile   -t ENGINES < <(                                                                        \
  54.   buku   --nostdin --np --stag --tacit                                                           \
  55.   | awk -F'.' '!/^$|cansearch/ {$1="";print}'                                                    \
  56.   | rofi  -dmenu -sorting-method fzf -sort  -mesg "$ENGINES_HELP"                                \
  57.           -multi-select                     -p    "Search ${SEARCH_ITEMS:0:12}… "                \
  58.   | cut  --fields=1 --delimiter="("                                                              \
  59.   )
  60. [ "${#ENGINES}" -eq 0 ] && exit 2
  61.  
  62. # Do the searches ────────────────────────────────────────────────────────────────────────────────
  63.  
  64. wmctrl -x -a Firefox || ( (nohup firefox &) && sleep 8)
  65.  
  66. IFS=$'\n'
  67. for each_query_word in "${QUERY_LIST[@]}"; do
  68. for each_engine     in "${ENGINES[@]}"   ; do
  69. for each_url        in  $(buku --nostdin --np --format 10 --stag "cansearch + $each_engine" --tacit \
  70.                          | sed "s#\%s#$each_query_word#g"); do
  71.  
  72.   sleep .1$RANDOM && firefox --new-tab "$each_url"
  73.  
  74. done; done; done
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement