summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-09-30 00:42:24 +0200
committerhut <hut@lavabit.com>2010-09-30 00:42:24 +0200
commitd4900452fca51685349966d527d173fdefe83f08 (patch)
treea57f8aacf7069296668851dad794f7061ef9dbcb
parent483f11640f911767dfedba74f1db3efd819a68c6 (diff)
downloadranger-d4900452fca51685349966d527d173fdefe83f08.tar.gz
ext.utfwidth: stuff
-rw-r--r--ranger/ext/utfwidth.py18
-rw-r--r--ranger/gui/bar.py5
-rw-r--r--test/tc_utfwidth.py2
3 files changed, 12 insertions, 13 deletions
diff --git a/ranger/ext/utfwidth.py b/ranger/ext/utfwidth.py
index 885e1381..364db757 100644
--- a/ranger/ext/utfwidth.py
+++ b/ranger/ext/utfwidth.py
@@ -18,28 +18,26 @@
 # ----
 # This file contains portions of code from cmus (uchar.c).
 
-import sys
+try:
+	from sys import maxint
+except:
+	from sys import maxsize as maxint
 
 NARROW = 1
 WIDE = 2
 
-def uwid(string):
+def uwid(string, count=maxint):
 	"""Return the width of a string"""
 	end = len(string)
 	i = 0
 	width = 0
-	while i < end:
+	while i < end and count:
 		bytelen = utf_byte_length(string[i:])
 		width += utf_char_width(string[i:i+bytelen])
 		i += bytelen
+		count -= 1
 	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)
@@ -51,7 +49,7 @@ def uchars(string):
 		i += bytelen
 	return result
 
-def uwidslice(string, start=0, end=sys.maxint):
+def uwidslice(string, start=0, end=maxint):
 	chars = []
 	for c in uchars(string):
 		c_wid = utf_char_width(c)
diff --git a/ranger/gui/bar.py b/ranger/gui/bar.py
index ab38562b..56a9d97f 100644
--- a/ranger/gui/bar.py
+++ b/ranger/gui/bar.py
@@ -13,8 +13,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from ranger.ext.utfwidth import uwid, uwidslice, utf_char_width, \
-		uwid_of_first_char
+from ranger.ext.utfwidth import uwid, uwidslice, utf_char_width
 
 class Bar(object):
 	left = None
@@ -77,7 +76,7 @@ class Bar(object):
 		for item in self.left:
 			if not item.fixed:
 				itemlen = uwid(item.string)
-				minimal_width = uwid_of_first_char(item.string)
+				minimal_width = uwid(item.string, count=1)
 				if oversize > itemlen - minimal_width:
 					item.cut_off_to(minimal_width)
 					oversize -= (itemlen - minimal_width)
diff --git a/test/tc_utfwidth.py b/test/tc_utfwidth.py
index 0288c17b..67ff609e 100644
--- a/test/tc_utfwidth.py
+++ b/test/tc_utfwidth.py
@@ -42,5 +42,7 @@ class Test(TestCase):
 		self.assertEqual(4, uwid("asdf"))
 		self.assertEqual(5, uwid("löööl"))
 		self.assertEqual(6, uwid("バババ"))
+		self.assertEqual(1, uwid("äsdf", count=1))
+		self.assertEqual(2, uwid("バババ", count=1))
 
 if __name__ == '__main__': main()