You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pkgsearch 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env bash
  2. declare esc=$(printf '\033')
  3. declare c_reset="${esc}[0m"
  4. declare c_red="${esc}[31m"
  5. declare c_green="${esc}[32m"
  6. declare c_blue="${esc}[34m"
  7. declare reverse
  8. usage() {
  9. LESS=-EXR less <<HELP
  10. pkgsearch [options] [query]
  11. lists and installs packages from your distro's repositories
  12. -r --reverse reverse the orientation of the layout
  13. -h --help show this help
  14. without any arguments pkgsearch will list all available packages from your cache
  15. note: on Arch Linux you must pass a string to query the AUR
  16. HELP
  17. }
  18. err() {
  19. echo -e "${c_red}$1${c_reset}" >&2
  20. }
  21. die() {
  22. exit 1
  23. }
  24. has() {
  25. local verbose=false
  26. if [[ $1 == '-v' ]]; then
  27. verbose=true
  28. shift
  29. fi
  30. for c in "$@"; do c="${c%% *}"
  31. if ! command -v "$c" &> /dev/null; then
  32. [[ "$verbose" == true ]] && err "$c not found"
  33. return 1
  34. fi
  35. done
  36. }
  37. select-from() {
  38. local cmd='command -v'
  39. for a in "$@"; do
  40. case "$a" in
  41. -c)
  42. cmd="$2"
  43. shift 2
  44. ;;
  45. esac
  46. done
  47. for c in "$@"; do
  48. if $cmd "${c%% *}" &> /dev/null; then
  49. echo "$c"
  50. return 0
  51. fi
  52. done
  53. return 1
  54. }
  55. fzf() {
  56. command fzf -e +s --multi --cycle --ansi \
  57. ${reverse:+--reverse} \
  58. --no-hscroll --inline-info \
  59. --header='tab to select multiple packages, Ctrl-X for more info on a package' "$@"
  60. }
  61. install() {
  62. local installer pkgs
  63. installer="$1"
  64. mapfile -t pkgs
  65. (( ${#pkgs} > 0 )) || exit
  66. echo "installing ${pkgs[*]}"
  67. $installer "${pkgs[@]}" < /dev/tty
  68. }
  69. debian() {
  70. fzf --bind="Ctrl-X:execute[p={} ;
  71. less < <(apt-cache show \$(awk '{print \$1}' <<< \$p)) > /dev/tty]" \
  72. --query="$1" < <(apt-cache search '' |
  73. sort |
  74. sed -u -r "s|^([^ ]+)|${c_green}\1${c_reset}|") |
  75. cut -d' ' -f1 |
  76. install "sudo $(select-from 'apt' 'aptitude' 'apt-get') install"
  77. }
  78. arch() {
  79. local search
  80. search='pacman'
  81. [[ -n "$1" ]] && search=$(select-from 'pacaur' 'yaourt' 'packer' 'apacman' 'pacman')
  82. fzf \
  83. --bind="Ctrl-X:execute[p={} ;
  84. less < <($search -Si \$(awk '{print \$2}' <<< \$p)) > /dev/tty]" \
  85. < <( $search -Ss "$1" |
  86. awk '{
  87. getline descr;
  88. sub(/ */,"", descr);
  89. repo = blue "[" gensub(/\/.*/, "", 1) "]" reset;
  90. name = green gensub(/.*\//, "", 1, $1) reset;
  91. info = gensub(/[^ ]* /, "", 1);
  92. print repo, name, info, descr;
  93. }' blue="$c_blue" green="$c_green" reset="$c_reset"
  94. ) |
  95. cut -d' ' -f2 |
  96. install "${search/pacman/sudo pacman} -S"
  97. }
  98. while true; do
  99. case "$1" in
  100. -r|--reverse) reverse=1 ; shift ;;
  101. -h|--help) usage ; exit ;;
  102. *) break
  103. esac
  104. done
  105. has -v fzf || die
  106. request="$*"
  107. if [[ -f /etc/debian_version ]]; then
  108. debian "$request"
  109. elif [[ -f /etc/arch-release ]]; then
  110. arch "$request"
  111. fi