#paragraph #unicode-characters #unicode-text #text #justification

bin+lib justify

优雅处理 Unicode 的同时,对纯文本进行对齐

4 个版本

使用旧 Rust 2015

0.1.3 2020 年 9 月 9 日
0.1.2 2018 年 2 月 19 日
0.1.1 2018 年 1 月 23 日
0.1.0 2018 年 1 月 23 日

#1469 in 文本处理

MIT/Apache

23KB
344

justify.rs

此 crate 以(主要是)Unicode 友好的方式对纯文本进行对齐,以便在终端仿真器中显示。

使用示例可以在文件 tests/tests.rs 中找到。

如果编译此 crate 时启用了 unicode-width 功能(例如,通过 cargo build --features unicode-width),则可以优雅地处理 Unicode。使用此功能,如韩文字符한占用两个空格,而组合字符占用 0 个。如果没有此功能,每个 Unicode 字符都占用一个空格,这可能导致某些情况下输出不佳。如果您将仅对 ASCII 文本或拉丁语言的 NFC 标准化 Unicode 文本进行对齐,则不需要此功能。

宽度信息由 wcwidth crate 提供。

未启用 unicode-width(示例文本来自 此处

"Korea"  is the modern spelling of "Corea", a name attested in English as  early
as  1614.[citation  needed] Korea was transliterated as Cauli in The Travels  of
Marco  Polo,[10] based on the kingdom of Goryeo (Hangul: 고려; Hanja:  高麗;
MR:  Koryŏ), which ruled most of the Korean peninsula during Marco Polo's time.
Korea's  introduction to the West resulted from trade and contact with merchants
from  Arabic  lands,[11]  with  some  records dating back  as  far  as  the  9th
century.[12]  Goryeo's  name  was  a continuation  of  Goguryeo  (Koguryŏ)  the
northernmost  of  the  Three Kingdoms of Korea, which was  officially  known  as
Goryeo  beginning in the 5th century.[13] The original name was a combination of
the  adjective  go ("high, lofty") with the name of a local Yemaek tribe,  whose
original  name  is  thought to have been either *Guru  (溝樓,  "walled  city,"
inferred   from  some  toponyms  in  Chinese  historical  documents)  or  *Gauri
(가우리, "center"). 

启用 unicode-widthwcwidth: trueSettings 结构中

"Korea"  is the modern spelling of "Corea", a name attested in English as  early
as  1614.[citation  needed] Korea was transliterated as Cauli in The Travels  of
Marco  Polo,[10] based on the kingdom of Goryeo (Hangul: 고려; Hanja: 高麗;  MR:
Koryŏ),  which  ruled  most of the Korean peninsula during  Marco  Polo's  time.
Korea's  introduction to the West resulted from trade and contact with merchants
from  Arabic  lands,[11]  with  some  records dating back  as  far  as  the  9th
century.[12]  Goryeo's  name  was  a  continuation  of  Goguryeo  (Koguryŏ)  the
northernmost  of  the  Three Kingdoms of Korea, which was  officially  known  as
Goryeo  beginning in the 5th century.[13] The original name was a combination of
the  adjective  go ("high, lofty") with the name of a local Yemaek tribe,  whose
original  name  is  thought  to have been either  *Guru  (溝樓,  "walled  city,"
inferred  from some toponyms in Chinese historical documents) or *Gauri (가우리,
"center").  

请注意,启用 unicode-width 后,对齐效果更好,但仍有一些行对齐不准确。这是因为并非总是可以完美对齐:由于韩文字符占用两个终端空格,而拉丁字母占用一个,因此可能存在奇数个字符的行进行对齐。此外,根据您的浏览器,它可能看起来不正确,请尝试将其粘贴到终端仿真器中。

依赖项

~93KB