diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/text/text.go | 10 | ||||
-rw-r--r-- | util/text/text_test.go | 22 |
2 files changed, 18 insertions, 14 deletions
diff --git a/util/text/text.go b/util/text/text.go index f8910c2e..ee9d278c 100644 --- a/util/text/text.go +++ b/util/text/text.go @@ -100,9 +100,9 @@ func wrapText(s string, w int) string { word += string(r) } } - // The text may end without newlines, ensure flushing it or we can lose the - // last word. - flushWord() + // The text may end without newlines, ensure flushing it or we can lose the + // last word. + flushWord() return out } @@ -113,12 +113,12 @@ func wordLen(word string) int { length := 0 escape := false - for _, char := range []rune(word) { + for _, char := range word { if char == '\x1b' { escape = true } if !escape { - length += runewidth.RuneWidth(char) + length += runewidth.RuneWidth(rune(char)) } if char == 'm' { escape = false diff --git a/util/text/text_test.go b/util/text/text_test.go index 38e747df..c70d2ccd 100644 --- a/util/text/text_test.go +++ b/util/text/text_test.go @@ -89,16 +89,16 @@ func TestWrap(t *testing.T) { " This\nis a\nlist:\n\n\n *\nfoo\n *\nbar\n\n\n *\nbaz\nBAM\n", 6, }, - // Handle chinese + // Handle chinese (wide characters) { "婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。", - "婞一枳郲逴靲屮蜧曀殳,掫\n乇峔掮傎溒兀緉冘仜。", + "婞一枳郲逴靲\n屮蜧曀殳,掫\n乇峔掮傎溒兀\n緉冘仜。", 12, }, // Handle chinese with colors { "婞一枳郲逴\x1b[31m靲屮蜧曀殳,掫乇峔掮傎溒\x1b[0m兀緉冘仜。", - "婞一枳郲逴\x1b[31m靲屮蜧曀殳,掫\n乇峔掮傎溒\x1b[0m兀緉冘仜。", + "婞一枳郲逴\x1b[31m靲\n屮蜧曀殳,掫\n乇峔掮傎溒\x1b[0m兀\n緉冘仜。", 12, }, } @@ -134,10 +134,14 @@ func TestWrapLeftPadded(t *testing.T) { // Handle Chinese { "婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。", - ` 婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂一 - 洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋佫呬 - 耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲兀,嵲 - 媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。`, + ` 婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷 + 錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂 + 一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟 + 葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋 + 佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈 + 丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲 + 兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖 + 蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。`, 59, 4, }, } @@ -182,12 +186,12 @@ func TestWordLen(t *testing.T) { // Handle chinese { "快檢什麼望對", - 6, + 12, }, // Handle chinese with colors { "快\x1b[31m檢什麼\x1b[0m望對", - 6, + 12, }, } |