about summary refs log tree commit diff stats
path: root/ranger/ext/utfwidth.py
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-09-29 23:15:02 +0200
committerhut <hut@lavabit.com>2010-09-29 23:15:02 +0200
commite6dda13a71168f9ec4a1e4844edad5a3257803e9 (patch)
tree24b28098822d16f49207f058815bcd63a8aa10be /ranger/ext/utfwidth.py
parentaf17562d670575582ff6091d22d32a8d140a5f1a (diff)
downloadranger-e6dda13a71168f9ec4a1e4844edad5a3257803e9.tar.gz
gui.bar: improved titlebar shortening for utf file/dirnames
Diffstat (limited to 'ranger/ext/utfwidth.py')
-rw-r--r--ranger/ext/utfwidth.py19
1 files changed, 19 insertions, 0 deletions
diff --git a/ranger/ext/utfwidth.py b/ranger/ext/utfwidth.py
index a506c676..885e1381 100644
--- a/ranger/ext/utfwidth.py
+++ b/ranger/ext/utfwidth.py
@@ -18,6 +18,8 @@
 # ----
 # This file contains portions of code from cmus (uchar.c).
 
+import sys
+
 NARROW = 1
 WIDE = 2
 
@@ -32,6 +34,12 @@ def uwid(string):
 		i += bytelen
 	return width
 
+def uwid_of_first_char(string):
+	if not string:
+		return NARROW
+	bytelen = utf_byte_length(string[0])
+	return utf_char_width(string[:bytelen])
+
 def uchars(string):
 	"""Return a list with one string for each character"""
 	end = len(string)
@@ -43,6 +51,17 @@ def uchars(string):
 		i += bytelen
 	return result
 
+def uwidslice(string, start=0, end=sys.maxint):
+	chars = []
+	for c in uchars(string):
+		c_wid = utf_char_width(c)
+		if c_wid == NARROW:
+			chars.append(c)
+		elif c_wid == WIDE:
+			chars.append("")
+			chars.append(c)
+	return "".join(chars[start:end])
+
 def utf_byte_length(string):
 	"""Return the byte length of one utf character"""
 	firstord = ord(string[0])