File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 40
40
* .orig
41
41
* .rej
42
42
* ~
43
+ # Ignore backups of retabbed files
44
+ * .notab
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: binary -*-
3
+
4
+ # Replace leading tabs with 2-width spaces.
5
+ # I'm sure there's a sed/awk/perl oneliner that's
6
+ # a million times better but this is more readable for me.
7
+
8
+ require 'fileutils'
9
+ require 'find'
10
+
11
+ dir = ARGV [ 0 ] || "."
12
+ raise ArgumentError , "Need a filename or directory" unless ( dir and File . readable? dir )
13
+
14
+ Find . find ( dir ) do |infile |
15
+ next unless File . file? infile
16
+ next unless infile =~ /rb$/
17
+ outfile = infile
18
+ backup = "#{ infile } .notab"
19
+ FileUtils . cp infile , backup
20
+
21
+ data = File . open ( infile , "rb" ) { |f | f . read f . stat . size }
22
+ fixed = [ ]
23
+ data . each_line do |line |
24
+ fixed << line
25
+ next unless line =~ /^\x09 /
26
+ index = [ ]
27
+ i = 0
28
+ line . each_char do |char |
29
+ break unless char =~ /[\x20 \x09 ]/
30
+ index << i if char == "\x09 "
31
+ i += 1
32
+ end
33
+ index . reverse . each do |idx |
34
+ line [ idx ] = " "
35
+ end
36
+ fixed [ -1 ] = line
37
+ end
38
+
39
+ fh = File . open ( outfile , "wb" )
40
+ fh . write fixed . join
41
+ fh . close
42
+ puts "Retabbed #{ fh . path } "
43
+ end
You can’t perform that action at this time.
0 commit comments