From f027adc08ce0d15717c7694956f23ff637553543 Mon Sep 17 00:00:00 2001 From: hut Date: Sun, 29 Nov 2009 18:58:13 +0100 Subject: implemented colorschemes. --- ranger.py | 20 ++++++------- ranger/conf/colorschemes/snow.py | 35 +++++++++++++++++++++- ranger/conf/options.py | 2 +- ranger/gui/color.py | 33 +++++++++++++++++++++ ranger/gui/colorscheme.py | 64 ++++++++++++++-------------------------- ranger/gui/ui.py | 2 +- ranger/gui/wdisplay.py | 53 +++++++++++++++++---------------- ranger/gui/widget.py | 27 +++++++++++++---- 8 files changed, 151 insertions(+), 85 deletions(-) create mode 100644 ranger/gui/color.py diff --git a/ranger.py b/ranger.py index b44fad46..d4c4a961 100755 --- a/ranger.py +++ b/ranger.py @@ -15,6 +15,7 @@ else fi return 1 """ + from ranger.fm import FM from ranger.environment import Environment from ranger.command import CommandList @@ -53,16 +54,15 @@ try: my_fm.feed(path, my_ui) my_fm.run() -#except BaseException as original_error: -# try: my_ui.exit() -# except: pass -# -# raise original_error - finally: - try: my_ui.exit() - except: pass + try: + my_ui.exit() + except: + pass + if cd_after_exit: - try: sys.__stderr__.write(env.pwd.path) - except: pass + try: + sys.__stderr__.write(env.pwd.path) + except: + pass diff --git a/ranger/conf/colorschemes/snow.py b/ranger/conf/colorschemes/snow.py index 52f5dbb4..40909c9d 100644 --- a/ranger/conf/colorschemes/snow.py +++ b/ranger/conf/colorschemes/snow.py @@ -1,5 +1,38 @@ from ranger.gui.colorscheme import ColorScheme +from ranger.gui.color import * class Snow(ColorScheme): def use(self, context): - return 1, 0, 0 + if context.reset: + return default_colors + + if context.wdisplay: + fg, bg = default, default + + if context.selected: + attr = reverse + else: + attr = normal + + if context.empty: + bg = red + + if context.directory: + fg = blue + + if context.executable: + fg = green + + if context.media: + fg = pink + + if context.link: + fg = cyan + + if context.maindisplay and context.selected: + attr = attr | bold + fg = yellow + + return fg, bg, attr + + return default_colors diff --git a/ranger/conf/options.py b/ranger/conf/options.py index 92e38c8d..11ed80cc 100644 --- a/ranger/conf/options.py +++ b/ranger/conf/options.py @@ -4,4 +4,4 @@ def get(): def dummy(): """ provide a way of getting options until get() is implemented """ - return { 'show_hidden': False, 'scroll_offset': 2, 'directories_first': True, 'preview_files' : True } + return { 'show_hidden': False, 'scroll_offset': 2, 'directories_first': True, 'preview_files' : False } diff --git a/ranger/gui/color.py b/ranger/gui/color.py new file mode 100644 index 00000000..214383ee --- /dev/null +++ b/ranger/gui/color.py @@ -0,0 +1,33 @@ +import curses + +COLOR_PAIRS = {10: 0} + +def get_color(fg, bg): + import curses + + c = bg+2 + 9*(fg + 2) + + if c not in COLOR_PAIRS: + size = len(COLOR_PAIRS) + curses.init_pair(size, fg, bg) + COLOR_PAIRS[c] = size + + return COLOR_PAIRS[c] + +black = curses.COLOR_BLACK +blue = curses.COLOR_BLUE +cyan = curses.COLOR_CYAN +green = curses.COLOR_GREEN +magenta = curses.COLOR_MAGENTA +red = curses.COLOR_RED +white = curses.COLOR_WHITE +yellow = curses.COLOR_YELLOW +default = -1 + +normal = curses.A_NORMAL +bold = curses.A_BOLD +reverse = curses.A_REVERSE +underline = curses.A_UNDERLINE +invisible = curses.A_INVIS + +default_colors = (default, default, normal) diff --git a/ranger/gui/colorscheme.py b/ranger/gui/colorscheme.py index f410983f..ff3f9243 100644 --- a/ranger/gui/colorscheme.py +++ b/ranger/gui/colorscheme.py @@ -1,54 +1,34 @@ +CONTEXT_KEYS = ['reset', 'wdisplay', 'wstatusbar', 'wtitlebar', 'wconsole', 'directory', 'file', 'maindisplay', 'executable', 'media', 'link', 'broken', 'selected', 'empty'] -TUPLE_KEYS = ['wdisplay', 'wstatusbar', 'wtitlebar', 'wconsole', 'folder', 'executable', 'media', 'link', 'broken', 'selected'] +class ColorSchemeContext(): + pass -COLOR_PAIRS = {10: 0} - -def get_color(fg, bg): - import curses - - c = bg+2 + 9*(fg + 2) - - if c not in COLOR_PAIRS: - size = len(COLOR_PAIRS) - curses.init_pair(size, fg, bg) - COLOR_PAIRS[c] = size - - return COLOR_PAIRS[c] +class ColorScheme(): + def __init__(self): + self.cache = {} + def get(self, *keys): + try: + return self.cache[keys] -class ColorSchemeContext(object): - def __init__(self, dictionary): - object.__init__(self) + except KeyError: + context = ColorSchemeContext() - self.__tuple__ = None - for key in TUPLE_KEYS: - if key in dictionary: - self.__dict__[key] = dictionary[key] - else: - self.__dict__[key] = False + for key in CONTEXT_KEYS: + context.__dict__[key] = (key in keys) - def to_tuple(self): - d = self.__dict__ + color = self.use(context) + self.cache[keys] = color + return color - if '__tuple__' is None: - self.__tuple__ = tuple(d[key] for key in TUPLE_KEYS) + def get_attr(self, *keys): + from ranger.gui.color import get_color + import curses - return self.__tuple__ + fg, bg, attr = self.get(*keys) + return attr | curses.color_pair(get_color(fg, bg)) -class ColorScheme(): - def __init__(self): - self.cache = {} - def get(self, **keywords): - con = ColorSchemeContext(keywords) - tup = con.to_tuple() - try: - return self.cache[tup] - except KeyError: - color = self.use(con) - self.cache[tup] = color - return color - def use(self, context): return -1, -1, 0 - + diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py index 6bcfc7fe..a54003dc 100644 --- a/ranger/gui/ui.py +++ b/ranger/gui/ui.py @@ -17,7 +17,7 @@ class UI(): def initialize(self): curses.noecho() - curses.halfdelay(10) + curses.halfdelay(20) curses.curs_set(0) curses.start_color() curses.use_default_colors() diff --git a/ranger/gui/wdisplay.py b/ranger/gui/wdisplay.py index d8d70bf3..f021279b 100644 --- a/ranger/gui/wdisplay.py +++ b/ranger/gui/wdisplay.py @@ -1,6 +1,4 @@ -from ranger.gui.color import color_pairs from ranger.gui.widget import Widget as SuperClass -import curses class WDisplay(SuperClass): def __init__(self, win, colorscheme, level): @@ -45,64 +43,69 @@ class WDisplay(SuperClass): except: pass - else: - self.win.addnstr(self.y, self.x, "this is a file.", self.wid) - def draw_directory(self): from ranger.directory import Directory + import curses + self.target.show_hidden = self.show_hidden self.target.load_content_if_outdated() self.target.directories_first = self.directories_first self.target.sort_if_outdated() - main_display = self.main_display + + base_color = ['wdisplay'] + + if self.main_display: + base_color.append('maindisplay') if self.target.empty(): - self.color(bg=1) + self.color(base_color, 'empty') self.win.addnstr(self.y, self.x, "empty", self.wid) - self.color() + self.color_reset() return if not self.target.accessible: + self.color(base_color, 'error') self.win.addnstr(self.y, self.x, "not accessible", self.wid) + self.color_reset() return self.set_scroll_begin() selected_i = self.target.pointed_index for line in range(self.hei): - i = line + self.scroll_begin # last file reached? + i = line + self.scroll_begin try: drawed = self.target[i] except IndexError: break + this_color = base_color[:] + + if i == selected_i: + this_color.append('selected') + if isinstance(drawed, Directory): - self.color(fg = 4) + this_color.append('directory') else: - self.color() + this_color.append('file') - invert = i == selected_i - if invert: - self.win.attron(curses.A_REVERSE) + if drawed.islink: + this_color.append('link') + string = drawed.basename if self.main_display: - self.win.addnstr( - self.y + line, - self.x + 1, - ' ' + drawed.basename + ' ', - self.wid - 2) + self.win.addnstr(self.y + line, self.x+1, drawed.basename, self.wid-2) else: - self.win.addnstr( - self.y + line, - self.x, - drawed.basename, - self.wid) + self.win.addnstr(self.y + line, self.x, drawed.basename, self.wid) if self.display_infostring and drawed.infostring: info = drawed.infostring x = self.x + self.wid - 1 - len(info) if x > self.x: self.win.addstr(self.y + line, x, str(info) + ' ') - self.win.attrset(0) + + self.color_at(self.y + line, self.x, self.wid, this_color) + + self.color_reset() def get_scroll_begin(self): offset = self.scroll_offset diff --git a/ranger/gui/widget.py b/ranger/gui/widget.py index 9c64cc1a..cb78062b 100644 --- a/ranger/gui/widget.py +++ b/ranger/gui/widget.py @@ -1,7 +1,12 @@ -import curses -from ranger.gui.colorscheme import get_color -class OutOfBoundsException(Exception): pass +class OutOfBoundsException(Exception): + pass + +def combine(keylist, keys): + if type(keylist) in (list, tuple): + return tuple(tuple(keylist) + keys) + else: + return tuple((keylist, ) + keys) class Widget(): def __init__(self, win, colorscheme): @@ -9,17 +14,29 @@ class Widget(): self.colorscheme = colorscheme self.setdim(0, 0, 0, 0) - def color(self, fg = -1, bg = -1, attr = 0): - self.win.attrset(attr | curses.color_pair(get_color(fg, bg))) + def color(self, keylist = None, *keys): + keys = combine(keylist, keys) + self.win.attrset(self.colorscheme.get_attr(*keys)) + + def color_at(self, y, x, wid, keylist = None, *keys): + keys = combine(keylist, keys) + self.win.chgat(y, x, wid, self.colorscheme.get_attr(*keys)) + + def color_reset(self): + Widget.color(self, 'reset') def setdim(self, y, x, hei=None, wid=None): maxy, maxx = self.win.getmaxyx() + wid = wid or maxx - x hei = hei or maxy - y + if x + wid > maxx and y + hei > maxy: raise OutOfBoundsException("X and Y out of bounds!") + if x + wid > maxx: raise OutOfBoundsException("X out of bounds!") + if y + hei > maxy: raise OutOfBoundsException("Y out of bounds!") -- cgit 1.4.1-2-gfad0