Skip to content

Instantly share code, notes, and snippets.

@mlabrum
Created July 15, 2010 05:57
Show Gist options
  • Save mlabrum/476558 to your computer and use it in GitHub Desktop.
Save mlabrum/476558 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# Matt Labrum
# quick and dirty ruby script to neaten grep input by grouping by filename + some formatting
# Example Usage: grep -r -n "TODO:" . | neatgrep.rb
# must be called with -n, script doesn't handle other cases
#
# Handy when using a bash alias, for example
# alias todo='grep -r -n "TODO:" . | neatgrep.rb'
#
# then in your code directory, you just type todo, and it shows all your todo comments in your code :)
ignore_directories = Regexp.new([
"./vendor/plugins/delayed_job"
].map{|item| Regexp.escape(item)}.join("|"))
#escape codes to enable and disable bold
bold_start = "\033[1m"
bold_end = "\033[0m"
#bars is decided dynamicly depending on how big the console is
bars = "-" * (%x[tput cols].to_i / 1.7)
file_hash = {}
ARGF.each do |line|
parts = line.split(":", 3)
raise "Error, incorrect input" if parts.length != 3
#hide any ignored directories
next if ignore_directories.match(parts[0]) != nil
file_hash[parts[0]] = [] if !file_hash.key?(parts[0])
file_hash[parts[0]].push((parts[1] + ':').ljust(5) + parts[2].gsub(/^\s*/, ""))
end
file_hash.each do |file_name, matches|
#write header
STDOUT.write("\n" + bars + "\n#{bold_start}#" + file_name + "#{bold_end}\n" + bars + "\n")
matches.each do |match|
STDOUT.write(match)
end
end
STDOUT.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment