websearch 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/bash
  2. #
  3. # Requires Surfraw
  4. # Default search engine. If we really are at a loss, use this
  5. DEFAULT_SEARCH=duckduckgo
  6. # The location of the surfraw bookmark file
  7. BOOKMARKS="$HOME/.surfraw.bookmarks"
  8. # Colors for dmenu
  9. COLORS=" -sf #00ff00 -sb black -nb black"
  10. # Prompt for dmenu
  11. PROMPT="Search Web"
  12. # Use surfraw to search for the words
  13. function search()
  14. {
  15. # Does surfraw know what to do with it?
  16. url=`surfraw $@`
  17. # If not, then use the default search engine
  18. if [ $? -ne 0 ]
  19. then
  20. url=`surfraw $DEFAULT_SEARCH $@`
  21. fi
  22. echo $url
  23. }
  24. # We assume that this is a URL
  25. function goto()
  26. {
  27. case "$1" in
  28. *:*) echo $1 ;;
  29. *) echo "http://$1" ;;
  30. esac
  31. }
  32. # Use dmenu to navigate through possible choices
  33. function present_menu()
  34. {
  35. elvi=`surfraw -elvi | cut -f 1 | tail --lines=+2`
  36. if [ -r "$BOOKMARKS" ]
  37. then
  38. bookmarks=" `cut -f 1 -d ' ' "$BOOKMARKS" 2>/dev/null`"
  39. else
  40. bookmarks=""
  41. fi
  42. echo "${elvi}${bookmarks}" | tr ' ' '\n' | sort | dmenu -p "$PROMPT" -i $COLORS
  43. }
  44. present_menu | \
  45. ( read car cdr
  46. test -z "$car" && exit 1
  47. ( test -z "$cdr" && echo $car | fgrep -c '.
  48. :' > /dev/null && goto $car ) || search "$car $cdr" )