summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2011-10-01 17:57:09 +0200
committerhut <hut@lavabit.com>2011-10-01 17:57:09 +0200
commit7247fa714f1c39e6996f844261d57dca252f638d (patch)
tree1ba42cc8911d5fbbd3d571dd911eac5971f23eb7
parent964308467831d5e1bf93b29533d2cbe89102fd8c (diff)
downloadranger-7247fa714f1c39e6996f844261d57dca252f638d.tar.gz
gui.bar: fixing unicode in bars...
-rw-r--r--ranger/gui/bar.py27
-rw-r--r--ranger/gui/widgets/statusbar.py2
-rw-r--r--ranger/gui/widgets/titlebar.py4
3 files changed, 22 insertions, 11 deletions
diff --git a/ranger/gui/bar.py b/ranger/gui/bar.py
index ef769ca5..0f2d8cd8 100644
--- a/ranger/gui/bar.py
+++ b/ranger/gui/bar.py
@@ -13,7 +13,9 @@
 # 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.widestring import uwid
+from ranger.ext.widestring import WideString, utf_char_width
+import sys
+PY3 = sys.version > '3'
 
 class Bar(object):
 	left = None
@@ -45,7 +47,7 @@ class Bar(object):
 		# remove elemets from the left until it fits
 		if sumsize > wid:
 			while len(self.left) > 0:
-				leftsize -= len(self.left.pop(-1).string)
+				leftsize -= len(self.left.pop(-1))
 				if leftsize + rightsize <= wid:
 					break
 			sumsize = leftsize + rightsize
@@ -53,7 +55,7 @@ class Bar(object):
 			# remove elemets from the right until it fits
 			if sumsize > wid:
 				while len(self.right) > 0:
-					rightsize -= len(self.right.pop(0).string)
+					rightsize -= len(self.right.pop(0))
 					if leftsize + rightsize <= wid:
 						break
 				sumsize = leftsize + rightsize
@@ -117,7 +119,7 @@ class BarSide(list):
 			if item.fixed:
 				n += len(item)
 			else:
-				n += 1
+				n += item.width_of_first_letter
 		return n
 
 	def nonfixed_items(self):
@@ -126,19 +128,28 @@ class BarSide(list):
 
 class ColoredString(object):
 	def __init__(self, string, *lst):
-		self.string = string
+		self.string = WideString(string)
 		self.lst = lst
 		self.fixed = False
+		if not len(string):
+			self.width_of_first_letter = 0
+		elif PY3:
+			self.width_of_first_letter = utf_char_width(string[0])
+		else:
+			self.width_of_first_letter = utf_char_width(self.string.chars[0].decode('utf-8'))
 
 	def cut_off(self, n):
 		if n >= 1:
 			self.string = self.string[:-n]
 
 	def cut_off_to(self, n):
-		self.string = self.string[:n]
+		if n < self.width_of_first_letter:
+			self.string = self.string[:self.width_of_first_letter]
+		elif n < len(self.string):
+			self.string = self.string[:n]
 
 	def __len__(self):
-		return uwid(self.string)
+		return len(self.string)
 
 	def __str__(self):
-		return self.string
+		return str(self.string)
diff --git a/ranger/gui/widgets/statusbar.py b/ranger/gui/widgets/statusbar.py
index b7ab123c..d8704af3 100644
--- a/ranger/gui/widgets/statusbar.py
+++ b/ranger/gui/widgets/statusbar.py
@@ -267,7 +267,7 @@ class StatusBar(Widget):
 		self.win.move(0, 0)
 		for part in result:
 			self.color(*part.lst)
-			self.addstr(part.string)
+			self.addstr(str(part))
 		self.color_reset()
 
 class Message(object):
diff --git a/ranger/gui/widgets/titlebar.py b/ranger/gui/widgets/titlebar.py
index d87a0803..c1994b5b 100644
--- a/ranger/gui/widgets/titlebar.py
+++ b/ranger/gui/widgets/titlebar.py
@@ -77,7 +77,7 @@ class TitleBar(Widget):
 
 		pos = 0
 		for i, part in enumerate(self.result):
-			pos += len(part.string)
+			pos += len(part)
 			if event.x < pos:
 				if i < 2:
 					self.fm.enter_dir("~")
@@ -159,5 +159,5 @@ class TitleBar(Widget):
 		self.win.move(0, 0)
 		for part in result:
 			self.color(*part.lst)
-			self.addstr(part.string)
+			self.addstr(str(part))
 		self.color_reset()
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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480