diff options
author | Nikita Mikhailov <nslqqq@gmail.com> | 2015-03-08 18:29:14 +0100 |
---|---|---|
committer | Nikita Mikhailov <nslqqq@gmail.com> | 2015-03-08 19:02:32 +0100 |
commit | 63ae447a0fb30e0be364837ca0ceec6cfc12d252 (patch) | |
tree | 762cb0a251134ed05fd616077cd3d390427d1174 /lib/strings.nix | |
parent | 32ee876647124410c16a26162f555a3f498c07fd (diff) | |
download | nixlib-63ae447a0fb30e0be364837ca0ceec6cfc12d252.tar nixlib-63ae447a0fb30e0be364837ca0ceec6cfc12d252.tar.gz nixlib-63ae447a0fb30e0be364837ca0ceec6cfc12d252.tar.bz2 nixlib-63ae447a0fb30e0be364837ca0ceec6cfc12d252.tar.lz nixlib-63ae447a0fb30e0be364837ca0ceec6cfc12d252.tar.xz nixlib-63ae447a0fb30e0be364837ca0ceec6cfc12d252.tar.zst nixlib-63ae447a0fb30e0be364837ca0ceec6cfc12d252.zip |
Add 'fixedWidthString' and 'fixedWidthNumber' formatting functions
Diffstat (limited to 'lib/strings.nix')
-rw-r--r-- | lib/strings.nix | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/lib/strings.nix b/lib/strings.nix index 56d990de62da..39112407c570 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); } |