From 450cf576219a95dcd8e90cb4e5cd37ca28b4efa2 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 5 Aug 2016 12:08:44 +0100 Subject: factor out table building code --- dobble.pl | 59 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/dobble.pl b/dobble.pl index 84db265..76583cb 100755 --- a/dobble.pl +++ b/dobble.pl @@ -96,8 +96,8 @@ package CardsTable { } } - sub html_table($self,$lines) { - my @ret = (''); + sub _build_table($self,$lines,$row_coderef) { + my @ret = (); # this thing is isomorphic to its dual! we don't need to pivot # lines/points, it's all the same @@ -110,39 +110,38 @@ package CardsTable { my $column = $symbol->[1]; $row[$column] = $symbol->[0]; } - push @ret,' '; - for my $s (@row) { - next unless $s or $self->padding; - push @ret, ' '; - } - push @ret, ' '; + push @ret, $row_coderef->( + $self->padding + ? @row + : grep { $_ } @row + ); } - push @ret,'
',$s||' ','
'; return \@ret; } - sub text_table($self,$lines) { - my @ret = (); - - # this thing is isomorphic to its dual! we don't need to pivot - # lines/points, it's all the same - - for my $line (sort keys $lines->%*) { - my @row; - for my $point ($lines->{$line}->@*) { + sub html_table($self,$lines) { + my $ret = $self->_build_table( + $lines, + sub { + return ( + ' ', + ( map {; ' ', ($_ || ' '), ' ' } @_ ), + ' ', + ); + }, + ); + unshift $ret->@*,''; + push $ret->@*,'
'; + return $ret; + } - my $symbol = $self->symbol_for($point); - my $column = $symbol->[1]; - $row[$column] = $symbol->[0]; - } - my $string = '';; - for my $s (@row) { - next unless $s or $self->padding; - $string .= $s||' '; - } - push @ret, $string; - } - return \@ret; + sub text_table($self,$lines) { + return $self->_build_table( + $lines, + sub { + join '',map { $_ || ' ' } @_; + }, + ); } }; -- cgit v1.2.3