diff options
Diffstat (limited to 'blessings/__init__.py')
-rw-r--r-- | blessings/__init__.py | 27 |
1 files changed, 15 insertions, 12 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? |