JakeWharton / picnic

A Kotlin DSL and Java/Kotlin builder API for constructing HTML-like tables which can be rendered to text

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Columns are messed up with unicode chars

fardavide opened this issue · comments

Expected behavior

Tables to be correctly drawn with unicode characters

Current behavior

Tables are messed up with unicode characters


Version: 0.4.0
OS: MacOS BigSur - english language
IDE: IntelliJ Idea Ultimate 2020.2
Kotlin: 1.4.0
Project type: Multiplatform, JVM variant only for this module ( stdlib-jdk8 )
JDK: 14

Chinese chars

image

TEXT:

┌──────────────────────────────────────────────────────────────────────────────────┬─────────────┬───────────────────┐
│  呀〈KIBA〉 ~暗黒騎士鎧伝~                                                                │  Year 2011  │  Tmdb id: 257171  │
├──────────────────────────────────────────────────────────────────────────────────┴─────────────┴───────────────────┤
│  Actors: Masaki Kyômoto, Mika Hijii, Leah Dizon, Mikoto Inoue, Raima Hiramatsu, Kazuhiko Inoue, Miz...             │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│  Genres: Fantasy, Action, Adventure                                                                                │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

ANSI color code

image
image

TEXT EXAMPLE

"\u001B[46mWelcome to My App!\u001B[0m"

TEXT RESULT

┌───────────────────────────────────────────────────────────────────┐
│                                                                   │
│                    Welcome to My App!                    │
│                                                                   │
├──────────────────────────────────┬────────────────────────────────┤
│                                  │            commands            │
├──────────────────────────────────┼───────┬────────────────┬───────┤
│   Search a Movie by title        │   1   │   search       │   s   │
├──────────────────────────────────┼───────┼────────────────┼───────┤
│   Rate a Movie by id             │   2   │   rate         │   r   │
├──────────────────────────────────┼───────┼────────────────┼───────┤
│   Get suggested Movies for you   │   3   │   suggestion   │   g   │
└──────────────────────────────────┴───────┴────────────────┴───────┘

Ignoring ANSI colors, for which I've reopened your other issue, I think there's two problems here:

  1. There is no measuring for graphemes wider than one. This is our bug.

  2. Many monospace fonts do not contain these characters and so they end up falling back to non-monospace characters. There's no much we can do about this. It happens in the terminal long after we've measured.

    In your specific example, you can see how the widths of the characters are not lining up with 1 or 2 columns.

    Screen Shot 2020-08-25 at 11 04 22 AM

  1. I sadly discovered that

  2. After fixing that bug, am I right to expect the columns to be "almost aligned"?

I was wondering if there any kind of non-visible char that can be used to "balance" the row width.
Let's say something like:

  • expect row to be 20 points
  • current text is measured 11.5 points
  • let's add 8 spaces + "half space"

Looks like maybe http://unifoundry.com/unifont/index.html is a solution for the problem although it is not pretty 😬

Looks like maybe http://unifoundry.com/unifont/index.html is a solution for the problem although it is not pretty 😬

Quick dumb question: this solution can be applied only locally, right? I guess I can't ship it packed within my CLI app ( ? )

Yes. There is likely no way to solve this in the general case. The font of the terminal is completely opaque to the library. The person may not even be using monospace!

I see. Does this ticket still make sense to be open? Is there something more that can be done?

I think in general, no, I think there's nothing we can do. If there's actual bugs around measuring (like #20 was) we can fix those, but ultimately there's not much we can do here except recommend fonts which are fully monospace, despite being exceedingly rare.