[pve-devel] [pve-zsync 1/2] fix cut_taget_width

Wolfgang Link w.link at proxmox.com
Tue Jan 10 10:56:51 CET 2017


---
 pve-zsync | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/pve-zsync b/pve-zsync
index 194eabf..8bd437e 100644
--- a/pve-zsync
+++ b/pve-zsync
@@ -72,19 +72,26 @@ sub check_bin {
 sub cut_target_width {
     my ($target, $max) = @_;
 
+    die "mim length is 5\n" if $max < 5;
+
     return  $target if (length($target) <= $max);
     my @spl = split('/', $target);
 
-    my $count = length($spl[@spl-1]);
-    return "..\/".substr($spl[@spl-1],($count-$max)+3 , $count) if $count > $max;
+    my $length = length($spl[@spl - 1]);
+    return "..\/".substr($spl[@spl - 1],($length - $max) + 3 , $length)
+	if $length + 3 >= $max;
+
+    $length += length($spl[1])  if @spl > 2;
+
+    return "\/".substr($spl[1], 0, ($max - 4) - length($spl[@spl - 1])).
+	"..\/".$spl[@spl - 1] if $length + 4 >= $max;
 
-    $count +=  length($spl[0]) if @spl > 1;
-    return substr($spl[0], 0, $max-4-length($spl[@spl-1]))."\/..\/".$spl[@spl-1] if $count > $max;
+    return "\/".substr($spl[1], 0, ($max - 5) - length($spl[@spl - 1])).
+	"\/..\/".$spl[@spl - 1] if $length + 5 >= $max;
 
-    my $rest = 1;
-    $rest = $max-$count if ($max-$count > 0);
+    my $rest = $max - $length;
 
-    return "$spl[0]".substr($target, length($spl[0]), $rest)."..\/".$spl[@spl-1];
+    return "\/$spl[1]\/".substr($target, length($spl[2]), $rest)."..\/".$spl[@spl-1];
 }
 
 sub lock {
-- 
2.1.4





More information about the pve-devel mailing list