Browse Source

Add retab util to this branch

Tab Assassin 6 years ago
parent
commit
d3a60135b8
2 changed files with 45 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 43
    0
      tools/dev/retab.rb

+ 2
- 0
.gitignore View File

@@ -40,3 +40,5 @@ tags
40 40
 *.orig
41 41
 *.rej
42 42
 *~
43
+# Ignore backups of retabbed files
44
+*.notab

+ 43
- 0
tools/dev/retab.rb View File

@@ -0,0 +1,43 @@
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

Loading…
Cancel
Save