Skip to content

Instantly share code, notes, and snippets.

@theory
Last active June 10, 2017 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theory/f608e59adb38e62be792a7acd235b1e5 to your computer and use it in GitHub Desktop.
Save theory/f608e59adb38e62be792a7acd235b1e5 to your computer and use it in GitHub Desktop.
My dumb Go source code line counter.
#!/usr/bin/env perl -w
use warnings;
use utf8;
use strict;
use File::Find qw(find);
push @ARGV, '.' unless @ARGV;
my $go_comments = 0;
my $go_blanks = 0;
my $go_code = 0;
my $test_comments = 0;
my $test_blanks = 0;
my $test_code = 0;
print "\n";
print " File Code Comment Blank Total\n";
print "----------------------------------------- -------- --------- -------- --------\n";
find {
wanted => sub {
return unless /[.]go\z/;
return if -d;
(my $fn = $_) =~ s{\A[.]/}{};
open my $fh, '<:raw', $fn or die "Cannot open $fn: $!\n";
my ($comments, $blanks, $code, $in_comment) = (0, 0, 0, 0);
while (<$fh>) {
if ($in_comment) {
$comments++;
$in_comment = $_ !~ m{[*]/};
} elsif (m{\A\s*//}) {
$comments++;
} elsif (m{/[*]}) {
$comments++;
$in_comment = 1;
} elsif (m{\S}) {
$code++;
} else {
$blanks++;
}
}
printf " %-40s %7d %7d %7d %7d\n", $fn, $code, $comments, $blanks,
$code + $comments + $blanks;
if ($fn =~ /_test[.]go\z/) {
$test_comments += $comments;
$test_blanks += $blanks;
$test_code += $code;
} else {
$go_comments += $comments;
$go_blanks += $blanks;
$go_code += $code;
}
},
follow => 1,
no_chdir => 1,
}, @ARGV;
print "----------------------------------------- -------- --------- -------- --------\n";
printf " %-40s %7d %7d %7d %7d\n", 'Source Total',
$go_code, $go_comments, $go_blanks,
$go_code + $go_comments + $go_blanks;
printf " %-40s %7d %7d %7d %7d\n", 'Test Total',
$test_code, $test_comments, $test_blanks,
$test_code + $test_comments + $test_blanks;
print "----------------------------------------- -------- --------- -------- --------\n";
printf " %-40s %7d %7d %7d %7d\n", 'Grand Total',
$go_code + $test_code, $go_comments + $test_comments, $go_blanks + $test_blanks,
$go_code + $test_code + $go_comments + $test_comments + $go_blanks + $test_blanks;
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment