Skip to content

Instantly share code, notes, and snippets.

@woodwardjd
Created December 13, 2010 20:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodwardjd/739571 to your computer and use it in GitHub Desktop.
Save woodwardjd/739571 to your computer and use it in GitHub Desktop.
#!/bin/env ruby
# == Synopsis
# Executes two gonzui searches, shows you results where both terms appear within a few lines of one another
few_lines = 5
searchTerm1 = ARGV[0]
searchTerm2 = ARGV[1]
puts searchTerm1
puts searchTerm2
Result = Struct.new(:which, :filename, :line, :code)
class Result
include Comparable
def <=>(other)
((self.filename <=> other.filename) != 0) ? (self.filename <=> other.filename) : (self.line.to_i <=> other.line.to_i)
end
end
r1 = `gonzui-search --gonzuirc=/usr/local/etc/gonzuirc -n #{searchTerm1}`.to_a.map! { |s| Result.new(*('a:'+s).split(":", 4)) }.sort! { |a,b| ((a.filename <=> b.filename) != 0) ? (a.filename <=> b.filename) : (a.line.to_i <=> b.line.to_i) }
r2 = `gonzui-search --gonzuirc=/usr/local/etc/gonzuirc -n #{searchTerm2}`.to_a.map! { |s| Result.new(*('b:'+s).split(":", 4)) }.sort! { |a,b| ((a.filename <=> b.filename) != 0) ? (a.filename <=> b.filename) : (a.line.to_i <=> b.line.to_i) }
## pop it off
## print it
## record it as printed
printed = Hash.new
Position = Struct.new(:filename, :line)
while r1.length > 0 && r2.length > 0 do
if r1.first.filename == r2.first.filename
r1pos = Position.new(r1.first.filename, r1.first.line)
r2pos = Position.new(r2.first.filename, r2.first.line)
if r1.first < r2.first
if r2.first.line.to_i - r1.first.line.to_i <= few_lines then
if not printed.member? r1pos then
puts r1.first
printed[r1pos] = true
end
if r1.first != r2.first then
puts r2.first
printed[r2pos] = true
end
end
r1.shift
elsif r2.first < r1.first
if r1.first.line.to_i - r2.first.line.to_i <= few_lines then
if not printed.member? r2pos then
puts r2.first
printed[r2pos] = true
end
puts r1.first
printed[r1pos] = true
end
r2.shift
else
## same line in both? print it!
puts r1.first
printed[r1pos] = true
r1.shift
end
else
## not same file? not a NEAR match then
if r1.first.filename < r2.first.filename then
r1.shift
else
r2.shift
end
end
end
@woodwardjd
Copy link
Author

yeah, this is my first ruby program. don't laugh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment