studio
studio copied to clipboard
EEZ Flow: make string formatting function String.format(...) similar to sprintf.
Nice to have and also see discussion in #196
d3 might be an option for dashboards. Plotly is also using this. The nice thing about d3 is that is has native support for SI prefixes.
You mean this: https://d3js.org/d3-format ?
You mean this: https://d3js.org/d3-format ?
Yes
I will add String.format
function. This function will use d3-format
for Dashboard and sprintf
for LVGL and EEZ-GUI projects.
Regarding sprintf
usage, It will not support full format strings supported by this functions. But, you can use it to format for example single double like this: String.format("%7.3f", float_val)
. This is only temporary solution. Ideally, we will need library for C that works exactly like d3-format
library. Once this library is implemented, format specification in String.format
should be written without %
as first character to use this library, otherwise sprintf
will be still used. This will ensure backward compatibility.
String.format
can be used in combination with multiline strings with embedded expressions for full formatting of strings.
Implemented as described in my previous comment.
I did some research on the background of d3-format
:
It implements (and extends) the Python formatspec.
In C++ there is a library that also implements formatspec
named fmt. This requires C++. I did not find a pure C implementation. Don't know if this is a show stopper.
Important note:
For engineering applications (like measurement/SCPI instruments) the formatPrefix(specifier, value)
extension of d3-format
is really useful to display values with an SI-prefix. I did not find the equivalent in fmt
or Python's formatspec
, but it can probably be added.
I did not find a pure C implementation. Don't know if this is a show stopper.
Not necessary. I only prefer C libraries over C++ as they are often more lightweight (this is important for embedded usage) and simpler to use.
I will check fmt library and see if it is suitable for our purpose. We can add a #ifdef to disable it and fallback to sprintf in case when it is too much for the target embedded platform.
For engineering applications (like measurement/SCPI instruments) the formatPrefix(specifier, value) extension of d3-format is really useful to display values with an SI-prefix
We can add this for the dashboard projects only at first.
The function String.formatPrefix
is added for the Dashboard projects
Have noticed a bug in version 0.16.1 in the implementation of String.format
To reproduce:
- Create a label on any screen.
- Make the label an expression with
String.format(".2f",1.1)
- In
EDIT
mode, observe the label correctly display as1.10
- Switch to
RUN
orDEBUG
mode, observe the label display as.2f
See ticket #546
B
Format specification for the String.format
function when used in LVGL project must use %
prefix. So, your expression should be String.format("%.2f",1.1)
.