From 8203ef6fb6bfa8db3ec5b176c8fd77451d1ea762 Mon Sep 17 00:00:00 2001 From: Eliran Wong Date: Thu, 2 Nov 2023 00:02:18 +0000 Subject: [PATCH] v35.19; improved wrapping CJK words --- UniqueBibleAppVersion.txt | 2 +- latest_changes.txt | 3 +++ patches.txt | 12 +++++----- startup/nonGui.py | 2 +- util/ConfigUtil.py | 2 +- util/LocalCliHandler.py | 47 ++++++++++++++++++++++++++++----------- 6 files changed, 46 insertions(+), 22 deletions(-) diff --git a/UniqueBibleAppVersion.txt b/UniqueBibleAppVersion.txt index acd3e0f495..ac1e4c7f39 100755 --- a/UniqueBibleAppVersion.txt +++ b/UniqueBibleAppVersion.txt @@ -1 +1 @@ -35.18 +35.19 diff --git a/latest_changes.txt b/latest_changes.txt index 7d4fcd509a..d885dd2500 100755 --- a/latest_changes.txt +++ b/latest_changes.txt @@ -1,3 +1,6 @@ +Changes in 35.19: +* improved wrapping Chinese, Japanese and Korean words in terminal mode + Changes in 35.18: * fixed bible data diff --git a/patches.txt b/patches.txt index 0861d6d605..f780e535ff 100755 --- a/patches.txt +++ b/patches.txt @@ -1297,7 +1297,6 @@ (33.96, "file", "gui/SimpleBrowser.py") (33.96, "file", "gui/WorkSpace.py") (33.96, "file", "gui/YouTubePopover.py") -(33.96, "file", "startup/nonGui.py") (33.98, "file", "gui/MenuItems.py") (34.04, "file", "htmlResources/material/action/terminal/materialiconsoutlined/48dp/2x/outline_terminal_black_48dp.png") (34.05, "file", "plugins/menu/Terminal.py") @@ -1385,11 +1384,12 @@ (35.12, "file", "plugins/terminal/open html content with w3m.py") (35.13, "file", "util/CrossPlatform.py") (35.13, "file", "util/TextEditorUtility.py") -(35.14, "file", "util/ConfigUtil.py") (35.16, "file", "util/VlcUtil.py") -(35.16, "file", "util/LocalCliHandler.py") (35.17, "file", "util/TextUtil.py") (35.18, "file", "util/TextCommandParser.py") -(35.18, "file", "patches.txt") -(35.18, "file", "UniqueBibleAppVersion.txt") -(35.18, "file", "latest_changes.txt") +(35.19, "file", "startup/nonGui.py") +(35.19, "file", "util/LocalCliHandler.py") +(35.19, "file", "util/ConfigUtil.py") +(35.19, "file", "patches.txt") +(35.19, "file", "UniqueBibleAppVersion.txt") +(35.19, "file", "latest_changes.txt") diff --git a/startup/nonGui.py b/startup/nonGui.py index ff7cba714a..73be97dc1e 100755 --- a/startup/nonGui.py +++ b/startup/nonGui.py @@ -112,7 +112,7 @@ def run_terminal_mode(): print(f"Running Unique Bible App {config.version} in terminal mode ...") if ("Art" in config.enabled): from art import text2art - print(text2art("UBA")) + print(text2art("UBA")[:-1]) checkMigration() needUpdate = checkApplicationUpdateCli() diff --git a/util/ConfigUtil.py b/util/ConfigUtil.py index 4af0efabac..81cdbc85d1 100644 --- a/util/ConfigUtil.py +++ b/util/ConfigUtil.py @@ -164,7 +164,7 @@ def getCurrentVenvDir(): # Start of terminal mode setting setConfig("terminalWrapWords", """ # Wrap words in terminal mode.""", - False) + True) setConfig("terminalEnableTermuxAPI", """ # Option to enable use of Termux:API tools in UBA. # Make sure you have both Termux:API app and termux-api package installed if you want to enable it. diff --git a/util/LocalCliHandler.py b/util/LocalCliHandler.py index f5ad0e7203..7482701af9 100644 --- a/util/LocalCliHandler.py +++ b/util/LocalCliHandler.py @@ -1,4 +1,4 @@ -import re, config, pprint, os, requests, platform, pydoc, markdown, sys, subprocess, json, shutil, webbrowser, traceback, textwrap, wcwidth +import re, config, pprint, os, requests, platform, pydoc, markdown, sys, subprocess, json, shutil, webbrowser, traceback, textwrap, wcwidth, unicodedata import openai, threading, time from duckduckgo_search import ddg from functools import partial @@ -158,6 +158,12 @@ def setOsOpenCmd(self): elif platform.system() == "Windows": config.open = config.openWindows + def is_CJK(self, text): + for char in text: + if 'CJK' in unicodedata.name(char): + return True + return False + # wrap html text at spaces def getWrappedHTMLText(self, text, terminal_width=None): if not " " in text: @@ -169,20 +175,35 @@ def getWrappedHTMLText(self, text, terminal_width=None): def addWords(words): words = words.split(" ") - length = len(words) for index, item in enumerate(words): - isLastItem = (length - index == 1) - itemWidth = self.getStringWidth(item) - if isLastItem: - newLineWidth = self.lineWidth + itemWidth - else: - newLineWidth = self.lineWidth + itemWidth + 1 - if newLineWidth > terminal_width: - self.wrappedText += f"\n{item}" if isLastItem else f"\n{item} " - self.lineWidth = itemWidth if isLastItem else itemWidth + 1 + isLastItem = (len(words) - index == 1) + + if self.is_CJK(item): + for iIndex, i in enumerate(item): + isSpaceItem = (not isLastItem and (len(item) - iIndex == 1)) + iWidth = self.getStringWidth(i) + if isSpaceItem: + newLineWidth = self.lineWidth + iWidth + 1 + else: + newLineWidth = self.lineWidth + iWidth + if newLineWidth > terminal_width: + self.wrappedText += f"\n{i} " if isSpaceItem else f"\n{i}" + self.lineWidth = iWidth + 1 if isSpaceItem else iWidth + else: + self.wrappedText += f"{i} " if isSpaceItem else i + self.lineWidth += iWidth + 1 if isSpaceItem else iWidth else: - self.wrappedText += item if isLastItem else f"{item} " - self.lineWidth += itemWidth if isLastItem else itemWidth + 1 + itemWidth = self.getStringWidth(item) + if isLastItem: + newLineWidth = self.lineWidth + itemWidth + else: + newLineWidth = self.lineWidth + itemWidth + 1 + if newLineWidth > terminal_width: + self.wrappedText += f"\n{item}" if isLastItem else f"\n{item} " + self.lineWidth = itemWidth if isLastItem else itemWidth + 1 + else: + self.wrappedText += item if isLastItem else f"{item} " + self.lineWidth += itemWidth if isLastItem else itemWidth + 1 def processLine(lineText): if re.search("<[^<>]+?>", lineText):