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

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