Subject: | Adding horizontal rule (hr) capability |
Attached it a patch (including tests) that adds a generic horizontal
rule (hr) capability. It basically formalizes the line between the title
row and the first row of data, and allows you to insert them anywhere. E.g.
my $t = Text::SimpleTable->new(5);
$t->row('Everything works!');
$t->hr;
$t->row('Everything works!');
print $->draw;
gives:
.-------.
| Ever- |
| ythi- |
| ng w- |
| orks! |
+-------+
| Ever- |
| ythi- |
| ng w- |
| orks! |
'-------'
Subject: | simpletable.patch |
=== Changes
==================================================================
--- Changes (revision 13598)
+++ Changes (local)
@@ -1,5 +1,8 @@
Tis file documents the revision history for Perl extension Text::SimpleTable.
+0.04 2006-10-19
+ - Added horizontal rule (hr) method
+
0.03 2006-01-17 22:00:00
- Fixed bug where text items of '0' were not displayed.
=== lib/Text/SimpleTable.pm
==================================================================
--- lib/Text/SimpleTable.pm (revision 13598)
+++ lib/Text/SimpleTable.pm (local)
@@ -2,7 +2,7 @@
use strict;
-our $VERSION = '0.03';
+our $VERSION = '0.04';
our $TOP_LEFT = '.-';
our $TOP_BORDER = '-';
@@ -96,6 +96,17 @@
return $self;
}
+=item $table->hr
+
+=cut
+
+sub hr {
+ my ( $self ) = @_;
+ for( 0..@{ $self->{columns} } - 1 ) {
+ push @{ $self->{columns}->[ $_ ]->[ 1 ] }, undef;
+ }
+}
+
=item $table->row( @texts )
=cut
@@ -127,6 +138,28 @@
return $self;
}
+sub _draw_hr {
+ my $self = shift;
+ my $columns = @{ $self->{columns} } - 1;
+ my $output = '';
+
+ for my $j ( 0 .. $columns ) {
+ my $column = $self->{columns}->[$j];
+ my $width = $column->[0];
+ my $text = $MIDDLE_BORDER x $width;
+ if ( ( $j == 0 ) && ( $columns == 0 ) ) {
+ $text = "$MIDDLE_LEFT$text$MIDDLE_RIGHT";
+ }
+ elsif ( $j == 0 ) { $text = "$MIDDLE_LEFT$text$MIDDLE_SEPARATOR" }
+ elsif ( $j == $columns ) { $text = "$text$MIDDLE_RIGHT" }
+ else { $text = "$text$MIDDLE_SEPARATOR" }
+ $output .= $text;
+ }
+ $output .= "\n";
+
+ return $output;
+}
+
=item $table->draw
=cut
@@ -179,25 +212,18 @@
}
# Title separator
- for my $j ( 0 .. $columns ) {
- my $column = $self->{columns}->[$j];
- my $width = $column->[0];
- my $text = $MIDDLE_BORDER x $width;
- if ( ( $j == 0 ) && ( $columns == 0 ) ) {
- $text = "$MIDDLE_LEFT$text$MIDDLE_RIGHT";
- }
- elsif ( $j == 0 ) { $text = "$MIDDLE_LEFT$text$MIDDLE_SEPARATOR" }
- elsif ( $j == $columns ) { $text = "$text$MIDDLE_RIGHT" }
- else { $text = "$text$MIDDLE_SEPARATOR" }
- $output .= $text;
- }
- $output .= "\n";
-
+ $output .= $self->_draw_hr;
}
# Rows
for my $i ( 0 .. $rows ) {
+ # Check for horizontal rule
+ if( !grep { defined $self->{columns}->[$_]->[1]->[$i] } 0..$columns ) {
+ $output .= $self->_draw_hr;
+ next;
+ }
+
for my $j ( 0 .. $columns ) {
my $column = $self->{columns}->[$j];
my $width = $column->[0];
=== t/04tables.t
==================================================================
--- t/04tables.t (revision 13598)
+++ t/04tables.t (local)
@@ -1,4 +1,4 @@
-use Test::More tests => 4;
+use Test::More tests => 5;
use_ok('Text::SimpleTable');
@@ -44,3 +44,21 @@
| orks! |
'-------'
EOF
+
+my $t4 = Text::SimpleTable->new(5);
+$t4->row('Everything works!');
+$t4->hr;
+$t4->row('Everything works!');
+is( $t4->draw, <<"EOF");
+.-------.
+| Ever- |
+| ythi- |
+| ng w- |
+| orks! |
++-------+
+| Ever- |
+| ythi- |
+| ng w- |
+| orks! |
+'-------'
+EOF