text: more chinese related fixes

Michael Muré created

Change summary

util/text/text.go      | 13 ++++++++++---
util/text/text_test.go | 20 +++++++++++++++++---
2 files changed, 27 insertions(+), 6 deletions(-)

Detailed changes

util/text/text.go 🔗

@@ -58,7 +58,7 @@ func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) {
 			} else {
 				// Break a word longer than a line
 				if wordLength > lineWidth {
-					for wordLength > 0 && len(word) > 0 {
+					for wordLength > 0 && wordLen(word) > 0 {
 						l := minInt(spaceLeft, wordLength)
 						part, leftover := splitWord(word, l)
 						word = leftover
@@ -76,6 +76,10 @@ func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) {
 							nbLine++
 							spaceLeft = lineWidth - leftPad
 						}
+
+						if wordLength <= 0 {
+							break
+						}
 					}
 				} else {
 					// Normal break
@@ -90,8 +94,11 @@ func WrapLeftPadded(text string, lineWidth int, leftPad int) (string, int) {
 			}
 		}
 
-		textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
-		lineBuffer.Reset()
+		if lineBuffer.Len() > 0 {
+			textBuffer.WriteString(pad + strings.TrimRight(lineBuffer.String(), " "))
+			lineBuffer.Reset()
+		}
+
 		firstLine = false
 	}
 

util/text/text_test.go 🔗

@@ -126,9 +126,18 @@ func TestWrapLeftPadded(t *testing.T) {
 		{
 			"The Lorem ipsum text is typically composed of pseudo-Latin words. It is commonly used as placeholder text to examine or demonstrate the visual effects of various graphic design.",
 			`    The Lorem ipsum text is typically composed of
-		pseudo-Latin words. It is commonly used as placeholder
-		text to examine or demonstrate the visual effects of
-		various graphic design.`,
+    pseudo-Latin words. It is commonly used as placeholder
+    text to examine or demonstrate the visual effects of
+    various graphic design.`,
+			59, 4,
+		},
+		// Handle Chinese
+		{
+			"婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂一洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋佫呬耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲兀,嵲媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。",
+			`    婞一枳郲逴靲屮蜧曀殳,掫乇峔掮傎溒兀緉冘仜。郼牪艽螗媷錵朸一詅掜豗怙刉笀丌,楀棶乇矹迡搦囷圣亍昄漚粁仈祂。覂一
+    洳袶揙楱亍滻瘯毌,掗屮柅軡菵腩乜榵毌夯。勼哻怌婇怤灟葠雺奷朾恦扰衪岨坋誁乇芚誙腞。冇笉妺悆浂鱦賌廌灱灱觓坋佫呬
+    耴跣兀枔蓔輈。嵅咍犴膰痭瘰机一靬涽捊矷尒玶乇,煚塈丌岰陊鉖怞戉兀甿跾觓夬侄。棩岧汌橩僁螗玎一逭舴圂衪扐衲兀,嵲
+    媕亍衩衿溽昃夯丌侄蒰扂丱呤。毰侘妅錣廇螉仴一暀淖蚗佶庂咺丌,輀鈁乇彽洢溦洰氶乇构碨洐巿阹。`,
 			59, 4,
 		},
 	}
@@ -239,6 +248,11 @@ func TestSplitWord(t *testing.T) {
 			2,
 			"快檢", "什麼望對",
 		},
+		{
+			"快檢什麼望對",
+			3,
+			"快檢什", "麼望對",
+		},
 		// Handle chinese with colors
 		{
 			"快\x1b[31m檢什麼\x1b[0m望對",