Add retab util to this branch
parent
c9bd791ff6
commit
d3a60135b8
|
@ -40,3 +40,5 @@ tags
|
|||
*.orig
|
||||
*.rej
|
||||
*~
|
||||
# Ignore backups of retabbed files
|
||||
*.notab
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
#!/usr/bin/env ruby
|
||||
# -*- coding: binary -*-
|
||||
|
||||
# Replace leading tabs with 2-width spaces.
|
||||
# I'm sure there's a sed/awk/perl oneliner that's
|
||||
# a million times better but this is more readable for me.
|
||||
|
||||
require 'fileutils'
|
||||
require 'find'
|
||||
|
||||
dir = ARGV[0] || "."
|
||||
raise ArgumentError, "Need a filename or directory" unless (dir and File.readable? dir)
|
||||
|
||||
Find.find(dir) do |infile|
|
||||
next unless File.file? infile
|
||||
next unless infile =~ /rb$/
|
||||
outfile = infile
|
||||
backup = "#{infile}.notab"
|
||||
FileUtils.cp infile, backup
|
||||
|
||||
data = File.open(infile, "rb") {|f| f.read f.stat.size}
|
||||
fixed = []
|
||||
data.each_line do |line|
|
||||
fixed << line
|
||||
next unless line =~ /^\x09/
|
||||
index = []
|
||||
i = 0
|
||||
line.each_char do |char|
|
||||
break unless char =~ /[\x20\x09]/
|
||||
index << i if char == "\x09"
|
||||
i += 1
|
||||
end
|
||||
index.reverse.each do |idx|
|
||||
line[idx] = " "
|
||||
end
|
||||
fixed[-1] = line
|
||||
end
|
||||
|
||||
fh = File.open(outfile, "wb")
|
||||
fh.write fixed.join
|
||||
fh.close
|
||||
puts "Retabbed #{fh.path}"
|
||||
end
|
Loading…
Reference in New Issue