From 7247fa714f1c39e6996f844261d57dca252f638d Mon Sep 17 00:00:00 2001 From: hut Date: Sat, 1 Oct 2011 17:57:09 +0200 Subject: gui.bar: fixing unicode in bars... --- ranger/gui/bar.py | 27 +++++++++++++++++++-------- ranger/gui/widgets/statusbar.py | 2 +- ranger/gui/widgets/titlebar.py | 4 ++-- 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 . -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() -- cgit 1.4.1-2-gfad0 =cfbb8c84355f28bc7f669105f90c7aae0a972569'>cfbb8c84 ^