summaryrefslogtreecommitdiff
path: root/blessings
diff options
context:
space:
mode:
authorjquast <contact@jeffquast.com>2013-11-04 23:22:19 -0800
committerjquast <contact@jeffquast.com>2013-11-04 23:22:19 -0800
commit6d8fe019be1e1c1bd1238980288e1451d99eba80 (patch)
tree00fff24582347d1f4b2b6c220ff2e33b1c10478e /blessings
parent4cff579fbf395e7cfe3d12cb1b5f53ca535d6673 (diff)
parent6df1e39037b36320eab5a90b8d6bdd88091e3598 (diff)
downloadblessings-6d8fe019be1e1c1bd1238980288e1451d99eba80.tar.gz
Merge remote-tracking branch 'origin/bugfix-3' into bugfix-1
Diffstat (limited to 'blessings')
-rw-r--r--blessings/__init__.py27
-rw-r--r--blessings/tests.py24
2 files changed, 27 insertions, 24 deletions
diff --git a/blessings/__init__.py b/blessings/__init__.py
index bee547e..f7c1659 100644
--- a/blessings/__init__.py
+++ b/blessings/__init__.py
@@ -302,7 +302,8 @@ class Terminal(object):
:arg num: The number, 0-15, of the color
"""
- return ParametrizingString(self._foreground_color, self.normal)
+ return (ParametrizingString(self._foreground_color, self.normal)
+ if self.does_styling else NullCallableString())
@property
def on_color(self):
@@ -311,7 +312,8 @@ class Terminal(object):
See ``color()``.
"""
- return ParametrizingString(self._background_color, self.normal)
+ return (ParametrizingString(self._background_color, self.normal)
+ if self.does_styling else NullCallableString())
@property
def number_of_colors(self):
@@ -333,11 +335,11 @@ class Terminal(object):
# don't name it after the underlying capability, because we deviate
# slightly from its behavior, and we might someday wish to give direct
# access to it.
- colors = tigetnum('colors') # Returns -1 if no color support, -2 if no
- # such cap.
+ # Returns -1 if no color support, -2 if no such capability.
+ colors = self.does_styling and tigetnum('colors') or -1
# self.__dict__['colors'] = ret # Cache it. It's not changing.
# (Doesn't work.)
- return colors if colors >= 0 else 0
+ return max(0, colors)
def _resolve_formatter(self, attr):
"""Resolve a sugary or plain capability name, color, or compound
@@ -391,6 +393,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))
@@ -447,12 +451,6 @@ class ParametrizingString(unicode):
parametrized = tparm(self.encode('utf-8'), *args).decode('utf-8')
return (parametrized if self._normal is None else
FormattingString(parametrized, self._normal))
- except curses.error:
- # Catch "must call (at least) setupterm() first" errors, as when
- # running simply `nosetests` (without progressive) on nose-
- # progressive. Perhaps the terminal has gone away between calling
- # tigetstr and calling tparm.
- return u''
except TypeError:
# If the first non-int (i.e. incorrect) arg was a string, suggest
# something intelligent:
@@ -526,7 +524,12 @@ class NullCallableString(unicode):
# determine which of 2 special-purpose classes,
# NullParametrizableString or NullFormattingString, to return, and
# retire this one.
- return u''
+ # As a NullCallableString, even when provided with a parameter,
+ # such as t.color(5), we must also still be callable, fe:
+ # >>> t.color(5)('shmoo')
+ # is actually simplified result of NullCallable()(), so
+ # turtles all the way down: we return another instance.
+ return NullCallableString()
return args[0] # Should we force even strs in Python 2.x to be
# unicodes? No. How would I know what encoding to use
# to convert it?
diff --git a/blessings/tests.py b/blessings/tests.py
index 168c396..ee9c3c8 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():