On Mon, Sep 25, 2017 at 10:13:29AM +0200, Thomas Lamprecht wrote:
> On 09/25/2017 07:05 AM, Dietmar Maurer wrote:
> > 
> > > + sub {
> > > +     my ($res) = @_;
> > > +     my $longest = 0;
> > > +     foreach my $role(map($_->{roleid}, @$res)) {
> > > +         my $len = length $role;
> > > +         $longest = $len if $len > $longest;
> > > +     }
> > > +     $longest += 2;
> > > +     my $width = 96;
> > > +     my $maxlength = $width - $longest;
> > > +     printf("%-${longest}s%s\n", "ROLE", "PRIVILEGES");
> > > +     for my $role(sort {lc($a->{roleid}) cmp lc($b->{roleid})} @$res) {
> > > +         my @lines_privs = ("");
> > > +         my $cur_line = 0;
> > > +         for my $priv(split(',', $role->{privs})) {
> > > +             if (length($lines_privs[$cur_line]) == 0) {
> > > +                 $lines_privs[$cur_line] .= "$priv";
> > > +             } elsif (length($lines_privs[$cur_line]) + length($priv) <= 
> > > $maxlength)
> > > {
> > > +                 $lines_privs[$cur_line] .= ", $priv";
> > > +             } else {
> > > +                 $lines_privs[++$cur_line] .= "$priv";
> > > +             }
> > > +         }
> > > +         printf("%-${longest}s%s\n", "$role->{roleid}:", 
> > > $lines_privs[0]);
> > > +         for my $line(1..(scalar(@lines_privs) - 1)) {
> > > +             printf("%${longest}s%s\n", "", $lines_privs[$line]);
> > > +         }
> > > +     }
> > 
> > I would like to have a generic utility function to print nicely formatted 
> > tables
> > instead.
> > 
> 
> Philip, do you have interest to take a look at that?
> Would be nice if we could unify all this.
> 
I think Alwin planned to adopt that as a pet project. I wouldn't wanna
take it form him.

> We have such or similar implementations in:
> 
> pvesm status
> pct list
> qm list
> 
> possible other places too, just mentioning those from top of my head.
> 
> We probably can go a long way if we can tell the formatter that a
> column is left, right and maybe center aligned.
> We currently just pad columns where we know that the may get long,
> but padding all and call it a day would work too.
> 
> Your formatting (which looks nice, btw) would need an additional
> "wrap column line" feature.
> 
> The signature of this all could be a small class or just a method,
> what's nicer to use and reuse.
> We could pass a Column headings to alignment mapping and , e.g.:
> 
> my $columns = {
>     Name => { align => 'l', data => 'id' },
>     Type => { align => 'c', data => 'type' },
>     ...,
>     'Used %' => { align => 'r', data => sub { my $d = shift; return 
> ($d->{used}/$d->{total}) * 100 },
> };
> Where l,r,c are for left-, right- and center-padded, respectively.
> And data is either a hash key if scalar or a coderef to allow making
> transformation on the data or calculating columns which are not
> directly available from the data.
> 
> PVE::FormatTable::print($columns, $data);
> 
> Just a rough draft what would be needed and idea how the interface
> could look like..
> 
> Perl modules which could be looked at, e.g. for inspiration:
> http://search.cpan.org/~shlomif/Text-Table-1.133/lib/Text/Table.pm
> http://search.cpan.org/~cub/Text-SimpleTable-AutoWidth-0.09/lib/Text/SimpleTable/AutoWidth.pm
> http://search.cpan.org/dist/Text-FormatTable/lib/Text/FormatTable.pm
> (the last ones source doesn't looks to big)
> 
> Maybe, if their quite small, support our use cases and do not need
> to much extra dependencies (at best none :) them self and look like
> they will stay with us longer (i.e. active and have some
> reverse-dependencies in Debian) we could also use this directly.

_______________________________________________
pve-devel mailing list
pve-devel@pve.proxmox.com
https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to