| 1 | package Table::Simple::Output; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use Carp qw(carp); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Table::Simple::Output - generates ASCII output for an Table::Simple object | ||||||||||
| 12 | |||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||
| 14 | |||||||||||
| 15 | This class generates the ASCII table output for a L<Table::Simple> object. | ||||||||||
| 16 | The output functionality was split to make subclassing easier. | ||||||||||
| 17 | |||||||||||
| 18 | =head2 ATTRIBUTES | ||||||||||
| 19 | |||||||||||
| 20 | =over 4 | ||||||||||
| 21 | |||||||||||
| 22 | =item column_marker | ||||||||||
| 23 | |||||||||||
| 24 | This attribute is the character to mark the beginning or end of a column. The | ||||||||||
| 25 | default is ":" | ||||||||||
| 26 | |||||||||||
| 27 | =back | ||||||||||
| 28 | |||||||||||
| 29 | =cut | ||||||||||
| 30 | |||||||||||
| 31 | 3 | has 'column_marker' => ( | |||||||||
| 32 | is => 'ro', | ||||||||||
| 33 | isa => 'Str', | ||||||||||
| 34 | default => ":", | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | =over 4 | ||||||||||
| 38 | |||||||||||
| 39 | =item horizontal_rule | ||||||||||
| 40 | |||||||||||
| 41 | This attribute is the character to mark horizontal lines in the table. The | ||||||||||
| 42 | default is "=" | ||||||||||
| 43 | |||||||||||
| 44 | =back | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | 3 | has 'horizontal_rule' => ( | |||||||||
| 49 | is => 'ro', | ||||||||||
| 50 | isa => 'Str', | ||||||||||
| 51 | default => '=', | ||||||||||
| 52 | ); | ||||||||||
| 53 | |||||||||||
| 54 | =over 4 | ||||||||||
| 55 | |||||||||||
| 56 | =item intersection_marker | ||||||||||
| 57 | |||||||||||
| 58 | This attribute is the character marking the intersection between the column | ||||||||||
| 59 | marker and a horizontal rule. The default is "+" | ||||||||||
| 60 | |||||||||||
| 61 | =back | ||||||||||
| 62 | |||||||||||
| 63 | =cut | ||||||||||
| 64 | |||||||||||
| 65 | 3 | has 'intersection_marker' => ( | |||||||||
| 66 | is => 'ro', | ||||||||||
| 67 | isa => 'Str', | ||||||||||
| 68 | default => "+", | ||||||||||
| 69 | ); | ||||||||||
| 70 | |||||||||||
| 71 | =over 4 | ||||||||||
| 72 | |||||||||||
| 73 | =item padding | ||||||||||
| 74 | |||||||||||
| 75 | This attribute defines the amount of spaces to put around data rows. The | ||||||||||
| 76 | default is 1 space. | ||||||||||
| 77 | |||||||||||
| 78 | =back | ||||||||||
| 79 | |||||||||||
| 80 | =cut | ||||||||||
| 81 | |||||||||||
| 82 | 3 | has 'padding' => ( | |||||||||
| 83 | is => 'ro', | ||||||||||
| 84 | isa => 'Int', | ||||||||||
| 85 | default => 1, | ||||||||||
| 86 | ); | ||||||||||
| 87 | |||||||||||
| 88 | =over 4 | ||||||||||
| 89 | |||||||||||
| 90 | =item table | ||||||||||
| 91 | |||||||||||
| 92 | This attribute is required at construction time. It is the table to be | ||||||||||
| 93 | output. It must be an object of type L<Table::Simple>. | ||||||||||
| 94 | |||||||||||
| 95 | =back | ||||||||||
| 96 | |||||||||||
| 97 | =cut | ||||||||||
| 98 | |||||||||||
| 99 | 3 | has 'table' => ( | |||||||||
| 100 | is => 'ro', | ||||||||||
| 101 | isa => 'Table::Simple', | ||||||||||
| 102 | required => 1, | ||||||||||
| 103 | ); | ||||||||||
| 104 | |||||||||||
| 105 | has '_widths' => ( | ||||||||||
| 106 | is => 'ro', | ||||||||||
| 107 | isa => 'HashRef[Int]', | ||||||||||
| 108 | traits => [ 'Hash' ], | ||||||||||
| 109 | 2 | ▶ | default => sub { {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 110 | 3 | handles => { | |||||||||
| 111 | '_set_width' => 'set', | ||||||||||
| 112 | '_get_width' => 'get', | ||||||||||
| 113 | }, | ||||||||||
| 114 | lazy => 1, | ||||||||||
| 115 | ); | ||||||||||
| 116 | |||||||||||
| 117 | 3 | has '_hrule' => ( | |||||||||
| 118 | is => 'rw', | ||||||||||
| 119 | isa => 'Str', | ||||||||||
| 120 | lazy_build => 1, | ||||||||||
| 121 | ); | ||||||||||
| 122 | |||||||||||
| 123 | 3 | has '_blank_row' => ( | |||||||||
| 124 | is => 'rw', | ||||||||||
| 125 | isa => 'Str', | ||||||||||
| 126 | lazy_build => 1, | ||||||||||
| 127 | ); | ||||||||||
| 128 | |||||||||||
| 129 | 2 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 130 | 2 | my $self = shift; | |||||||||
| 131 | |||||||||||
| 132 | 2 | ▶ | $self->_compute_widths if defined $self->table; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | |||||||||||
| 136 | 2 | ▶ | sub _compute_widths { | ||||||||
|
Subroutine
_compute_widths: called CC=2 SCAR=6.9
| |||||||||||
| 137 | 2 | my $self = shift; | |||||||||
| 138 | 2 | my $table = $self->table; | |||||||||
| 139 | |||||||||||
| 140 | 2 | my $total = $table->columns->Length - 1; | |||||||||
| 141 | |||||||||||
| 142 | 2 | foreach my $column ( $table->get_columns ) { | |||||||||
| 143 | 4 | $self->_set_width( $column->name => | |||||||||
| 144 | ( $column->width + ( 2 * $self->padding ) ) ); | ||||||||||
| 145 | 4 | $total += $column->width + ( 2 * $self->padding ); | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | 2 | $self->_set_width( '_total' => $total ); | |||||||||
| 149 | |||||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 2 | ▶ | sub _build__hrule { | ||||||||
|
Subroutine
_build__hrule: called CC=2 SCAR=6.9
| |||||||||||
| 153 | 2 | my $self = shift; | |||||||||
| 154 | 2 | my $table = $self->table; | |||||||||
| 155 | |||||||||||
| 156 | 2 | my $hrule = $self->intersection_marker; | |||||||||
| 157 | 2 | foreach my $column ( $table->get_columns ) { | |||||||||
| 158 | 4 | $hrule .= $self->horizontal_rule x $self->_get_width($column->name()); | |||||||||
| 159 | 4 | $hrule .= $self->intersection_marker; | |||||||||
| 160 | } | ||||||||||
| 161 | 2 | $hrule .= "\n"; | |||||||||
| 162 | |||||||||||
| 163 | 2 | return $hrule; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 0 | ▶ | sub _build__blank_row { | ||||||||
|
Subroutine
_build__blank_row: not called CC=2 SCAR=17.9
| |||||||||||
| 167 | 0 | my $self = shift; | |||||||||
| 168 | 0 | my $table = $self->table; | |||||||||
| 169 | |||||||||||
| 170 | 0 | my $blank_row = $self->column_marker; | |||||||||
| 171 | 0 | foreach my $column ( $table->get_columns ) { | |||||||||
| 172 | 0 | $blank_row .= " " x $self->_get_width($column->name()); | |||||||||
| 173 | 0 | $blank_row .= $self->column_marker; | |||||||||
| 174 | } | ||||||||||
| 175 | 0 | $blank_row .= "\n"; | |||||||||
| 176 | |||||||||||
| 177 | 0 | return $blank_row; | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | =head2 METHODS | ||||||||||
| 181 | |||||||||||
| 182 | =over 4 | ||||||||||
| 183 | |||||||||||
| 184 | =item center($text, $width) | ||||||||||
| 185 | |||||||||||
| 186 | This method takes a text string and a width and centers the text in the | ||||||||||
| 187 | width given. It returns the text string padded with spaces to put the | ||||||||||
| 188 | text in the middle of the width. | ||||||||||
| 189 | |||||||||||
| 190 | =back | ||||||||||
| 191 | |||||||||||
| 192 | =cut | ||||||||||
| 193 | |||||||||||
| 194 | 6 | ▶ | sub center { | ||||||||
|
Subroutine
center: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 195 | 6 | my $self = shift; | |||||||||
| 196 | 6 | my $text = shift; | |||||||||
| 197 | 6 | my $width = shift; | |||||||||
| 198 | |||||||||||
| 199 | 6 | my $padding = int ( ( $width - length($text) ) / 2 ); | |||||||||
| 200 | 6 | my $left = " " x $padding . $text; | |||||||||
| 201 | 6 | my $right = " " x ($width - length($left)); | |||||||||
| 202 | 6 | return $left . $right; | |||||||||
| 203 | |||||||||||
| 204 | } | ||||||||||
| 205 | |||||||||||
| 206 | =over 4 | ||||||||||
| 207 | |||||||||||
| 208 | =item left_justify($text, $width) | ||||||||||
| 209 | |||||||||||
| 210 | This puts text against the left margin of a table cell, padded with | ||||||||||
| 211 | spaces until it is the specified width. | ||||||||||
| 212 | |||||||||||
| 213 | =back | ||||||||||
| 214 | |||||||||||
| 215 | =cut | ||||||||||
| 216 | |||||||||||
| 217 | 4 | ▶ | sub left_justify { | ||||||||
|
Subroutine
left_justify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 218 | 4 | my $self = shift; | |||||||||
| 219 | 4 | my $text = shift; | |||||||||
| 220 | 4 | my $width = shift; | |||||||||
| 221 | |||||||||||
| 222 | 4 | return " " . $text . " " x ($width - ( length($text) + 1 )); | |||||||||
| 223 | } | ||||||||||
| 224 | |||||||||||
| 225 | =over 4 | ||||||||||
| 226 | |||||||||||
| 227 | =item right_justify($text, $width) | ||||||||||
| 228 | |||||||||||
| 229 | This method puts text against the right margin of a table cell, padded | ||||||||||
| 230 | with spaces until it is the specified width. | ||||||||||
| 231 | |||||||||||
| 232 | =back | ||||||||||
| 233 | |||||||||||
| 234 | =cut | ||||||||||
| 235 | |||||||||||
| 236 | 4 | ▶ | sub right_justify { | ||||||||
|
Subroutine
right_justify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 237 | 4 | my $self = shift; | |||||||||
| 238 | 4 | my $text = shift; | |||||||||
| 239 | 4 | my $width = shift; | |||||||||
| 240 | |||||||||||
| 241 | 4 | return " " x ( $width - ( length($text) + 1 )) . $text . " "; | |||||||||
| 242 | } | ||||||||||
| 243 | |||||||||||
| 244 | =over 4 | ||||||||||
| 245 | |||||||||||
| 246 | =item build_row_output( @array ) | ||||||||||
| 247 | |||||||||||
| 248 | This method returns a scalar string composed of the rows specified in | ||||||||||
| 249 | the input @array. | ||||||||||
| 250 | |||||||||||
| 251 | =back | ||||||||||
| 252 | |||||||||||
| 253 | =cut | ||||||||||
| 254 | |||||||||||
| 255 | 2 | ▶ | sub build_row_output { | ||||||||
|
Subroutine
build_row_output: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||
| 256 | 2 | my $self = shift; | |||||||||
| 257 | 2 | my $table = $self->table; | |||||||||
| 258 | |||||||||||
| 259 | 2 | my $rv; | |||||||||
| 260 | 2 | foreach my $row ( @_ ) { | |||||||||
| 261 | 4 | ▶ | if ( $row > $table->row_count ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 262 | 0 | carp "$row does not exist in the given table."; | |||||||||
| 263 | 0 | next; | |||||||||
| 264 | } | ||||||||||
| 265 | 4 | $rv .= $self->column_marker; | |||||||||
| 266 | 4 | foreach my $column ( $table->get_columns ) { | |||||||||
| 267 | 8 | my $format = $column->output_format; | |||||||||
| 268 | 8 | $rv .= $self->$format( | |||||||||
| 269 | $column->get_row($row), | ||||||||||
| 270 | $self->_get_width( $column->name ) ); | ||||||||||
| 271 | 8 | $rv .= $self->column_marker; | |||||||||
| 272 | } | ||||||||||
| 273 | 4 | $rv .= "\n"; | |||||||||
| 274 | 4 | $rv .= $self->_hrule; | |||||||||
| 275 | } | ||||||||||
| 276 | |||||||||||
| 277 | 2 | return $rv; | |||||||||
| 278 | } | ||||||||||
| 279 | |||||||||||
| 280 | =over 4 | ||||||||||
| 281 | |||||||||||
| 282 | =item build_column_name_output | ||||||||||
| 283 | |||||||||||
| 284 | This method returns a string scalar composed of column names centered in | ||||||||||
| 285 | the width for each column. | ||||||||||
| 286 | |||||||||||
| 287 | =back | ||||||||||
| 288 | |||||||||||
| 289 | =cut | ||||||||||
| 290 | |||||||||||
| 291 | 2 | ▶ | sub build_column_name_output { | ||||||||
|
Subroutine
build_column_name_output: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 292 | 2 | my $self = shift; | |||||||||
| 293 | |||||||||||
| 294 | 2 | my $table = $self->table; | |||||||||
| 295 | |||||||||||
| 296 | 2 | my $rv .= $self->_hrule; | |||||||||
| 297 | 2 | $rv .= $self->column_marker; | |||||||||
| 298 | 2 | foreach my $column ( $table->get_columns ) { | |||||||||
| 299 | 4 | $rv .= $self->center( $column->name, | |||||||||
| 300 | $self->_get_width( $column->name ) ); | ||||||||||
| 301 | 4 | $rv .= $self->column_marker; | |||||||||
| 302 | } | ||||||||||
| 303 | 2 | $rv .= "\n"; | |||||||||
| 304 | 2 | $rv .= $self->_hrule; | |||||||||
| 305 | |||||||||||
| 306 | 2 | return $rv; | |||||||||
| 307 | } | ||||||||||
| 308 | |||||||||||
| 309 | =over 4 | ||||||||||
| 310 | |||||||||||
| 311 | =item build_table_name_output | ||||||||||
| 312 | |||||||||||
| 313 | This outputs the L<Table::Simple> name attribute centered in its own row. It | ||||||||||
| 314 | returns a string scalar. | ||||||||||
| 315 | |||||||||||
| 316 | =back | ||||||||||
| 317 | |||||||||||
| 318 | =cut | ||||||||||
| 319 | |||||||||||
| 320 | 2 | ▶ | sub build_table_name_output { | ||||||||
|
Subroutine
build_table_name_output: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 321 | 2 | my $self = shift; | |||||||||
| 322 | |||||||||||
| 323 | 2 | my $table_width = $self->_get_width('_total'); | |||||||||
| 324 | |||||||||||
| 325 | 2 | my $rv; | |||||||||
| 326 | |||||||||||
| 327 | 2 | $rv .= $self->_hrule; | |||||||||
| 328 | 2 | $rv .= $self->column_marker; | |||||||||
| 329 | 2 | $rv .= $self->center($self->table->name, $table_width); | |||||||||
| 330 | 2 | $rv .= $self->column_marker; | |||||||||
| 331 | 2 | $rv .= "\n"; | |||||||||
| 332 | |||||||||||
| 333 | 2 | return $rv; | |||||||||
| 334 | |||||||||||
| 335 | } | ||||||||||
| 336 | |||||||||||
| 337 | =over 4 | ||||||||||
| 338 | |||||||||||
| 339 | =item build_table_output | ||||||||||
| 340 | |||||||||||
| 341 | This method returns a string scalar composed of the table name row (if | ||||||||||
| 342 | defined), column names, and all row values. | ||||||||||
| 343 | |||||||||||
| 344 | =back | ||||||||||
| 345 | |||||||||||
| 346 | =cut | ||||||||||
| 347 | |||||||||||
| 348 | 2 | ▶ | sub build_table_output { | ||||||||
|
Subroutine
build_table_output: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 349 | 2 | my $self = shift; | |||||||||
| 350 | |||||||||||
| 351 | 2 | my $rv; | |||||||||
| 352 | |||||||||||
| 353 | 2 | ▶ | $rv .= $self->build_table_name_output if defined $self->table->name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 354 | 2 | $rv .= $self->build_column_name_output; | |||||||||
| 355 | 2 | $rv .= $self->build_row_output( 0..( $self->table->row_count - 1 ) ); | |||||||||
| 356 | |||||||||||
| 357 | 2 | return $rv; | |||||||||
| 358 | } | ||||||||||
| 359 | |||||||||||
| 360 | =over 4 | ||||||||||
| 361 | |||||||||||
| 362 | =item print_table | ||||||||||
| 363 | |||||||||||
| 364 | This method outputs the string scalar from the build_table_output() method | ||||||||||
| 365 | to standard out. | ||||||||||
| 366 | |||||||||||
| 367 | =back | ||||||||||
| 368 | |||||||||||
| 369 | =cut | ||||||||||
| 370 | |||||||||||
| 371 | 2 | ▶ | sub print_table { | ||||||||
|
Subroutine
print_table: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 372 | 2 | my $self = shift; | |||||||||
| 373 | |||||||||||
| 374 | 2 | print $self->build_table_output; | |||||||||
| 375 | } | ||||||||||
| 376 | |||||||||||
| 377 | =head1 LICENSE | ||||||||||
| 378 | |||||||||||
| 379 | Copyright (C) 2010 Mark Allen | ||||||||||
| 380 | |||||||||||
| 381 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 382 | under the terms of either: the GNU General Public License as published | ||||||||||
| 383 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 384 | |||||||||||
| 385 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 386 | |||||||||||
| 387 | =head1 AUTHOR | ||||||||||
| 388 | |||||||||||
| 389 | Mark Allen <mrallen1@yahoo.com> | ||||||||||
| 390 | |||||||||||
| 391 | =head1 SEE ALSO | ||||||||||
| 392 | |||||||||||
| 393 | L<Moose>, L<Table::Simple>, L<Table::Simple::Output::Theory> | ||||||||||
| 394 | |||||||||||
| 395 | =cut | ||||||||||
| 396 | |||||||||||
| 397 | 3 | __PACKAGE__->meta->make_immutable(); | |||||||||
| 398 | 3 | 1; | |||||||||