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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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=-FEXR 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. printf "${c_red}%s${c_reset}\n" "$*" >&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 pkgs count
  63. mapfile -t pkgs
  64. (( ${#pkgs} > 0 )) || exit
  65. count="${#pkgs[@]} package"
  66. (( ${#pkgs[@]} > 1 )) && count+='s'
  67. printf "installing %s: %s\n" "$count" "${pkgs[*]}"
  68. $1 "${pkgs[@]}" < /dev/tty
  69. }
  70. debian() {
  71. fzf --bind="Ctrl-X:execute[
  72. p=\$(cut -d' ' -f1 <<< {});
  73. less < <(apt-cache show \$p) > /dev/tty]" \
  74. --query="$1" \
  75. < <(apt-cache search '' | sort |
  76. sed -u -r "s|^([^ ]+)|${c_green}\1${c_reset}|") |
  77. cut -d' ' -f1 |
  78. install "sudo $(select-from 'apt' 'aptitude' 'apt-get') install"
  79. }
  80. arch() {
  81. local search packages
  82. search='pacman'
  83. [[ -n "$1" ]] && search=$(select-from 'pacaur' 'yaourt' 'packer' 'apacman' 'pacman')
  84. packages=$(fzf --bind="Ctrl-X:execute[
  85. p=\$(cut -d' ' -f2 <<< {});
  86. less < <($search -Si \$p) > /dev/tty]" \
  87. < <( $search -Ss "$1" |
  88. gawk '{
  89. getline descr;
  90. sub(/ */,"", descr);
  91. repo = blue "[" gensub(/\/.*/, "", 1) "]" reset;
  92. name = green gensub(/.*\//, "", 1, $1) reset;
  93. info = gensub(/[^ ]* /, "", 1);
  94. print repo, name, info, descr;
  95. }' blue="$c_blue" green="$c_green" reset="$c_reset"
  96. ) | cut -d' ' -f2)
  97. [[ "$search" == "pacman" ]] && search="sudo pacman"
  98. install "$search -S" <<< "$packages"
  99. }
  100. void() {
  101. local packagelist packagename='{ sub(/-[^\-]*$/, "", $2); print $2 }'
  102. packagelist=$(xbps-query -Rs '' | sort)
  103. fzf --bind="Ctrl-X:execute[
  104. p=\$(awk \"$packagename\" <<< {});
  105. less < <(xbps-query -Rx \$p) > /dev/tty]" <<< "$packagelist" |
  106. awk "$packagename" |
  107. install 'xbps-install -S'
  108. }
  109. while true; do
  110. case "$1" in
  111. -r|--reverse) reverse=1 ; shift ;;
  112. -h|--help) usage ; exit ;;
  113. *) break
  114. esac
  115. done
  116. has -v fzf gawk || die
  117. request="$*"
  118. osrelease=$(awk -F'=' '/^NAME/{gsub(/"/, "", $2); print tolower($2)}' /etc/os-release)
  119. case "${osrelease%% *}" in
  120. debian|ubuntu) debian "$request" ;;
  121. arch) arch "$request" ;;
  122. void) void "$request" ;;
  123. esac