Skip to content

Commit

Permalink
Take unicode into account when finding the length of the prompt
Browse files Browse the repository at this point in the history
Addresses oils-for-unix#269.
Uses the  [wcwidth](https://pypi.org/project/wcwidth/) python library to
find the display width of a character.
  • Loading branch information
jyn514 committed Jun 22, 2019
1 parent 31fbf66 commit 08efae8
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/comp_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys

import libc

import wcwidth

_RESET = '\033[0;0m'
_BOLD = '\033[1m'
Expand All @@ -31,16 +31,18 @@


def _PromptLen(prompt_str):
"""Ignore all characters between \x01 and \x02."""
"""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."""
escaped = False
length = 0
for c in prompt_str:
for c in unicode(prompt_str, 'utf-8'):
if c == '\x01':
escaped = True
elif c == '\x02':
escaped = False
elif not escaped:
length += 1
length += wcwidth.wcwidth(c)
return length


Expand Down

0 comments on commit 08efae8

Please sign in to comment.