wrag: clean up & fix opt forwarding

This commit is contained in:
Henrik Lissner 2014-12-12 16:19:47 -05:00
parent d08e0ac93a
commit c97c785dd5

View file

@ -1,6 +1,6 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# A hacky script that allows you to search with ag # Wrag stands for writeable-ag; a hacky script that allows you to search with ag
# <https://github.com/ggreer/the_silver_searcher> and edit the results in # <https://github.com/ggreer/the_silver_searcher> and edit the results in
# $EDITOR. The changes are propagated to all listed files, if any. # $EDITOR. The changes are propagated to all listed files, if any.
# #
@ -21,6 +21,7 @@ end
usage if ARGV.empty? usage if ARGV.empty?
## Parsing options ################################
options = {:rename => false, options = {:rename => false,
:backup => false} :backup => false}
@ -28,13 +29,22 @@ opt = OptionParser.new
opt.on('-f', '--rename-files') { options[:rename] = true } opt.on('-f', '--rename-files') { options[:rename] = true }
opt.on('-b', '--backup') { options[:backup] = true } opt.on('-b', '--backup') { options[:backup] = true }
t = Tempfile.new('.ag-results') begin
out = system("ag #{ARGV.join(' ')} --group --nocolor > #{t.path}") opt.parse
if [false, nil].include? out # Get rid of misc args to pass the rest to ag
puts "No matches" if out === false args = ARGV - ["-f", "--rename-files", "-b", "--backup"]
rescue OptionParser::InvalidOption => e
puts "Invalid option: #{e}"
usage
end
## Perform search #################################
t = Tempfile.new('.ag-results')
out = system("ag #{args.join(' ')} --group --nocolor > #{t.path}")
puts "No matches" if out === false
if out == nil
t.unlink t.unlink
exit 1 exit 2
end end
old_lines = File.open(t.path).readlines old_lines = File.open(t.path).readlines
@ -44,9 +54,10 @@ t.unlink
if old_lines.length != lines.length if old_lines.length != lines.length
puts "There was a line-number mismatch. Abort! Abort! Abort!" puts "There was a line-number mismatch. Abort! Abort! Abort!"
exit 2 exit 3
end end
## Parse edited output ############################
i = nil i = nil
current_filename = nil current_filename = nil
lines.each_with_index do |line, i| lines.each_with_index do |line, i|
@ -76,7 +87,7 @@ lines.each_with_index do |line, i|
next next
else else
puts "Malformed line: #{line}" puts "Malformed line: #{line}"
exit 4 exit 3
end end
next if line.eql?(old_lines[i].strip) next if line.eql?(old_lines[i].strip)