Skip Menu |

This queue is for tickets about the Text-Table CPAN distribution.

Report information
The Basics
Id: 2053
Status: resolved
Priority: 0/
Queue: Text-Table

People
Owner: ANNO [...] cpan.org
Requestors:
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 0.03
Fixed in: (no value)



Subject: wanted/missing methods: column_width, align and column_align
Recently at perlmonks ( http://perlmonks.com/index.pl?node_id=233466 ) I came up with the following snippet #!/usr/bin/perl use strict; use warnings; use Text::Table; print "==== Monks I think rock! ====\n\n"; my $tB = Text::Table->new( \'[ ', { title => 'Name', align => 'left', }, \" | ", #separator { title => ' ', align => 'right', }, \' ]', ); $tB->load( [ steves => 'rocks plenty, found Text::Table'], [ 'Anno Siegel' => 'rocks plenty, after all he wrote Text::Table'], ); print $tB->title(); print $tB->rule('#','#'); print $tB->body(0); # bugger, a buG # print $tB->title_rule('.'); print $tB->rule(); print $tB->body(1); print $tB->body_rule('.'); print $tB->rule(); # this one ought to be called "align" or body_align # and part of the package print Text::Aligner::align( right => ( ' ' x ( $tB->width() - length '... and many more ' ) ) . '... and many more ' ),"\n"; # and there ought to be one called column_align # which would work the same, except would align the thing within # the specified column, as in: # print $tB->column_align(1,'left',' ... and many more '); __END__ ==== Monks I think rock! ==== [ Name | ] ############################################################## [ steves | rocks plenty, found Text::Table ] [ | ] [ Anno Siegel | rocks plenty, after all he wrote Text::Table ] [.............|..............................................] [ | ] ... and many more Now I hated having to mess with Text::Aligner, which I why the methods align and column_align are missing. I couldn't align a piece of text in a particular column, which is why column_width is missing (i didn't wanna involve Data::Dumper in finding that info out, that's what APIs are for). Now if column_align exists, column_width might not be really neccessary, but it would still be nice. I hope you can see that these methods ought to be included. P.S. I realized after posting that you're probably not a perl monk, oh well.
Date: Mon, 10 Feb 2003 10:30:22 +0100
From: Anno Siegel <anno4000 [...] lublin.zrz.TU-Berlin.DE>
To: bug-Text-Table [...] rt.cpan.org
Subject: Re: [cpan #2053] wanted/missing methods: column_width, align and column_align
RT-Send-Cc:
via RT wrote: Show quoted text
> This message about Text-Table was sent to you by PODMASTER <PODMASTER@cpan.org> via rt.cpan.org > > Full context and any attached attachments can be found at: > <URL: https://rt.cpan.org/Ticket/Display.html?id=2053 > > > Recently at perlmonks ( http://perlmonks.com/index.pl?node_id=233466 ) > I came up with the following snippet > > #!/usr/bin/perl > use strict; > use warnings; > use Text::Table; > > print "==== Monks I think rock! ====\n\n"; > > my $tB = Text::Table->new( > \'[ ', > { > title => 'Name', > align => 'left', > }, > \" | ", #separator > { > title => ' ', > align => 'right', > }, > \' ]', > ); > > $tB->load( > [ steves => 'rocks plenty, found Text::Table'], > [ 'Anno Siegel' => 'rocks plenty, after all he wrote Text::Table'], > ); > > print $tB->title(); > > print $tB->rule('#','#'); > print $tB->body(0); > # bugger, a buG > # print $tB->title_rule('.');
Right. The documentation was wrong: rule() does what title_rule() was described to do. title_rule() doesn't exist, but body_rule() does (as you have noticed). This is fixed in Text::Table v 0.04. Show quoted text
> print $tB->rule(); > print $tB->body(1); > print $tB->body_rule('.'); > print $tB->rule(); > # this one ought to be called "align" or body_align > # and part of the package > print Text::Aligner::align( > right => > ( ' ' x ( $tB->width() - length '... and many more ' ) ) > . '... and many more ' ),"\n"; > # and there ought to be one called column_align > # which would work the same, except would align the thing within > # the specified column, as in: > # print $tB->column_align(1,'left',' ... and many more '); > > __END__ > ==== Monks I think rock! ==== > > [ Name | ] > ############################################################## > [ steves | rocks plenty, found Text::Table ] > [ | ] > [ Anno Siegel | rocks plenty, after all he wrote Text::Table ] > [.............|..............................................] > [ | ] > ... and many more > > Now I hated having to mess with Text::Aligner, > which I why the methods align and column_align are missing.
For what you do above you don't have to. Instead of printing the lines (and aligning the last one manually), collect them in another (auxiliary) table that has only one right-aligned title-less column. Then add the final line. (See below for complete code.) Show quoted text
> I couldn't align a piece of text in a particular column, > which is why column_width is missing > (i didn't wanna involve Data::Dumper in finding that info out, > that's what APIs are for). > > Now if column_align exists, column_width might not be really > neccess#!/usr/bin/perlary, but it would still be nice.
Aligning of text outside the table to particular columns will not be supported. However, the next upgrade (0.05, presumably out next weekend) will allow $table->colrange( $i) which will return the starting position and width of the $i-th column. Anno #!/usr/bin/perl use strict; use warnings; $| = 1; # @~^` use Text::Table; my $tB = Text::Table->new( \'[ ', { title => 'Name', align => 'left', }, \" | ", #separator { title => ' ', align => 'right', }, \' ]', ); $tB->load( [ steves => 'rocks plenty, found Text::Table'], [ 'Anno Siegel' => 'rocks plenty, after all he wrote Text::Table'], ); # add line (right justified wrt. whole table) # create untitled single-column (right-aligned) auxiliary table my $tb_aux = Text::Table->new( { align => 'right'}); # build aux table from lines and rules from original # lines all have equal length, so no justification happens $tb_aux->load( map( { chomp; [ $_]} # must remove newline and pack in [] $tB->title, $tB->rule( '#', '#'), $tB->body( 0), $tB->body_rule, $tB->body( 1), $tB->body_rule('.'), $tB->body_rule, ), ); # add another line (this one is shorter, so will be right-justified) $tb_aux->add( '... and many more '); print $tb_aux;