summaryrefslogtreecommitdiff
path: root/blessings
diff options
context:
space:
mode:
authorjquast <contact@jeffquast.com>2013-11-04 23:19:15 -0800
committerjquast <contact@jeffquast.com>2013-11-04 23:19:15 -0800
commit6df1e39037b36320eab5a90b8d6bdd88091e3598 (patch)
treed6262cb0c9a2814bcf171896723b72a8a8084037 /blessings
parentc7abd20192b9381b464a649898f7bae3b8eb0122 (diff)
downloadblessings-6df1e39037b36320eab5a90b8d6bdd88091e3598.tar.gz
only perform color lookup for colored terminals
Diffstat (limited to 'blessings')
-rw-r--r--blessings/__init__.py2
-rw-r--r--blessings/tests.py24
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():