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()
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403