From 9cce9fab0792ee0ac17ad4355d9af9274159f294 Mon Sep 17 00:00:00 2001 From: hut Date: Thu, 31 Dec 2009 15:23:43 +0100 Subject: updated statusbar/titlebar --- ranger/gui/bar.py | 124 ++++++++++++++++++++++++++++++++++++++ ranger/gui/widgets/statusbar.py | 130 ++++++++++++++++------------------------ ranger/gui/widgets/titlebar.py | 130 +++------------------------------------- 3 files changed, 184 insertions(+), 200 deletions(-) create mode 100644 ranger/gui/bar.py diff --git a/ranger/gui/bar.py b/ranger/gui/bar.py new file mode 100644 index 00000000..57550399 --- /dev/null +++ b/ranger/gui/bar.py @@ -0,0 +1,124 @@ +class Bar(object): + left = None + right = None + gap = None + + def __init__(self, base_color_tag): + self.left = BarSide(base_color_tag) + self.right = BarSide(base_color_tag) + self.gap = BarSide(base_color_tag) + + def add(self, *a, **kw): + self.left.add(*a, **kw) + + def addright(self, *a, **kw): + self.right.add(*a, **kw) + + def sumsize(self): + return self.left.sumsize() + self.right.sumsize() + + def fixedsize(self): + return self.left.fixedsize() + self.right.fixedsize() + + def shrink_by_removing(self, wid): + leftsize = self.left.sumsize() + rightsize = self.right.sumsize() + sumsize = leftsize + rightsize + + # remove elemets from the left until it fits + if sumsize > wid: + while len(self.left) > 0: + leftsize -= len(self.left.pop(-1).string) + if leftsize + rightsize <= wid: + break + sumsize = leftsize + rightsize + + # remove elemets from the right until it fits + if sumsize > wid: + while len(self.right) > 0: + rightsize -= len(self.right.pop(0).string) + if leftsize + rightsize <= wid: + break + sumsize = leftsize + rightsize + + if sumsize < wid: + self.fill_gap(' ', (wid - sumsize), gapwidth=True) + + def shrink_by_cutting(self, wid): + fixedsize = self.fixedsize() + if wid < fixedsize: + raise ValueError("Cannot shrink down to that size by cutting") + + leftsize = self.left.sumsize() + rightsize = self.right.sumsize() + nonfixed_items = self.left.nonfixed_items() + + itemsize = int(float(wid - rightsize - fixedsize) / nonfixed_items) + 1 + + for item in self.left: + if not item.fixed: + item.cut_off_to(itemsize) + + self.fill_gap(' ', wid, gapwidth=False) + + def fill_gap(self, char, wid, gapwidth=False): + del self.gap[:] + + if not gapwidth: + wid = wid - self.sumsize() + + if wid > 0: + self.gap.add(char * wid, 'space') + + def combine(self): + return self.left + self.gap + self.right + + +class BarSide(list): + def __init__(self, base_color_tag): + self.base_color_tag = base_color_tag + + def add(self, string, *lst, **kw): + cs = ColoredString(string, self.base_color_tag, *lst) + if 'fixedsize' in kw: + cs.fixed = kw['fixedsize'] + self.append(cs) + + def add_space(self, n=1): + self.add(' ' * n, 'space') + + def sumsize(self): + return sum(len(item) for item in self) + + def fixedsize(self): + n = 0 + for item in self: + if item.fixed: + n += len(item) + else: + n += 1 + return n + + def nonfixed_items(self): + return sum(1 for item in self if not item.fixed) + + +class ColoredString(object): + fixed = False + + def __init__(self, string, *lst): + self.string = string + self.lst = lst + + def cut_off(self, n): + n = max(n, min(len(self.string), 1)) + self.string = self.string[:-n] + + def cut_off_to(self, n): + self.string = self.string[:n] + + def __len__(self): + return len(self.string) + + def __str__(self): + return self.string diff --git a/ranger/gui/widgets/statusbar.py b/ranger/gui/widgets/statusbar.py index 1715a490..b5f3bdce 100644 --- a/ranger/gui/widgets/statusbar.py +++ b/ranger/gui/widgets/statusbar.py @@ -1,6 +1,9 @@ -"""The StatusBar displays information about the currently selected file -in the same form as the "ls -l" command on the left side, and -some additional info about the current directory on the right side. +""" +The statusbar displays information about the current file and directory. + +On the left side, there is a display similar to what "ls -l" would +print for the current file. The right side shows directory information +such as the space used by all the files in this directory. """ from . import Widget @@ -9,6 +12,8 @@ from grp import getgrgid from os import getuid from time import strftime, localtime +from ranger.gui.bar import Bar + class StatusBar(Widget): __doc__ = __doc__ owners = {} @@ -44,13 +49,18 @@ class StatusBar(Widget): self.old_mtime = mtime self.old_cf = self.env.cf self.old_wid = self.wid + self._calc_bar() - left = self._get_left_part() - right = self._get_right_part() - - self.result = self._combine_parts(left, right) self._print_result(self.result) + def _calc_bar(self): + bar = Bar('in_statusbar') + self._get_left_part(bar) + self._get_right_part(bar) + bar.shrink_by_removing(self.wid) + + self.result = bar.combine() + def _draw_message(self): highlight = True space_left = self.wid @@ -71,43 +81,38 @@ class StatusBar(Widget): # if starting_point >= self.wid: # break - def _get_left_part(self): - part = [] - + def _get_left_part(self, bar): + left = bar.left + if self.column is not None: target = self.column.target.pointed_obj else: target = self.env.at_level(0).pointed_obj if target is None: - return part + return if target.accessible is False: - return part + return perms = target.get_permission_string() - color = ['permissions'] - if getuid() == target.stat.st_uid: - color.append('good') - else: - color.append('bad') - part.append([color, perms]) - - part.append([['space'], " "]) - part.append([['nlink'], str(target.stat.st_nlink)]) - part.append([['space'], " "]) - part.append([['owner'], self._get_owner(target)]) - part.append([['space'], " "]) - part.append([['group'], self._get_group(target)]) - part.append([['space'], " "]) + how = getuid() == target.stat.st_uid and 'good' or 'bad' + left.add(perms, 'permissions', how) + + left.add_space() + left.add(str(target.stat.st_nlink), 'nlink') + left.add_space() + left.add(self._get_owner(target), 'owner') + left.add_space() + left.add(self._get_group(target), 'group') + left.add_space() + if target.islink: - color = ['link'] - color.append(target.exists and 'good' or 'bad') - part.append([color, '-> ' + target.readlink]) + how = target.exists and 'good' or 'bad' + left.add('-> ' + target.readlink, 'link', how) else: - part.append([['mtime'], strftime(self.timeformat, \ - localtime(target.stat.st_mtime))]) - return part + left.add(strftime(self.timeformat, + localtime(target.stat.st_mtime)), 'mtime') def _get_owner(self, target): uid = target.stat.st_uid @@ -133,68 +138,39 @@ class StatusBar(Widget): except KeyError: return str(gid) - def _get_right_part(self): - part = [] + def _get_right_part(self, bar): + right = bar.right if self.column is None: - return part + return target = self.column.target -# target = self.env.at_level(0) if not target.content_loaded or not target.accessible: - return part + return pos = target.scroll_begin max_pos = len(target) - self.column.hei + base = 'scroll' if target.marked_items: - part.append([['scroll', 'marked'], 'Mrk']) + # Indicate that there are marked files. Useful if you scroll + # away and don't see them anymore. + right.add('Mrk', base, 'marked') elif max_pos > 0: if pos == 0: - part.append([['scroll', 'top'], 'Top']) + right.add('Top', base, 'top') elif pos >= max_pos: - part.append([['scroll', 'bot'], 'Bot']) + right.add('Bot', base, 'bot') else: - part.append([['scroll', 'percentage'], \ - '{0:0>.0f}%'.format(100.0 * pos / max_pos)]) + right.add('{0:0>.0f}%'.format(100.0 * pos / max_pos), + base, 'percentage') else: - part.append([['scroll', 'all'], 'All']) - return part - - def _combine_parts(self, left, right): - """Combines left and right, filling the middle with spaces and - removing elements which don't have enough room to fit in. - will be turned into the result (which is also returned). - """ - - leftsize = sum(len(part[1]) for part in left) - rightsize = sum(len(part[1]) for part in right) - sumsize = leftsize + rightsize - - # remove elemets from the left until it fits - if sumsize > self.wid: - while len(left) > 0: - leftsize -= len(left.pop(-1)[1]) - if leftsize + rightsize <= self.wid: - break - sumsize = leftsize + rightsize - - # remove elemets from the right until it fits - if sumsize > self.wid: - while len(right) > 0: - rightsize -= len(right.pop(0)[1]) - if leftsize + rightsize <= self.wid: - break - sumsize = leftsize + rightsize - - if sumsize < self.wid: - left.append([ ['space'], " " * (self.wid - sumsize) ]) - left.extend(right) - return left + right.add('All', base, 'all') def _print_result(self, result): import _curses self.win.move(0, 0) for part in result: - self.color('in_statusbar', *part[0]) - self.addstr(part[1]) + self.color(*part.lst) + self.addstr(part.string) + self.color_reset() diff --git a/ranger/gui/widgets/titlebar.py b/ranger/gui/widgets/titlebar.py index 09870f34..69a3b8e0 100644 --- a/ranger/gui/widgets/titlebar.py +++ b/ranger/gui/widgets/titlebar.py @@ -1,8 +1,12 @@ -"""The TitleBar widget displays the current path and some other useful -information.""" +""" +The titlebar is the widget at the top, giving you broad orientation. + +It displays the current path among other things. +""" from . import Widget from math import floor +from ranger.gui.bar import Bar class TitleBar(Widget): old_cf = None @@ -24,7 +28,7 @@ class TitleBar(Widget): self.win.addnstr(self.y, self.wid - 2, self.throbber, 1) def _calc_bar(self): - bar = Bar() + bar = Bar('in_titlebar') self._get_left_part(bar) self._get_right_part(bar) try: @@ -65,123 +69,3 @@ class TitleBar(Widget): self.color(*part.lst) self.addstr(part.string) self.color_reset() - - -class Bar(object): - left = None - right = None - gap = None - - def __init__(self): - self.left = BarSide() - self.right = BarSide() - self.gap = BarSide() - - def add(self, *a, **kw): - self.left.add(*a, **kw) - - def addright(self, *a, **kw): - self.right.add(*a, **kw) - - def sumsize(self): - return self.left.sumsize() + self.right.sumsize() - - def fixedsize(self): - return self.left.fixedsize() + self.right.fixedsize() - - def shrink_by_removing(self, wid): - leftsize = self.left.sumsize() - rightsize = self.right.sumsize() - sumsize = leftsize + rightsize - - # remove elemets from the left until it fits - if sumsize > wid: - while len(self.left) > 0: - leftsize -= len(self.left.pop(-1).string) - if leftsize + rightsize <= wid: - break - sumsize = leftsize + rightsize - - # remove elemets from the right until it fits - if sumsize > wid: - while len(self.right) > 0: - rightsize -= len(self.right.pop(0).string) - if leftsize + rightsize <= wid: - break - sumsize = leftsize + rightsize - - if sumsize < wid: - self.fill_gap(' ', (wid - sumsize), gapwidth=True) - - def shrink_by_cutting(self, wid): - fixedsize = self.fixedsize() - if wid < fixedsize: - raise ValueError("Cannot shrink down to that size by cutting") - - leftsize = self.left.sumsize() - rightsize = self.right.sumsize() - nonfixed_items = self.left.nonfixed_items() - - itemsize = int(float(wid - rightsize - fixedsize) / nonfixed_items) + 1 - - for item in self.left: - if not item.fixed: - item.cut_off_to(itemsize) - - self.fill_gap(' ', wid, gapwidth=False) - - def fill_gap(self, char, wid, gapwidth=False): - del self.gap[:] - - if not gapwidth: - wid = wid - self.sumsize() - - if wid > 0: - self.gap.add(char * wid, 'space') - - def combine(self): - return self.left + self.gap + self.right - - -class BarSide(list): - def add(self, string, *lst, **kw): - cs = ColoredString(string, 'in_titlebar', *lst) - if 'fixedsize' in kw: - cs.fixed = kw['fixedsize'] - self.append(cs) - - def sumsize(self): - return sum(len(item) for item in self) - - def fixedsize(self): - n = 0 - for item in self: - if item.fixed: - n += len(item) - else: - n += 1 - return n - - def nonfixed_items(self): - return sum(1 for item in self if not item.fixed) - - -class ColoredString(object): - fixed = False - - def __init__(self, string, *lst): - self.string = string - self.lst = lst - - def cut_off(self, n): - n = max(n, min(len(self.string), 1)) - self.string = self.string[:-n] - - def cut_off_to(self, n): - self.string = self.string[:n] - - def __len__(self): - return len(self.string) - - def __str__(self): - return self.string -- cgit 1.4.1-2-gfad0