summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--lib/strings.nix11
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index 56d990de62d..39112407c57 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -208,4 +208,15 @@ rec {
   # standard GNU Autoconf scripts.
   enableFeature = enable: feat: "--${if enable then "enable" else "disable"}-${feat}";
 
+  # Create a fixed width string with additional prefix to match required width
+  fixedWidthString = width: filler: str:
+    let
+      strw = lib.stringLength str;
+      reqWidth = width - (lib.stringLength filler);
+    in
+      assert strw <= width;
+      if strw == width then str else filler + fixedWidthString reqWidth filler str;
+
+  # Format a number adding leading zeroes up to fixed width
+  fixedWidthNumber = width: n: fixedWidthString width "0" (toString n);
 }