From 8ca75066c73a51320be8861a34a86ec4c198614c Mon Sep 17 00:00:00 2001
From: Eric Oulashin <eric.oulashin@gmail.com>
Date: Sat, 22 Jun 2024 21:11:47 -0700
Subject: [PATCH] dd_ligbtar_menu.js: Fix for behavior with the item color when
 printing the last portion of an item. Also, UTF8 detection improvement. The
 menu items have an isUTF8 property, which must be set to true by the
 developer if it is known that the item text has UTF-8 characters.  Sometimes
 (as discovered when listing personal emails), an item might have UTF-8
 characters but the isUTF8 properrty wouldn't have been set to true.  In that
 case, try to detect whether the text has UTF-8 using str_is_utf8() (which
 maybe should always be used instead of the isUTF8 proprety). Also, this
 includes an improvement for item formatting with the background color if it
 still gets into the 'else' case where it thinks the item text doesn't have
 UTF-8 characters when it actually does.

---
 exec/load/dd_lightbar_menu.js | 37 ++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/exec/load/dd_lightbar_menu.js b/exec/load/dd_lightbar_menu.js
index 3437760357..1b3a18552a 100644
--- a/exec/load/dd_lightbar_menu.js
+++ b/exec/load/dd_lightbar_menu.js
@@ -1187,6 +1187,18 @@ function DDLightbarMenu_WriteItem(pIdx, pItemLen, pHighlight, pSelected, pScreen
 {
 	var itemText = this.GetItemText(pIdx, pItemLen, pHighlight, pSelected);
 	/*
+	// Temporary
+	if (user.is_sysop)
+	{
+		console.pushxy();
+		console.gotoxy(1, 1);
+		console.attributes = "N";
+		console.print("itemText is: " + typeof(itemText) + "\x01;");
+		console.popxy();
+	}
+	// End Temporary
+	*/
+	/*
 	// If the text is UTF-8 and the user's terminal is UTF-8, then set the mode bit accordingly.
 	// If the text is UTF-8 and the user's terminal doesn't support UTF-8, convert the text to cp437.
 	var printModeBits = P_NONE;
@@ -1293,8 +1305,23 @@ function DDLightbarMenu_WriteItem(pIdx, pItemLen, pHighlight, pSelected, pScreen
 			var printedLen = console.strlen(itemText, P_AUTO_UTF8);
 			if (printedLen < itemLen)
 			{
-				console.print(this.GetColorForItem(pIdx, pHighlight));
-				printf("%*s", itemLen - printedLen, "");
+				var remainingLen = itemLen - printedLen;
+				var itemColor = this.GetColorForItem(pIdx, pHighlight);
+				// If the item color is an array of colors for various parts of the text,
+				// then get all the attributes up to the current text index
+				if (Array.isArray(itemColor))
+				{
+					var tmpItemColor = "\x01n";
+					var startIdx = printedLen - remainingLen - 1;
+					for (var idx = 0; idx < itemColor.length; ++idx)
+					{
+						if (itemColor[idx].start <= startIdx)
+							tmpItemColor += itemColor[idx].attrs;
+					}
+					itemColor = tmpItemColor;
+				}
+				console.print(itemColor);
+				printf("%*s", remainingLen, "");
 				console.attributes = "N";
 			}
 		}
@@ -1714,7 +1741,11 @@ function DDLightbarMenu_ItemTextIsUTF8(pIdx)
 	if (pIdx < 0 || pIdx >= this.NumItems())
 		return false;
 
-	return this.GetItem(pIdx).textIsUTF8;
+	var item = this.GetItem(pIdx);
+	var isUTF8 = item.textIsUTF8;
+	if (!isUTF8)
+		isUTF8 = str_is_utf8(item.text);
+	return isUTF8;
 }
 
 // Erases the menu - Draws black (normal color) where the menu was
-- 
GitLab