Guest User

Untitled

a guest
Dec 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # Provides a --files-from option: rgf --files-from=[-|FILELIST]
  4. # The list of files to be searched is specified in FILELIST and must be separated by newlines.
  5. # If FILELIST is "-", the list is loaded from standard input.
  6. # This option may be specified multiple times.
  7. #
  8. # Note that this affects how the -t option is applied. When --files-from is specified,
  9. # -t is used to filter the list of files and then ripgrep searches for PATTERN on the list of filtered files
  10. #
  11. # * rgf --files-from=FILELIST PATTERN
  12. # - Search for PATTERN in all files specified in FILELIST
  13. #
  14. # * cat FILELIST | rgf --files-from=- PATTERN
  15. # rgf --files-from=- PATTERN < FILELIST
  16. # - Search for PATTERN in all files specified in FILELIST
  17. #
  18. # * rgf --files-from=- -tFILETYPE PATTERN < FILELIST
  19. # - Filter FILELIST for all files of type FILETYPE and then search for PATTERN on the filtered output
  20.  
  21. re=\(-h\|--help\)\\b
  22. if [[ "$*" =~ $re ]]; then
  23. command rg "$@"
  24. echo "
  25. Options added by rgf:
  26. --files-from=FILE The list of files to be searched is specified in FILE (similar to ack)
  27. The list of files must be separated by newlines.
  28. If FILE is "-", the list is loaded from standard input.
  29. When used together with --type=TYPE, files specified in FILE
  30. are filtered by TYPE and ripgrep is then run on the filtered output"
  31. exit 0
  32. elif [[ ! "$*" =~ '--files-from' ]]; then
  33. command rg "$@"
  34. exit $!
  35. fi
  36.  
  37. local _ft_filt=()
  38. local _flist=()
  39. local _rg_opts=()
  40.  
  41. while (( $# > 0 )); do
  42. if [[ "$1" =~ '--files-from=' ]]; then
  43. local _f=$(sed 's/--files-from=//' <<< "$1")
  44. _flist+=("$_f")
  45. elif [[ "$1" =~ '--files-from' ]]; then
  46. shift
  47. _flist+=("$1")
  48. elif [[ "$1" =~ [^-]-t[[:alpha:]]+ ]]; then
  49. _ft_filt+=($(sed -s 's/^-t//' <<< "$1"))
  50. elif [[ "$1" == "--type" ]]; then
  51. shift
  52. _ft_filt+=("$1")
  53. else
  54. _rg_opts+=("$1")
  55. fi
  56.  
  57. shift
  58. done
  59. # echo "FList: ${_flist[@]}"
  60. # echo "Types: ${_ft_filt[@]}"
  61. # echo "rg Opts: ${_rg_opts[@]}"
  62.  
  63. # Compile list of filetypes to filter filelist by
  64. local _ft_pat=""
  65. for _ft in "${_ft_filt[@]}"; do
  66. _ft_pat="${_ft_pat}|$(command rg --type-list | command rg "^$_ft" | cut -d: -f2 | tr -d '*.{ }' | tr ',' '|')"
  67. done
  68.  
  69. # Remove the leading |
  70. _ft_pat=${_ft_pat#|}
  71. # echo "FileType Pattern: $_ft_pat"
  72.  
  73. # Finally search for PATTERN on all the files
  74. for _f in ${_flist[@]}; do
  75. cat "$_f"
  76. done | command xargs -d '\n' rg "${_rg_opts[@]}"
Add Comment
Please, Sign In to add comment