aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/_pyrepl/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/_pyrepl/utils.py')
-rw-r--r--Lib/_pyrepl/utils.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/_pyrepl/utils.py b/Lib/_pyrepl/utils.py
new file mode 100644
index 00000000000..cd1df7c49a2
--- /dev/null
+++ b/Lib/_pyrepl/utils.py
@@ -0,0 +1,18 @@
+import re
+import unicodedata
+
+ANSI_ESCAPE_SEQUENCE = re.compile(r"\x1b\[[ -@]*[A-~]")
+
+
+def str_width(c: str) -> int:
+ w = unicodedata.east_asian_width(c)
+ if w in ('N', 'Na', 'H', 'A'):
+ return 1
+ return 2
+
+
+def wlen(s: str) -> int:
+ length = sum(str_width(i) for i in s)
+
+ # remove lengths of any escape sequences
+ return length - sum(len(i) for i in ANSI_ESCAPE_SEQUENCE.findall(s))