summary refs log tree commit diff stats
diff options
context:
space:
mode:
authornfnty <git@nfnty.se>2017-02-10 22:34:01 +0100
committernfnty <git@nfnty.se>2017-02-10 22:41:17 +0100
commit9b0673cf05fc07a9131cd357ca75715c6ad0f11f (patch)
tree0c0cda254de4f55f9e6d94bbff5731a61ebe50d8
parent5866ab804be2395618542202936635407e65d15b (diff)
downloadranger-9b0673cf05fc07a9131cd357ca75715c6ad0f11f.tar.gz
gui.ansi.char_slice: Widestring aware
Fix Python 2 widestrings being cut off

Fixes #164
-rw-r--r--ranger/gui/ansi.py10
1 files changed, 6 insertions, 4 deletions
diff --git a/ranger/gui/ansi.py b/ranger/gui/ansi.py
index 5cb2ab90..ff8b2fd9 100644
--- a/ranger/gui/ansi.py
+++ b/ranger/gui/ansi.py
@@ -8,6 +8,7 @@ from __future__ import (absolute_import, division, print_function)
 
 import re
 
+from ranger.ext.widestring import WideString
 from ranger.gui import color
 
 
@@ -118,7 +119,7 @@ def char_len(ansi_text):
     >>> char_len("")
     0
     """
-    return len(ansi_re.sub('', ansi_text))
+    return len(WideString(ansi_re.sub('', ansi_text)))
 
 
 def char_slice(ansi_text, start, length):
@@ -153,19 +154,20 @@ def char_slice(ansi_text, start, length):
             last_color = chunk
             continue
 
+        chunk = WideString(chunk)
         old_pos = pos
         pos += len(chunk)
         if pos <= start:
             pass  # seek
         elif old_pos < start and pos >= start:
             chunks.append(last_color)
-            chunks.append(chunk[start - old_pos:start - old_pos + length])
+            chunks.append(str(chunk[start - old_pos:start - old_pos + length]))
         elif pos > length + start:
             chunks.append(last_color)
-            chunks.append(chunk[:start - old_pos + length])
+            chunks.append(str(chunk[:start - old_pos + length]))
         else:
             chunks.append(last_color)
-            chunks.append(chunk)
+            chunks.append(str(chunk))
         if pos - start >= length:
             break
     return ''.join(chunks)