Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.47 KB | None | 0 0
  1. #!/bin/zsh
  2.  
  3. # search - A semi-complicated search script
  4.  
  5. # Usage:
  6. #  search PATTERN
  7. #  Search for PATTERN in all files under cwp (except .git, node_modules...)
  8.  
  9. # Copyright (c) 2017 Marcel Robitaille <[email protected]>
  10.  
  11. # Dependencies:
  12. #   highlight
  13. #   grep
  14. #   tput
  15. #   sed
  16.  
  17. # Disallow searching for nothing
  18. if [[ -z "$1" ]]; then
  19.   echo "What are you trying to pull?"
  20.   exit 1
  21. fi
  22.  
  23. # Find all files matching search pattern
  24. # We just want the file names here
  25. grep \
  26.   -n \
  27.   -R \
  28.   -l \
  29.   --exclude-dir '.git' \
  30.   --exclude-dir 'node_modules' \
  31.   --exclude-dir 'public' \
  32.   --exclude-dir 'build' \
  33.   --exclude 'package.*' \
  34.   --exclude 'yarn.*' \
  35.   --exclude '*.swp' \
  36.   "$1" \
  37.   . |
  38.  
  39. # Read each matching file
  40. while read match; do
  41.  
  42.   # Get file extension for syntax highlighting
  43.   filename=$(basename "$match")
  44.   extname="${filename##*.}"
  45.  
  46.   # Echo filename in fancy way
  47.   # We want a string of "=" of length equal to the number of columns minus our filename minus a space on each side
  48.   columns=$(tput cols)
  49.   width=$(( (columns - ${#match} - 2) / 2))
  50.   space=$(for i in `seq $width`; do echo -n "="; done)
  51.   # Set distinct colour
  52.   tput setaf 2
  53.   # Echo our spacer, the file, then our spacer again
  54.   echo "$space $match $space"
  55.   # Reset colours
  56.   tput sgr0
  57.  
  58.   # Search file for matches
  59.   grep "$1" "$match" -n --context=3 | highlight --syntax "$extname" --force --out-format ansi | sed "s/$1/$(tput rev)\0$(tput sgr0)/I"
  60. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement