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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. usage() {
  8. LESS=-EXR less <<-HELP
  9. pkgsearch [query]
  10. lists and installs packages from your distro's repositories
  11. without any arguments pkgsearch will list all available packages from your cache
  12. all arguments are passed as search strings
  13. note: on Arch Linux you must pass a string to query the AUR
  14. HELP
  15. }
  16. err() {
  17. echo -e "${c_red}$1${c_reset}" >&2
  18. }
  19. die() {
  20. exit 1
  21. }
  22. has() {
  23. local verbose=false
  24. if [[ $1 == '-v' ]]; then
  25. verbose=true
  26. shift
  27. fi
  28. for c in "$@"; do c="${c%% *}"
  29. if ! command -v "$c" &> /dev/null; then
  30. [[ "$verbose" == true ]] && err "$c not found"
  31. return 1
  32. fi
  33. done
  34. }
  35. select-from() {
  36. local cmd='command -v'
  37. for a in "$@"; do
  38. case "$a" in
  39. -c)
  40. cmd="$2"
  41. shift 2
  42. ;;
  43. esac
  44. done
  45. for c in "$@"; do
  46. if $cmd "${c%% *}" &> /dev/null; then
  47. echo "$c"
  48. return 0
  49. fi
  50. done
  51. return 1
  52. }
  53. fzf() {
  54. command fzf -e +s --multi --cycle --ansi --no-hscroll --inline-info --reverse "$@"
  55. }
  56. debian() {
  57. local out pkgs
  58. out=$(fzf --query="$1" < <( \
  59. apt-cache search '' |
  60. sort |
  61. sed -u -r "s|^([^ ]+)|${c_green}\1${c_reset}|" ) )
  62. mapfile -t pkgs < <(awk '{print $1}' <<< "$out")
  63. (( ${#pkgs} > 0 )) || exit
  64. sudo $(select-from 'apt' 'apt-get') install "${pkgs[@]}"
  65. }
  66. arch() {
  67. local search out pkgs
  68. search='pacman'
  69. (( $# > 0)) && search=$(select-from 'pacaur' 'yaourt' 'packer' 'apacman' 'pacman')
  70. out=$(fzf < <( $search -Ss "$1" |
  71. awk '{
  72. getline descr;
  73. sub(/ */,"", descr);
  74. repo = blue gensub(/\/.*/, "", 1) reset;
  75. name = green gensub(/.*\//, "", 1, $1) reset;
  76. info = gensub(/[^ ]* /, "", 1);
  77. print repo, "\t", name, info, descr;
  78. }' blue="$c_blue" green="$c_green" reset="$c_reset"
  79. )
  80. )
  81. mapfile -t pkgs < <(awk '{print $2}' <<< "$out")
  82. (( ${#pkgs} > 0 )) || exit
  83. echo "installing ${pkgs[*]}"
  84. ${search/pacman/sudo pacman} -S "${pkgs[@]}"
  85. }
  86. if [[ $1 == -h ]]; then
  87. usage
  88. exit
  89. fi
  90. has -v fzf || die
  91. request="$*"
  92. if [[ -f /etc/debian_version ]]; then
  93. debian "$request"
  94. elif [[ -f /etc/arch-release ]]; then
  95. arch "$request"
  96. fi