torchooser 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/bash
  2. # Torchooser
  3. # If stdin is not a tty, start it in a terminal - unix.stackexchange.com/a/26691
  4. if ! [ -t 0 ]; then
  5. exec ${XTERMINAL-xterm} -sh 1.3 -geometry 70x24 -T "${0##*/}" -e sh -c "$0 \"$@\"" "$@"
  6. fi
  7. # if we have a command line argument, and it is "-x", then get content of clipboard
  8. # otherwise use it as is...
  9. [ -n "$1" ] && { [[ "$1" == "-x" ]] && str="$(xclip -o)" || str="$1"; }
  10. # ... but only the 1st line
  11. str="${str%%$'\n'*}"
  12. torstart() {
  13. [ -z "$1" ] && return
  14. cd "$HOME/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/"
  15. ./start-tor-browser.desktop "$1"
  16. }
  17. array=()
  18. helpy=()
  19. if [ -n "$str" ]; then
  20. if [ -r "$str" ]; then
  21. mapfile -t array < "$str"
  22. else
  23. #~ while read line; do break; done <<<"$1"
  24. array=( "$str" "https://duckduckgo.com/?q=$str" "https://thepiratebay.org/search.php?video=on&q=$str" ) # only read until first newline
  25. fi
  26. fi
  27. for file in "$(dirname "$0")/urls" "$XDG_CONFIG_HOME/tor/urls"; do
  28. [ -r "$file" ] && mapfile -t helpy < "$file"
  29. done
  30. array=( "${array[@]}" "${helpy[@]}" )
  31. # array with 2 colors and reset to normal
  32. helpy=( "" "" "(B" )
  33. # intersperse list with alternating colors
  34. for ((i=0;i<${#array[@]};i++)); do ((i==${#array[@]}-1)) && array[i]="${array[i]}${helpy[2]}" || array[i]="${array[i]}${helpy[$((i%2))]}"; done
  35. PS3="Choice: "
  36. # Start with the 2nd color
  37. printf '%s' "${helpy[1]}"
  38. select url in "${array[@]}"; do
  39. # Remove escape sequences from back of Url:
  40. for i in "${helpy[@]}"; do url="${url%$i}"; done
  41. torstart "$url"
  42. done