atorus-research / Tplyr

Home Page:https://atorus-research.github.io/Tplyr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Width preserving parenthesis hugging for format strings

mstackhouse opened this issue · comments

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:

  • Uppercase 'X' signifies a specific width
  • Where an uppercase 'X' exists, the format string will identify the preceding character
    • internally within Tplyr, the replacement string would end up looking like %s %s%) - so the preceding character is removed from the replacement string, then the f_str() would need to store the preceding character within the number group to talk with num_fmt()
  • When formatting, the total width of the number group will be preserved, the number will be right aligned, and the preceding character will concatenate into that replacement within num_fmt().

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.