def _PromptLen(prompt_str): """Ignore all characters between \x01 and \x02 and handle unicode characters. In particular, the display width of a string may be different from either the number of bytes or the number of unicode characters. Additionally, if there are multiple lines in the prompt, only give the length of the last line.""" escaped = False display_str = "" for c in prompt_str: if c == '\x01': escaped = True elif c == '\x02': escaped = False elif not escaped: display_str += c last_line = display_str.split('\n')[-1] try: width = libc.wcswidth(last_line) # en_US.UTF-8 locale missing, just return the number of bytes except (SystemError, UnicodeError): return len(display_str) if width == -1: return len(display_str) return width
def testWcsWidth(self): self.assertEqual(1, libc.wcswidth("▶️")) self.assertEqual(28, libc.wcswidth("(osh) ~/.../unchanged/oil ▶️ ")) self.assertEqual(2, libc.wcswidth("→ ")) self.assertRaises(UnicodeError, libc.wcswidth, "\xfe")