public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Dominic Jäger" <d.jaeger@proxmox.com>
To: Thomas Lamprecht <t.lamprecht@proxmox.com>
Cc: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Subject: Re: [pve-devel] [PATCH manager] vzdump mail: Refactor text part
Date: Tue, 17 Nov 2020 09:35:56 +0100	[thread overview]
Message-ID: <20201117083556.GA1845190@mala.proxmox.com> (raw)
In-Reply-To: <16bc7f15-a092-f021-a710-a1900baf41f6@proxmox.com>

On Mon, Nov 16, 2020 at 06:12:37PM +0100, Thomas Lamprecht wrote:
> 
> Or move out even more than just the format string generation out, so that it
> becomes a simple loop calling
> 
> $text .= render_task_plain($vmid, $task);
> 
> or something similar to that.

my $namelength = 20;
$text .= sprintf ("%-10s %-${namelength}s %-6s %10s %10s  %s\n",
    qw(VMID NAME STATUS TIME SIZE FILENAME));
my $render_task_plain = sub {
    my ($vmid, $task) = @_;
    my $successful = $task->{state} eq 'ok';
    $text .= sprintf("%-10s %-${namelength}s %-6s %10s " .
	($successful ? "%10s": "%8.2fMB")."  %s\n", $task->{vmid},
	$name, $task->{state}, format_time($task->{backuptime}), $size,
	$filename);
};
foreach my $task (@$tasklist) {
    $text .= render_task_plain($vmid, $task);
}

Not sure about this, we cannot move the heading into render_task_plain => Still
two format strings? So I don't really see how we would benefit from the
additional sub.

> we could avoid the sub and the if by using the $size_conversion directly in
> the format string?

I personally would prefer this idea. Then we can still decide if we prefer
 1. Short, but two format strings written out

my $namelength = 20;
$text .= sprintf ("%-10s %-${namelength}s %-6s %10s %10s  %s\n",
    qw(VMID NAME STATUS TIME SIZE FILENAME));
foreach my $task (@$tasklist) {
    my $successful = $task->{state} eq 'ok';
    $text .= sprintf("%-10s %-${namelength}s %-6s %10s " . 
	($successful ? "%10s": "%8.2fMB")."  %s\n", $task->{vmid}, $name,
	$task->{state}, format_time($task->{backuptime}), $size, $filename);
    }

 2. or a longer version. We could put all the decisions into the $fmt sub (idea
thanks to Hannes). Then it's a little longer, but relatively easy to read I
think, and has no two written out format strings.

my $namelength = 20;
my $fmt = sub {
    my ($successful) = @_;
    my $fmt = "%-10s %-${namelength}s %-6s %10s ";
    $fmt .= $successful ? "%10s": "%8.2fMB";
    $fmt .= "  %s\n";
    return $fmt;
};
$text .= sprintf ($fmt->(1), qw(VMID NAME STATUS TIME SIZE FILENAME));
foreach my $task (@$tasklist) {
    my $name = substr($task->{hostname}, 0, $namelength);
    my $successful = $task->{state} eq 'ok';
    my $size = $successful ? format_size ($task->{size}) : 0;
    my $filename = $successful ? $task->{target} : '-';
    $text .= sprintf($fmt->($successful), $task->{vmid}, $name,
	$task->{state}, format_time($task->{backuptime}), $size, $filename);
}





  reply	other threads:[~2020-11-17  8:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-16 10:14 Dominic Jäger
2020-11-16 17:12 ` Thomas Lamprecht
2020-11-17  8:35   ` Dominic Jäger [this message]
2020-11-17  8:48     ` Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201117083556.GA1845190@mala.proxmox.com \
    --to=d.jaeger@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    --cc=t.lamprecht@proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal