-
Notifications
You must be signed in to change notification settings - Fork 576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Permit Mojo::Util::tablify to work on non-rectangular arrays. #1132
Conversation
Well clearly you need to make sure it passes existing tests. Once you've done that can you please add a test and verify the formatting; that second line looks longer than the character limit. |
lib/Mojo/Util.pm
Outdated
@@ -282,8 +282,8 @@ sub tablify { | |||
} | |||
} | |||
|
|||
my $format = join ' ', map({"\%-${_}s"} @spec[0 .. $#spec - 1]), '%s'; | |||
return join '', map { sprintf "$format\n", @$_ } @$rows; | |||
my @formats = (map({"\%-${_}s"} @spec[0 .. $#spec - 1]),'%s'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parentheses around the map are unnecessary. This should be run through perltidy.
lib/Mojo/Util.pm
Outdated
my $format = join ' ', map({"\%-${_}s"} @spec[0 .. $#spec - 1]), '%s'; | ||
return join '', map { sprintf "$format\n", @$_ } @$rows; | ||
my @formats = (map({"\%-${_}s"} @spec[0 .. $#spec - 1]),'%s'); | ||
return join '', map { sprintf join(' ', @formats[0..scalar @$_ - 1]) . "\n", @$_ } @$rows; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Columns should be joined on 2 spaces, this appears to be the cause of test failures. @formats[0..scalar @$_ - 1]
can be shortened to @formats[0..$#$_]
…use of constant in void context
I am still unsure why: |
I'm neutral to the change, though I can't come up with a case myself where I would give (Tests are still missing) |
I lean slightly with @jhthorsen, especially since the data could be rectangularlized fairly easily by the user (push to unfilled rows with That said, if it can be made more flexible and DWIM without any cost, I don't see any reason to oppose (once the PR works and tests are added). |
@lindleyw: Could you please provide a real life example for when you don't have rectangular data? |
@lindleyw Running Since this is a MakeMaker based dist, you can also run the tests as |
No need to add extra lines. diff --git a/lib/Mojo/Util.pm b/lib/Mojo/Util.pm
index c6c2e3491..5438d1621 100644
--- a/lib/Mojo/Util.pm
+++ b/lib/Mojo/Util.pm
@@ -282,8 +282,8 @@ sub tablify {
}
}
- my $format = join ' ', map({"\%-${_}s"} @spec[0 .. $#spec - 1]), '%s';
- return join '', map { sprintf "$format\n", @$_ } @$rows;
+ my @fs = (map({"\%-${_}s"} @spec[0 .. $#spec - 1]), '%s');
+ return join '', map { sprintf join(' ', @fs[0 .. $#$_]) . "\n", @$_ } @$rows;
}
sub term_escape { |
Has this pull request been abandoned? |
I was away for awhile. Back now; will proceed. |
I kinda wanted to merge this for the last release, but the pull request has still not been fixed. |
Resolved by #1150. |
Summary
Should a non-rectangular array (one in which not all rows have equal numbers of columns) be passed to Mojo::Util::tablify, the resulting columns or entirely empty rows will be skipped rather than producing errors from missing sprintf() arguments.
Motivation
Users who write simple classes based on Mojolicious::Command should be able to produce arrays for table output, perhaps from a variety of functions or a JSON structure, without having to ensure that each row has an equal number of columns.
References
https://irclog.perlgeek.de/mojo/2017-09-14#i_15164351