From 34c131ef561ffb22586e2094fc0c478a44f24ac6 Mon Sep 17 00:00:00 2001 From: hut Date: Sun, 28 Mar 2010 20:28:42 +0200 Subject: new option: colorscheme_overlay For those who want small colorscheme changes without creating an extra file. --- TODO | 1 + ranger/api/options.py | 1 + ranger/defaults/options.py | 19 +++++++++++++++++++ ranger/gui/colorscheme.py | 12 ++++++++++++ ranger/shared/settings.py | 5 +++++ 5 files changed, 38 insertions(+) diff --git a/TODO b/TODO index 464f8ffb..2f8578cc 100644 --- a/TODO +++ b/TODO @@ -89,4 +89,5 @@ Ideas ( ) #72 10/03/21 ranger daemon which does the slow io tasks ( ) #75 10/03/28 navigate in history ( ) #76 10/03/28 save history between sessions + (X) #77 10/03/28 colorscheme overlay in options.py diff --git a/ranger/api/options.py b/ranger/api/options.py index 7ead8c90..4748823d 100644 --- a/ranger/api/options.py +++ b/ranger/api/options.py @@ -16,6 +16,7 @@ import re from re import compile as regexp from ranger import colorschemes as allschemes +from ranger.gui import color class AttrToString(object): """ diff --git a/ranger/defaults/options.py b/ranger/defaults/options.py index ace68592..ce010195 100644 --- a/ranger/defaults/options.py +++ b/ranger/defaults/options.py @@ -85,3 +85,22 @@ sort_reverse = False sort_case_insensitive = False sort_directories_first = True + +# Apply an overlay function to the colorscheme. It will be called with +# 4 arguments: the context and the 3 values (fg, bg, attr) returned by +# the original use() function of your colorscheme. The return value +# must be a 3-tuple of (fg, bg, attr). +# Note: Here, the colors/attributes aren't directly imported into +# the namespace but have to be accessed with color.xyz. +def colorscheme_overlay(context, fg, bg, attr): + if context.directory and attr & color.bold and \ + not any((context.marked, context.selected)): + attr ^= color.bold # I don't like bold directories! + + if context.main_column and context.selected: + fg, bg = color.red, color.default # To highlight the main column! + + return fg, bg, attr + +# The above function was just an example, let's set it back to None +colorscheme_overlay = None diff --git a/ranger/gui/colorscheme.py b/ranger/gui/colorscheme.py index 199a5523..b76131ae 100644 --- a/ranger/gui/colorscheme.py +++ b/ranger/gui/colorscheme.py @@ -45,6 +45,10 @@ from curses import color_pair from ranger.gui.color import get_color from ranger.gui.context import Context +# ColorScheme is not SettingsAware but it will gain access +# to the settings during the initialization. We can't import +# SettingsAware here because of circular imports. + class ColorScheme(object): """ This is the class that colorschemes must inherit from. @@ -73,6 +77,14 @@ class ColorScheme(object): # add custom error messages for broken colorschemes color = self.use(context) + if self.settings.colorscheme_overlay: + result = self.settings.colorscheme_overlay(context, *color) + assert isinstance(result, (tuple, list)), \ + "Your colorscheme overlay doesn't return a tuple!" + assert all(isinstance(val, int) for val in result), \ + "Your colorscheme overlay doesn't return a tuple"\ + " containing 3 integers!" + color = result self.cache[keys] = color return color diff --git a/ranger/shared/settings.py b/ranger/shared/settings.py index e53f31b0..63e9d4e4 100644 --- a/ranger/shared/settings.py +++ b/ranger/shared/settings.py @@ -40,6 +40,7 @@ ALLOWED_SETTINGS = { 'preview_directories': bool, 'flushinput': bool, 'colorscheme': str, + 'colorscheme_overlay': (type(None), type(lambda:0)), 'hidden_filter': lambda x: isinstance(x, str) or hasattr(x, 'match'), } @@ -136,6 +137,10 @@ class SettingsAware(object): raise Exception("The module contains no " \ "valid colorscheme!") + # Making the colorscheme SettingsAware doesn't work because + # of circular imports, so we do it like this: + settings.colorscheme.settings = settings + try: import apps except ImportError: -- cgit 1.4.1-2-gfad0