xpl / as-table

A simple function that prints objects as ASCII tables. Supports ANSI styling and weird Unicode 💩 emojis – they won't break the layout.
http://npmjs.com/package/as-table
MIT License
62 stars 10 forks source link

double width unicode character not displayed correctly #12

Open mknj opened 3 years ago

mknj commented 3 years ago

Problem

Tables with double width unicode characters are not correctly aligned (see screenshot).

observed output

grafik

sidenote

The vi Editor in the same terminal window can handles these characters correctly. (the cursor does not jump left/right when going up/down on lines that contain such characters)

versions

example program

const asTable = require ('as-table').configure({delimiter:" | "})

console.log(asTable ( [ 
{ string: 'abcde',      num: "ok" },
{ string: 'ᄍ', num: "ok" },
{ string: '無', num: "bad" },
{ string: '🕑', num: "bad" },
{ string: '💩', num: "bad" },
{ string: '⬢', num: "ok" },
]))
mknj commented 3 years ago

The function stringWidth from "string-with" reports the correct width of strings, but strlen from "printable-characters" does not.

import p from 'printable-characters'
import a from 'as-table'
import stringWidth from 'string-width'
const asTable=a.configure({delimiter:" | "})
const d=[
{ string: 'abcde',      num: "ok" },
{ string: 'ᄍ', num: "ok" },
{ string: '無', num: "bad" },
{ string: '🕑', num: "bad" },
{ string: '💩', num: "bad" },
{ string: '⬢', num: "ok" },
]
for(const e of d) {
  e.stringWidth=stringWidth(e.string)
  e.strlen=p.strlen(e.string)
}
console.log(asTable (d) )

result

string | num | stringWidth | strlen
-----------------------------------
abcde  | ok  | 5           | 5     
ᄍ      | ok  | 1           | 1     
無      | bad | 2           | 1     
🕑      | bad | 2           | 1     
💩      | bad | 2           | 1     
⬢      | ok  | 1           | 1     
xpl commented 3 years ago

Thanks for the report. Seems that emoji rendering is terminal-dependent, sometimes it's double width, sometimes single width...

And in VSCode it's somehow 1.5-width:

Screen Shot 2021-06-19 at 1 06 23 AM

It also seems that now most terminals render it as double width (several years ago it was different), so we probably need to change printable-characters to account for that.