Crystal: Discuss: Determines monospace(unicode) display width of a string

Created on 22 Oct 2017  ·  9Comments  ·  Source: crystal-lang/crystal

Crystal was built-in size and byte size in String, why not add monospace(unicode) display width, it's better in terminal and other usage.

Examples:

hello | size: 5, bytesize: 5, display width: 5
你好 | size: 2, bytesize: 6, display width: 4
hello | size: 5, bytesize: 15, display width: 10
здравствуйте | size: 12, bytesize: 24, display width: 12
namasté | size: 7, bytesize: 8, display width: 7
márħaban | size: 8, bytesize: 10, display width: 8
여보세요 | size: 4, bytesize: 12, display width: 8
おはよう | size: 4, bytesize: 12, display width: 8
مرحبًا | size: 6, bytesize: 12, display width: 5
здравствуйте | size: 12, bytesize: 24, display width: 12

terminal display width:

|hello|
+-----+
+----+
|你好|
+----+
+----------+
|hello|
+----------+
+------------+
|здравствуйте|
+------------+
+-------+
|namasté|
+-------+
+--------+
|márħaban|
+--------+
+--------+
|여보세요|
+--------+
+--------+
|おはよう|
+--------+
+-----+
|مرحبًا|
+-----+
+------------+
|здравствуйте|
+------------+

Ruby implementations:

Most helpful comment

However this causes real problem, in crystal tool context for example:

2017-10-23 18 19 38

Japanese (also size != display_width) named variable breaks table column.

All 9 comments

Sounds like a shard idea to me. Perhaps a bit heavy and obscure for the stdlib.

However this causes real problem, in crystal tool context for example:

2017-10-23 18 19 38

Japanese (also size != display_width) named variable breaks table column.

It was affect all CJK languages and emoji too

Couldn't we use libc's wcswidth() function for that?

I'm also not sure this belongs to the standard library, despite that fact that some tools won't work well (well, they work well, you just get a display that's not nice, but it's still easy to understand). But if wcswidth is portable we could use it (I don't know if that works well, though, there doesn't seem to be a standard way to know the width of a char).

Golang has a text library, and support width: https://github.com/golang/text/blob/master/width/width.go#L139

Yes, a library. It's not in the standard library.

You are right. Maybe make a shard is best choice.

We can always import it later from a shard if required.

Was this page helpful?
0 / 5 - 0 ratings