atorus-research / Tplyr

https://atorus-research.github.io/Tplyr/
Other
95 stars 17 forks source link

Width preserving parenthesis hugging for format strings #117

Closed mstackhouse closed 1 year ago

mstackhouse commented 1 year ago

In the current formatting available within format strings, any non-numeric character will preserve it's space and align directly, regardless of the integer. So, for example - this is how Tplyr will generally format things. If the format_string parameter of f_str() is set as xx (xxx.x%)

 5 ( 50.0%)
10 (100.0%)
 4 ( 40.0%)

An alternative to this would be to let integers pad themselves, using a format_string like x (x.x%). The resulting formatted data would look like:

5 (50.0%)
10 (100.0%)
4 (40.0%)

This distorts alignment though. But the missing capability would be to format the data with width preserving parentheses. Basically, keep decimal alignment within a number "group". The resulting format should look like:

 5  (50.0%)
10 (100.0%)
 4  (40.0%)

This update will require updates to f_str() object and related formatting application. As Tplyr is also within a stable release, we need to maintain full backwards compatibility. My current thought is that we add a new "special" character in formatting to signify formatting in this manner. Since Tplyr looks for lower case 'x', we can use an uppercase 'X' instead. The rules around this would be:

The same logic could also apply to auto formatting by using a capital A instead of the lowercase.

So to achieve:

 5  (50.0%)
10 (100.0%)
 4  (40.0%)

The format xx (XXX.x%) could be used.