Skip to content

Commit

Permalink
renamed table function to tablify and changed it to allow overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 6, 2014
1 parent 786d34e commit 18c7e41
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Changes
Expand Up @@ -6,7 +6,7 @@
of Mojo::DOM::content.
- Deprecated Mojo::DOM::to_xml in favor of Mojo::DOM::to_string.
- Added wrap_content method to Mojo::DOM.
- Added table function to Mojo::Util.
- Added tablify function to Mojo::Util.
- Improved wrap method in Mojo::DOM to allow wrapping of the root node.

4.76 2014-02-04
Expand Down
44 changes: 18 additions & 26 deletions lib/Mojo/Util.pm
Expand Up @@ -8,7 +8,7 @@ use Digest::SHA qw(hmac_sha1_hex sha1 sha1_hex);
use Encode 'find_encoding';
use File::Basename 'dirname';
use File::Spec::Functions 'catfile';
use List::Util qw(max min);
use List::Util 'min';
use MIME::Base64 qw(decode_base64 encode_base64);
use Time::HiRes ();

Expand Down Expand Up @@ -52,7 +52,7 @@ our @EXPORT_OK = (
qw(decode deprecated dumper encode get_line hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_header spurt squish),
qw(steady_time table trim unindent unquote url_escape url_unescape),
qw(steady_time tablify trim unindent unquote url_escape url_unescape),
qw(xml_escape xor_encode)
);

Expand Down Expand Up @@ -296,24 +296,21 @@ sub steady_time () {
: Time::HiRes::time;
}

