![]() |
![]() |
![]() |
![]() |
libnkutils Format string formatFormat string format — specification of an NkFormatString |
A format string can contain references to a data in a shell-like format: ${
.
Each reference is resolved to a data that is substitued to the reference at the corresponding place in the string.
The reference-name
}reference-name
can be composed of alphabetic characters, dashes ('-'
) and underscores ('_'
).
You can use an array-like syntax for some values: ${
(square brackets reference-name
[key
]}[]
are literals here).
The key
can be a name or a number depending on the data pointed by reference-name
.
If key
starts with a @
, then the remaining is used as a join value.
You can use fallback values, substitute values or regexes on array members. In case of joined values, such operations are done on the joined values as a whole.
You can use a fallback value if the data resolves to nothing
or false
.
The syntax is: ${
.
reference-name
:-fallback-value
}
You can use a substitute value if (and only if) the data resolves to something
or true
.
The syntax is: ${
.
reference-name
:+substitute-value
}
You can use a substitute value if (and only if) the data resolves to nothing
or false
.
The syntax is: ${
.
reference-name
:!substitute-value
}
You can use a switch substitute if the data resolves to a boolean.
The syntax is: ${
(spaces added for readability only, they are not ignored when parsed).
reference-name
:{separator
value-if-false
separator
value-if-true
}}
You can use a range substitute if the data resolves to a number.
The syntax is: ${
(spaces added for readability only, they are not ignored when parsed, the external square brackets reference-name
:[separator
range-start
separator
range-end
[separator
substitute-value
…]]}[]
are literals here).
You can use a prettify flag to make a big value more readable.
The syntax is: ${
.
The following flags are supported:
reference-name
(flag
[addition
])}
|
Work on a number. It will display the value as a float, allowing for extra formatting, as with printf: Optionally, you can add a width (optionally with zero-padding): Optionally, you can add a precision: |
|
Work on a number. They will reduce the number to a minimum, adding the corresponding SI prefix (or binary prefix for You can use the same additions as flag |
|
Work on a number. The number will be used as a UNIX timestamp. You can have an strftime-like format string as addition. |
|
Work on a number. The number will be used as seconds.
You can have a nested format string as addition, with the following data available:
The default format is |
You can replace parts of the data using the regex mechanism.
The syntax is: ${
.
The reference-name
/regex
/replacement
}replacement
part is optional (including the preceding forward slash). If it is omitted, matching parts of the data will simply be removed.
You can repeat the /
part to replace as many patterns as you like.
Each pattern is matched against the previous replacement so be careful in your order.
regex
/replacement
Examples (with data and resolved string):
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |
|
Resolves to: |