]> git.donarmstrong.com Git - term-progressbar-io.git/commitdiff
initial work on Term::ProgressBar::IO
authorDon Armstrong <don@donarmstrong.com>
Tue, 8 Apr 2014 20:13:32 +0000 (13:13 -0700)
committerDon Armstrong <don@donarmstrong.com>
Tue, 8 Apr 2014 20:13:32 +0000 (13:13 -0700)
.gitignore [new file with mode: 0644]
Build.PL [new file with mode: 0644]
MANIFEST [new file with mode: 0644]
MANIFEST.SKIP [new file with mode: 0644]
lib/Term/ProgressBar/IO.pm [new file with mode: 0644]
t/01_pod.t [new file with mode: 0644]
t/02_basic_test.t [new file with mode: 0644]
t/random_file [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..6358f72
--- /dev/null
@@ -0,0 +1,4 @@
+Build
+_build
+blib
+MANIFEST.SKIP.bak
diff --git a/Build.PL b/Build.PL
new file mode 100644 (file)
index 0000000..3f8255d
--- /dev/null
+++ b/Build.PL
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+
+use Module::Build;
+my $build = Module::Build->new
+    (
+     module_name => 'Term::ProgressBar::IO',
+     license  => 'perl',
+     requires => {
+                  'perl'          => '5.14.1',
+                  'Term::ProgressBar'  => '2.0',
+                 },
+    );
+$build->create_build_script;
diff --git a/MANIFEST b/MANIFEST
new file mode 100644 (file)
index 0000000..bc684da
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,3 @@
+Build.PL
+lib/Term/ProgressBar/IO.pm
+MANIFEST                       This list of files
diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP
new file mode 100644 (file)
index 0000000..6cf6903
--- /dev/null
@@ -0,0 +1,15 @@
+#!include_default
+# Avoid configuration metadata file
+^MYMETA\.
+
+# Avoid Module::Build generated and utility files.
+\bBuild$
+\bBuild.bat$
+\b_build
+\bBuild.COM$
+\bBUILD.COM$
+\bbuild.com$
+^MANIFEST\.SKIP
+
+# Avoid archives of this distribution
+\bTerm-ProgressBar-IO-[\d\.\_]+
diff --git a/lib/Term/ProgressBar/IO.pm b/lib/Term/ProgressBar/IO.pm
new file mode 100644 (file)
index 0000000..0617baf
--- /dev/null
@@ -0,0 +1,110 @@
+# This module is part of IO, and is released
+# under the terms of the GPL version 3, or any later version at your option. See the
+# file README and COPYING for more information.
+# Copyright 2014 by Don Armstrong <don@donarmstrong.com>.
+
+package Term::ProgressBar::IO;
+
+=head1 NAME
+
+Term::ProgressBar::IO -- Display a progress bar while reading from a seekable filehandle
+
+=head1 SYNOPSIS
+
+  my $pb = Term::ProgressBar::IO->new($fh);
+
+  while (<$fh>) {
+      $pb->update();
+  }
+
+=head1 DESCRIPTION
+
+Displays a progress bar using L<Term::ProgressBar> which corresponds
+to reading from a filehandle.
+
+=head1 BUGS
+
+None known.
+
+=cut
+
+use warnings;
+use strict;
+use vars qw($VERSION $DEBUG);
+
+use parent qw(Term::ProgressBar);
+use Carp;
+use Fcntl qw(:seek);
+
+BEGIN{
+     $VERSION = q(0.1);
+     $DEBUG = 0 unless defined $DEBUG;
+}
+
+sub init {
+    my $self = shift;
+    my $count;
+    if (@_==2) {
+        $count = $_[1];
+    } else {
+        croak
+            sprintf("Term::ProgressBar::IO::new We don't handle this many arguments: %d",
+                    scalar @_)
+            if @_ != 1;
+    }
+    my %config;
+    if ( UNIVERSAL::isa ($_[0], 'HASH') ) {
+        ($count) = @{$_[0]}{qw(count)};
+        %config = %{$_[0]};
+    } else {
+        ($count) = @_;
+    }
+    if (ref($count) and $count->can("seek")) {
+        $self->{__filehandle} = $count;
+        $count = $self->__determine_max();
+    }
+    $config{count} = $count;
+    $self->SUPER::init(\%config);
+}
+
+sub update {
+    my $self = shift;
+    my $count = $self->__determine_count();
+    $self->SUPER::update(scalar @_? @_ : $count);
+}
+
+sub __determine_max {
+    my $self = shift;
+    # is this an IO::Uncompress handle?
+    my $max = 0;
+    if ($self->{__filehandle}->can('getHeaderInfo')) {
+        $self->{__filehandle} = *$self->{__filehandle}{FH};
+    }
+    eval {
+        my $cur_pos = $self->{__filehandle}->tell;
+        $self->{__filehandle}->seek(0,SEEK_END);
+        $max = $self->{__filehandle}->tell;
+        $self->{__filehandle}->seek($cur_pos,SEEK_SET);
+    };
+    return $max;
+}
+
+sub __determine_count {
+    my $self = shift;
+    my $count = 0;
+    eval {
+        $count = $self->{__filehandle}->tell;
+    };
+    return $count;
+}
+
+1;
+
+
+__END__
+
+
+
+
+
+
diff --git a/t/01_pod.t b/t/01_pod.t
new file mode 100644 (file)
index 0000000..9a2e6b2
--- /dev/null
@@ -0,0 +1,7 @@
+# -*- mode: cperl; -*-
+use Test::More;
+eval "use Test::Pod 1.00";
+plan skip_all => "Test::Pod 1.00 required for testing POD" if $@;
+all_pod_files_ok(grep {$_ !~ /[~#]$/} 
+                 all_pod_files((-e 'blib'?'blib':(qw(lib))),
+                              ));
diff --git a/t/02_basic_test.t b/t/02_basic_test.t
new file mode 100644 (file)
index 0000000..e619edd
--- /dev/null
@@ -0,0 +1,24 @@
+# -*- mode: cperl; -*-
+use Test::More;
+
+use_ok('Term::ProgressBar::IO');
+
+use IO::File;
+
+my $fh = IO::File->new('t/random_file','r') or
+    die "Unable to open t/random_file for reading: $!";
+
+my $pb = Term::ProgressBar::IO->new($fh);
+
+ok($pb->target() == 9*2+3,'Correct number of bytes in __DATA__');
+
+while (<$fh>) {
+    $pb->update();
+}
+
+print STDERR $pb->last_update();
+ok($pb->last_update() == $pb->target(),'Last position is now target');
+
+close($fh);
+
+done_testing();
diff --git a/t/random_file b/t/random_file
new file mode 100644 (file)
index 0000000..f00c965
--- /dev/null
@@ -0,0 +1,10 @@
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10