From 6df1e39037b36320eab5a90b8d6bdd88091e3598 Mon Sep 17 00:00:00 2001 From: jquast Date: Mon, 4 Nov 2013 23:19:15 -0800 Subject: only perform color lookup for colored terminals --- blessings/__init__.py | 2 ++ blessings/tests.py | 24 ++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/blessings/__init__.py b/blessings/__init__.py index 9d37889..ca6a035 100644 --- a/blessings/__init__.py +++ b/blessings/__init__.py @@ -385,6 +385,8 @@ class Terminal(object): # bright colors at 8-15: offset = 8 if 'bright_' in color else 0 base_color = color.rsplit('_', 1)[-1] + if self.number_of_colors == 0: + return NullCallableString() return self._formatting_string( color_cap(getattr(curses, 'COLOR_' + base_color.upper()) + offset)) diff --git a/blessings/tests.py b/blessings/tests.py index 7dda746..a61b73e 100644 --- a/blessings/tests.py +++ b/blessings/tests.py @@ -137,23 +137,23 @@ def test_null_fileno(): def test_mnemonic_colors(): """Make sure color shortcuts work.""" - def color(num): - return unicode_parm('setaf', num) + def color(t, num): + return t.number_of_colors and unicode_parm('setaf', num) or '' - def on_color(num): - return unicode_parm('setab', num) + def on_color(t, num): + return t.number_of_colors and unicode_parm('setab', num) or '' # Avoid testing red, blue, yellow, and cyan, since they might someday # change depending on terminal type. t = TestTerminal() - eq_(t.white, color(7)) - eq_(t.green, color(2)) # Make sure it's different than white. - eq_(t.on_black, on_color(0)) - eq_(t.on_green, on_color(2)) - eq_(t.bright_black, color(8)) - eq_(t.bright_green, color(10)) - eq_(t.on_bright_black, on_color(8)) - eq_(t.on_bright_green, on_color(10)) + eq_(t.white, color(t, 7)) + eq_(t.green, color(t, 2)) # Make sure it's different than white. + eq_(t.on_black, on_color(t, 0)) + eq_(t.on_green, on_color(t, 2)) + eq_(t.bright_black, color(t, 8)) + eq_(t.bright_green, color(t, 10)) + eq_(t.on_bright_black, on_color(t, 8)) + eq_(t.on_bright_green, on_color(t, 10)) def test_callable_numeric_colors(): -- cgit v1.2.1