3 ms·
Good explanation of how (some) pretty printers work. There is one snag, at least on my browser (Brave on MacOS). The final example with line width 120 is marre
by tromp 3y ago
Good explanation of how (some) pretty printers work.
There is one snag, at least on my browser (Brave on MacOS).
The final example with line width 120 is marred by having some lines wrapped, no matter how I resize my browser window or resize the text:
"move_learn_method": {"name": "egg", "url": "https://pokeapi.co/api/v2/move-learn-
method/2/"},
"version_group": {"name": "gold-silver", "url": "https://pokeapi.co/api/v2/version-
group/3/"}
Which runs rather counter to all the pretty printer's efforts to make things fit on a line.
The page source shows it embedded in <pre><code><small><small> </small></small></code></pre>
How could they have prevented the browser from introducing line breaks? E.g. by making it use scrollbars?
- Joker_vD 3y agoSee also [0] for description of Dart's auto-formatter for how (some) other pretty printers work. P.S. "unicode-width is to get accurate string widths for monospaced terminal fonts including full-width Unicode characters. I’m pretty sure this is an impossible problem" ― it's not impossible, it's just that different terminals (and different terminal fonts) have different ideas on the amount of screen cells occupied by the Unicode codepoints and the chances that they will coincide with the ideas of the Unicode library your app uses are pretty slim. For instance, '\N{WOMAN}\N{ZWJ}\N{HEAVY BLACK HEART}\N{VARIATION SELECTOR-16}\N{ZWJ}\N{MAN}' could be either 2 cells if your terminal and font support the single combined 'Couple with Heart: Woman, Man' emoji; or 6 cells if they fall back to displaying it as a sequence of 'Woman', 'Red Heart', 'Man' emojis; or 5 cells if your terminal fails to recognize that '\N{HEAVY BLACK HEART}\N{VARIATION SELECTOR-16}' actually takes 2 cells, not 1: GNOME Terminal 3.28.2 for instance, does that although it still paints the 2-cells wide 'Red Heart' emoji which leads to visual overlap ― how do you even account for that?! [0] https://journal.stuffwithstuff.com/2015/09/08/the-hardest-program-ive-ever-written/ https://journal.stuffwithstuff.com/2015/09/08/the-hardest-pr...
- justinpombrio 3y ago> How could they have prevented the browser from introducing line breaks? Fixed it with: <code style="white-space: pre">
- IshKebab 3y agoThe Prettier algorithm (and this modification) don't actually use a hard length limit so it isn't unexpected to go over the limit a bit.