Skip to content

Instantly share code, notes, and snippets.

@tiennou
Last active September 2, 2015 14:29
Show Gist options
  • Save tiennou/1d2eab37b64a44d33947 to your computer and use it in GitHub Desktop.
Save tiennou/1d2eab37b64a44d33947 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -W
# encoding: utf-8
require 'set'
pattern = ARGV.shift
exit 1 if pattern.nil?
root_pattern = File.join(Dir.getwd, '**', pattern)
stats = {}
Dir.glob(root_pattern) do |file|
stats[file] ||= {
lines: 0,
tabs: 0,
spaces: 0,
}
IO.foreach(file) do |line|
stats[file][:lines] += 1
begin
if /^ /.match(line) then
stats[file][:tabs] += 1
end
if /^ /.match(line) then
stats[file][:spaces] += 1
end
rescue
puts "Failed to match #{file}:#{line}"
end
end
end
tabs_count = stats.collect {|file, stat| stat[:tabs]}.reduce :+
spaces_count = stats.collect {|file, stat| stat[:spaces]}.reduce :+
mismatched_files = stats.select {|file, stat| stat[:tabs] != 0 and stat[:spaces] != 0 }
puts "#{stats.count} files, #{tabs_count} tab lines, #{spaces_count} spaces line"
mismatch_stats = {tabs: 0, spaces: 0}
puts "#{mismatched_files.count} files have mismatching indent :"
mismatched_files.each do |file, stat|
file = file.sub Dir.getwd+'', "."
msg = case
when stat[:tabs] > stat[:spaces] then
mismatch_stats[:tabs] += 1
"has more tabs"
when stat[:tabs] < stat[:spaces] then
mismatch_stats[:spaces] += 1
"has more spaces"
else "has same number"
end
puts "#{file} #{msg}"
end
puts "#{mismatch_stats[:tabs]} mismatching files have more tabs, #{mismatch_stats[:spaces]} have more spaces"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment