Skip to content

Instantly share code, notes, and snippets.

@pjlsergeant
Created September 14, 2010 09:12
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 pjlsergeant/578775 to your computer and use it in GitHub Desktop.
Save pjlsergeant/578775 to your computer and use it in GitHub Desktop.
#!/opt/xt/xt-perl/bin/perl
use strict;
use warnings;
use File::Spec;
use Git::Wrapper;
use Cwd 'realpath';
use Term::ANSIColor;
use Term::ReadKey;
File::Spec->curdir();
my $rel_git_dir = `git rev-parse --show-cdup`;
chomp($rel_git_dir);
my $git_base = realpath(File::Spec->catdir( File::Spec->curdir(), $rel_git_dir ));
my $rel_path = File::Spec->abs2rel( File::Spec->curdir(), $git_base );
my $git = Git::Wrapper->new( $git_base );
for my $file ( sort `ls -1` ) {
chomp($file);
my $data = {};
my ( $log ) = $git->log( "$rel_path/$file" );
next unless $log;
$data->{'filename'} = pad_or_trim( 20, $file );
my $author = $log->author;
my $width = (GetTerminalSize())[0];
$author =~ s/<.+//;
$data->{'author'} = pad_or_trim( 15, $author );
$data->{'date'} = substr( $log->date, 4, 6 );
$data->{'message'} = pad_or_trim( ($width - 55), $log->message );
$data->{'id'} = substr( $log->id, 0, 8 );
$data->{'message'} =~ s/\s/ /g;
if ( -d $file ) {
# If the filename is already too long, shorten it by one so we can add
# dir marker
$data->{'filename'} =~ s/.~$/~/;
$data->{'filename'} =~ s!([^ ]+)[^~]?!$1/!;
}
for (
[ filename => 'blue' ],
[ author => 'green' ],
[ date => 'cyan' ],
[ id => 'yellow' ],
[ message => 'white' ],
) {
my ( $attr, $color ) = @$_;
print color($color) . $data->{$attr} . color('reset') . ' ';
}
print "\n";
}
sub pad_or_trim {
my ( $target_length, $string ) = @_;
my $actual_length = length($string);
if ( $actual_length < $target_length ) {
my $makeup = $target_length - $actual_length;
return $string . (' ' x $makeup);
} elsif ( $actual_length > $target_length ) {
$string = substr( $string, 0, ($target_length-1) );
$string = $string . '~';
return $string;
} else {
return $string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment