studio icon indicating copy to clipboard operation
studio copied to clipboard

EEZ Flow: make string formatting function String.format(...) similar to sprintf.

Open fietser28 opened this issue 1 year ago • 10 comments

Nice to have and also see discussion in #196

fietser28 avatar Jun 15 '23 14:06 fietser28

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.

fietser28 avatar Jun 15 '24 09:06 fietser28

You mean this: https://d3js.org/d3-format ?

mvladic avatar Jun 15 '24 16:06 mvladic

You mean this: https://d3js.org/d3-format ?

Yes

fietser28 avatar Jun 15 '24 22:06 fietser28

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.

mvladic avatar Jul 15 '24 22:07 mvladic

Implemented as described in my previous comment.

mvladic avatar Jul 15 '24 22:07 mvladic

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.

fietser28 avatar Jul 16 '24 15:07 fietser28

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.

mvladic avatar Jul 16 '24 15:07 mvladic

The function String.formatPrefix is added for the Dashboard projects

mvladic avatar Jul 18 '24 08:07 mvladic

Have noticed a bug in version 0.16.1 in the implementation of String.format

To reproduce:

  1. Create a label on any screen.
  2. Make the label an expression with String.format(".2f",1.1)
  3. In EDIT mode, observe the label correctly display as 1.10
  4. Switch to RUN or DEBUG mode, observe the label display as .2f

See ticket #546

B

bentron2000 avatar Sep 20 '24 03:09 bentron2000

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).

mvladic avatar Sep 20 '24 14:09 mvladic