chiark / gitweb /
commitid.scad.pl: make rjustt take $prefix (nfc as yet)
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 5 Feb 2016 19:06:55 +0000 (19:06 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 5 Feb 2016 19:06:55 +0000 (19:06 +0000)
commitid.scad.pl

index ebdbe917a39a6cb73db24da322d83a22e85af045..fbeed6fe1a4518f42374901c003303f342b3d7c3 100755 (executable)
@@ -268,16 +268,21 @@ our $do_git; # contains may chars 'c' (count) and/or 'o' (object)
 our $do_git_untracked = 1;
 our $argcounter;
 
-sub rjustt ($$) { # right justify and truncate (ie, pad and truncate at left)
-    my ($sz, $whole) = @_;
+sub rjustt ($$;$) { # right justify and truncate (ie, pad and truncate at left)
+                    # always includes prefix
+    my ($sz, $whole, $prefix) = @_;
+    $prefix //= '';
     my $lw = length $whole;
-    return $lw > $sz
-       ? substr($whole, $lw-$sz)
-       : sprintf "%${sz}s", $whole;
+    my $spare = $sz - $lw - (length $prefix);
+    return
+       $prefix.
+       ($spare > 0 ? (' ' x $spare) : '').
+       substr($whole, ($spare < 0 ? -$spare : 0));
 }
 
-sub ljustt ($$$) { # always includes $suffix
+sub ljustt ($$;$) {
     my ($sz, $whole, $suffix) = @_;
+    $suffix //= '';
     $sz -= length $suffix;
     return sprintf "%-${sz}.${sz}s%s", $whole, $suffix;
 }