sub table {
my $columns = shift;
sub tablify {
my $rows = shift;

my $spec = shift @$columns;
for my $i (0 .. $#$spec) {
my @len;
for my $j (0 .. $#$columns) {
$columns->[$j][$i] =~ s/[\r\n]//g;
push @len, length $columns->[$j][$i];
my @spec;
for my $row (@$rows) {
for my $i (0 .. $#$row) {
$row->[$i] =~ s/[\r\n]//g;
my $len = length $row->[$i];
$spec[$i] = $len if $len > ($spec[$i] // 0);
}
next unless $spec->[$i + 1] && (my $max = max @len) < $spec->[$i];
$spec->[$i + 1] += $spec->[$i] - $max;
$spec->[$i] = $max;
}

my $format = join ' ', map {"\%-${_}.${_}s"} @$spec[0 .. $#$spec - 1];
$format .= ($format ? ' %.' : '%.') . $spec->[-1] . "s\n";
return join '', map { sprintf $format, @$_ } @$columns;
my $format = join ' ', map {"\%-${_}s"} @spec[0 .. $#spec - 1];
$format .= $format ? ' %s' : '%s';
return join '', map { sprintf "$format\n", @$_ } @$rows;
}

sub trim {
Expand Down Expand Up @@ -647,19 +644,14 @@ consecutive groups of whitespace into one space each.
High resolution time, resilient to time jumps if a monotonic clock is
available through L<Time::HiRes>.
=head2 table
=head2 tablify
my $table = table [[20, 20], ['foo', 'bar'], ['baz', 'yada']];
my $table = tablify [['foo', 'bar'], ['baz', 'yada']];
Simple row-oriented table builder for command line tools, the first row
contains the maximum width for each column, if one column doesn't require its
full quota, the next one inherits the rest.
Simple row-oriented table builder for command line tools.
# "foo bar\nbaz yad\n"
table [[3, 3], ['foo', 'bar'], ['baz', 'yada']];
# "foo bar\nbaz yada\n"
table [[4, 3], ['foo', 'bar'], ['baz', 'yada']];
# "foo bar\nyada yada\n"
tablify [['foo', 'bar'], ['yada', 'yada']];
=head2 trim
Expand Down
14 changes: 7 additions & 7 deletions lib/Mojolicious/Command/routes.pm
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base 'Mojolicious::Command';

use re 'regexp_pattern';
use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::Util qw(encode table);
use Mojo::Util qw(encode tablify);

has description => 'Show available routes.';
has usage => sub { shift->extract_usage };
Expand All @@ -13,18 +13,18 @@ sub run {

GetOptionsFromArray \@args, 'v|verbose' => \my $verbose;

my $table = $verbose ? [[20, 16, 18, 20]] : [[26, 25, 25]];
$self->_walk($_, 0, $table, $verbose) for @{$self->app->routes->children};
print encode('UTF-8', table($table));
my $rows = [];
$self->_walk($_, 0, $rows, $verbose) for @{$self->app->routes->children};
print encode('UTF-8', tablify($rows));
}

sub _walk {
my ($self, $route, $depth, $table, $verbose) = @_;
my ($self, $route, $depth, $rows, $verbose) = @_;

# Pattern
my $prefix = '';
if (my $i = $depth * 2) { $prefix .= ' ' x $i . '+' }
push @$table, my $row = [$prefix . ($route->pattern->pattern || '/')];
push @$rows, my $row = [$prefix . ($route->pattern->pattern || '/')];

# Methods
my $via = $route->via;
Expand All @@ -46,7 +46,7 @@ sub _walk {
push @$row, $regex if $verbose;

$depth++;
$self->_walk($_, $depth, $table, $verbose) for @{$route->children};
$self->_walk($_, $depth, $rows, $verbose) for @{$route->children};
$depth--;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base 'Mojolicious::Command';

use Getopt::Long 'GetOptions';
use Mojo::Server;
use Mojo::Util 'table';
use Mojo::Util 'tablify';

has hint => <<EOF;
Expand Down Expand Up @@ -79,10 +79,10 @@ sub run {
}

# Print list of all available commands
my $table = [[30, 48]];
push @$table, [' ' . $_->[0], $_->[1]->new->description]
my $rows = [];
push @$rows, [' ' . $_->[0], $_->[1]->new->description]
for sort { $a->[0] cmp $b->[0] } @commands;
return print $self->message, table($table), $self->hint;
return print $self->message, tablify($rows), $self->hint;
}

sub start_app {
Expand Down
17 changes: 7 additions & 10 deletions t/mojo/util.t
Expand Up @@ -13,7 +13,7 @@ use Mojo::Util
qw(decode dumper encode get_line hmac_sha1_sum html_unescape md5_bytes),
qw(md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_header spurt squish),
qw(steady_time table trim unindent unquote url_escape url_unescape),
qw(steady_time tablify trim unindent unquote url_escape url_unescape),
qw(xml_escape xor_encode);

# camelize
Expand Down Expand Up @@ -418,16 +418,13 @@ is MojoMonkeyTest::yin(), 'yin', 'right result';
ok !!MojoMonkeyTest->can('yang'), 'function "yang" exists';
is MojoMonkeyTest::yang(), 'yang', 'right result';

# table
is table([[5, 5], ["f\r\no o", 'bar']]), "fo o bar\n", 'right result';
is table([[5, 5], [" foo", ' b a r']]), " foo b a\n", 'right result';
is table([[50], ['foo']]), "foo\n", 'right result';
is table([[3], ['foo'], ['bar'], ['yada']]), "foo\nbar\nyad\n", 'right result';
is table([[3, 3], ['yada', 'yada'], ['foo', 'yada']]), "yad yad\nfoo yad\n",
# tablify
is tablify([["f\r\no o", 'bar']]), "fo o bar\n", 'right result';
is tablify([[" foo", ' b a r']]), " foo b a r\n", 'right result';
is tablify([['foo']]), "foo\n", 'right result';
is tablify([['foo', 'yada'], ['yada', 'yada']]), "foo yada\nyada yada\n",
'right result';
is table([[5, 3], ['foo', 'bar'], ['baz', 'yada']]), "foo bar\nbaz yada\n",
'right result';
is table([[10, 3, 1], ['foo', 'bar', 'baz'], ['yada', 'yada', 'yada']]),
is tablify([['foo', 'bar', 'baz'], ['yada', 'yada', 'yada']]),
"foo bar baz\nyada yada yada\n", 'right result';

# deprecated
Expand Down

0 comments on commit 18c7e41

Please sign in to comment.