Unicode "Grapheme Break" support (user-perceived character breaks, with libunistring)
This is a discussion of a feature that would use a library.
htop often needs breaks of file paths of command lines, as well as other strings that may contain arbitrary Unicode text.
In discussions of #462 and #854, about shortening a path of a working directory for display, I think it becomes apparent that we need to support the Unicode algorithm of user-perceived character boundaries.
In Unicode, this is defined in UAX 29 section 3: https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries with a normative database: https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakProperty.txt
Not to be confused with Unicode line breaking algorithm (UAX 14).
What are the "Grapheme Cluster Boundaries" in Unicode and why are they important for htop?
When htop needs to truncate a file path or a command line argument string, it is supposed to remove "characters" from the path string until the string fits the given terminal column width. However, Unicode characters (code points) do not always have one-to-one relationships to the "characters" that user could perceive as one unit.
Special cases can include:
- Combining character sequences
- Character sequences with ZWJ (Zero Width Joiner, U+200D), commonly found in emoji sequences.
- Regional indicator symbols (a.k.a. flag emojis). These regional indicator character should be in pairs and break cannot happen within each pair.
- Languages that use a syllable as the minimum grapheme unit. This is the least concern for htop but I mention for completeness.
I don't think people would like a series of United Kingdom flag emojis () suddenly transform into Bulgaria flag emojis plus a letter B () when characters are cut at a wrong location. This is just an example.
The grapheme cluster boundary algorithm requires a database, and I'm not wishing to embed the whole database into htop. What I am suggesting is to incorporate libunistring, a library from GNU that had the database for our needs. Specifically the unigbrk.h APIs.
The license for libunistring is "LGPLv3+ or GPLv2+" (dual-licensed), and so is compatible to htop's license (GPLv2+).
Source: htop-dev/htop