layout of console output should adjust for double-width characters #242

Closed
charles-plessy opened this Issue Apr 10, 2015 · 6 comments

Projects

None yet

2 participants

@charles-plessy

Hello,

With hledger register, the information is displayed in a tabular manner, but this does not work when the description contains double-space characters, which are typically found in Chinese, Japanese or Korean (CJK) texts. This seems to be a limitation of the printf command used in hledger/hledger/Hledger/Cli/Register.hs, and I am not able to suggest a replacement, unfortunately.

Here is an example of a typically shifted output.

2015/04/10 Apples               expense:food                   1 $           1 $
                                asset:bank                    -1 $             0
2015/04/10 りんご                  expense:food                   1 $           1 $
                                asset:bank                    -1 $             0

Here is a joural file to reproduce the above.

; journal created 2015-04-10 by hledger

2015/04/10 Apples
    expense:food           1 $
    asset:bank

2015/04/10 りんご
    expense:food           1 $
    asset:bank

Many thanks for giving us hledger. I love it.

@simonmichael
Owner

Thanks Charles.

This is most visible with register, but also with balance -M if you have wide characters in account names.

What OS/application/font(s) are you using ? For me with Menlo or Monaco fonts, on OSX, in iTerm2, the double-width characters are rendered exactly two regular characters wide, but in a graphical Emacs they appear slightly less than two characters wide, I'm not sure why.

Still it seems like we could compensate for this somewhat.

@charles-plessy

I am running hledger on Linux (Debian Testing/Unstable), via the GNOME terminal, or via OSX's Terminal. The terminals display Wide characters (in the sense of the Unicode Standard Annex 11) with twice the space for Narrow characters, which I think is the correct thing to do.

I also noticed that web browsers fail to display correctly aligned monospace text containing a mixture of narrow and wide characters. For instance, the text below may be rendered unaligned in your browser, but if you paste it in your terminal it will be displayed correctly.

apple   10
りんご  9
リンゴ    7
苹果    6
猕猴桃  31
pear    16

The difference of rendering between web browsers and terminals is the amount of space (not characters, but just the distance) between east asian characters, that is shorter in the case of web browsers. This distance is the same as when rendering text in non-monospace context, therefore, my conclusion is that web browsers (and perhaps Emacs) do not provide a way to extend the concept of monospaced text to a mixture of east asian and latin characters.

So, altogether a working heuristic could be that for each wide character, hledger should remove a space.

@simonmichael
Owner

Interesting! So we won't worry about hledger-web right now. But that heuristic should help with the console UI. I made a start on it.

@simonmichael simonmichael added this to the 1.0 milestone May 19, 2015
@simonmichael simonmichael changed the title from hledger reg output shifted when description contains doublespace characters. to register output shifted when description contains doublespace characters. May 19, 2015
@simonmichael simonmichael changed the title from register output shifted when description contains doublespace characters. to register output shifted when description contains double-width characters May 19, 2015
@simonmichael simonmichael modified the milestone: 1.0, 0.27 Sep 2, 2015
@simonmichael simonmichael added a commit that referenced this issue Sep 29, 2015
@simonmichael register: wide-character-aware layout (#242)
Wide characters, eg chinese/japanese/korean characters, are typically
rendered wider than latin characters. In some applications (eg gnome
terminal or osx terminal) and fonts (eg monaco) they are exactly double
width. This is a start at making hledger aware of this. A register
report containing wide characters (in descriptions, account names, or
commodity symbols) should now align its columns correctly, when viewed
with a suitable font and application.
5b5e5ee
@simonmichael simonmichael added a commit that referenced this issue Sep 29, 2015
@simonmichael balance, print; more wide char support (#242)
Simple (non-multicolumn) balance reports containing wide characters
should now align correctly (in apps and fonts that show wide chars as
double width). Likewise, the print command.
42e2da4
@simonmichael simonmichael changed the title from register output shifted when description contains double-width characters to layout of console output should adjust for double-width characters Sep 29, 2015
@simonmichael simonmichael self-assigned this Oct 10, 2015
@simonmichael simonmichael added a commit that referenced this issue Oct 10, 2015
@simonmichael print: fix wide char support, add tests (#242)
The print command wasn't lining up amounts with wide chars in account
names, fixed it properly this time. Transaction and Posting's Show instances
should also be wide-char-aware now.
3b40edb
@simonmichael
Owner

With a decent terminal & font, all cli reports and hledger-ui now seem to align robustly with any mix of ascii and wide characters in codes/descriptions/account names/commodity symbols/comments. It feels pretty nice. Please reopen if you find more cases, hledger-web excluded.

@charles-plessy

It took me some time before I could upgrade to test it, but I confirm that it works perfectly, thanks a lot !

@simonmichael
Owner

Great, thanks for confirming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment