From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.jaeger@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 38530604DE
 for <pve-devel@lists.proxmox.com>; Tue, 17 Nov 2020 09:35:59 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 3281FD58B
 for <pve-devel@lists.proxmox.com>; Tue, 17 Nov 2020 09:35:59 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id AB9FCD581
 for <pve-devel@lists.proxmox.com>; Tue, 17 Nov 2020 09:35:58 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 751ED43725
 for <pve-devel@lists.proxmox.com>; Tue, 17 Nov 2020 09:35:58 +0100 (CET)
Date: Tue, 17 Nov 2020 09:35:56 +0100
From: Dominic =?iso-8859-1?Q?J=E4ger?= <d.jaeger@proxmox.com>
To: Thomas Lamprecht <t.lamprecht@proxmox.com>
Cc: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Message-ID: <20201117083556.GA1845190@mala.proxmox.com>
References: <20201116101457.61771-1-d.jaeger@proxmox.com>
 <16bc7f15-a092-f021-a710-a1900baf41f6@proxmox.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <16bc7f15-a092-f021-a710-a1900baf41f6@proxmox.com>
User-Agent: Mutt/1.10.1 (2018-07-13)
X-SPAM-LEVEL: Spam detection results:  0
 AWL 1.697 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: Re: [pve-devel] [PATCH manager] vzdump mail: Refactor text part
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 17 Nov 2020 08:35:59 -0000

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);
}