diff options
author | Erik Rose <erik@mozilla.com> | 2011-11-29 00:23:22 -0800 |
---|---|---|
committer | Erik Rose <erik@mozilla.com> | 2011-11-29 00:25:36 -0800 |
commit | cd8fbd9d04db72aa74c33647730c142eda92286e (patch) | |
tree | 3f22c67cb71fe5f04724cf29352ccfba6b448da1 | |
parent | 1636543bdaaa11cc27f6fb06a6787c7145860544 (diff) | |
download | blessings-cd8fbd9d04db72aa74c33647730c142eda92286e.tar.gz |
Make `color(n)` callable to wrap a string, like the named colors can. Bump version to 1.3. Closes #12.
-rw-r--r-- | README.rst | 8 | ||||
-rw-r--r-- | blessings/__init__.py | 48 | ||||
-rw-r--r-- | blessings/tests.py | 14 | ||||
-rw-r--r-- | docs/conf.py | 2 | ||||
-rw-r--r-- | setup.py | 2 |
5 files changed, 63 insertions, 11 deletions
@@ -174,6 +174,9 @@ There is also a numerical interface to colors, which takes integers from 0-15:: term.color(5) + 'Hello' + term.normal term.on_color(3) + 'Hello' + term.normal + term.color(5)('Hello') + term.on_color(3)('Hello') + Compound Formatting ------------------- @@ -330,6 +333,11 @@ Blessings is under the MIT License. See the LICENSE file. Version History =============== +1.3 + * Made ``color(n)`` and ``on_color(n)`` callable to wrap a string, like the + named colors can. Also, make them both fall back to the ``setf`` and ``setb`` + capabilities if the ANSI ``setaf`` and ``setab`` aren't available. + 1.2 * Added support for Python 3! We need 3.2.3 or greater, because the curses library couldn't decide whether to accept strs or bytes before that diff --git a/blessings/__init__.py b/blessings/__init__.py index 8846f9c..500982f 100644 --- a/blessings/__init__.py +++ b/blessings/__init__.py @@ -116,10 +116,6 @@ class Terminal(object): move_x='hpa', move_y='vpa', - # You can use these if you want, but the named equivalents - # like "red" and "on_green" are probably easier. - color='setaf', - on_color='setab', reset_colors='op', # oc doesn't work on my OS X terminal. normal='sgr0', @@ -190,6 +186,28 @@ class Terminal(object): """ return Location(self, x, y) + def color(self, num): + """Return a capability that sets the foreground color. + + :arg num: The number, 0-15, of the color + + The returned string can also be called on another string to wrap it in + the color and set the formatting to normal afterward. + + """ + return self._resolve_numeric_color(num, self._foreground_color) + + def on_color(self, num): + """Return a capability that sets the background color. + + :arg num: The number, 0-15, of the color + + The returned string can also be called on another string to wrap it in + the color and set the formatting to normal afterward. + + """ + return self._resolve_numeric_color(num, self._background_color8) + def _resolve_formatter(self, attr): """Resolve a sugary or plain capability name, color, or compound formatting function name into a callable capability.""" if attr in COLORS: @@ -230,15 +248,27 @@ class Terminal(object): # yellow when a terminal supports setf/setb rather than setaf/setab? # I'll be blasted if I can find any documentation. The following # assumes it does. - color_cap = ((self.setab or self.setb) if 'on_' in color else - (self.setaf or self.setf)) + color_cap = (self._background_color if 'on_' in color else + self._foreground_color) # curses constants go up to only 7, so add an offset to get at the # bright colors at 8-15: offset = 8 if 'bright_' in color else 0 base_color = color.rsplit('_', 1)[-1] - return FormattingString( - color_cap(getattr(curses, 'COLOR_' + base_color.upper()) + offset), - self) + return self._resolve_numeric_color( + getattr(curses, 'COLOR_' + base_color.upper()) + offset, + color_cap) + + def _resolve_numeric_color(self, num, cap): + """Resolve a numeric base color to a ``FormattingString``.""" + return FormattingString(cap(num), self) + + @property + def _foreground_color(self): + return self.setaf or self.setf + + @property + def _background_color(self): + return self.setab or self.setb def derivative_colors(colors): diff --git a/blessings/tests.py b/blessings/tests.py index 8690f4d..83b2ee7 100644 --- a/blessings/tests.py +++ b/blessings/tests.py @@ -134,6 +134,20 @@ def test_mnemonic_colors(): eq_(t.on_bright_green, on_color(10)) +def test_callable_numeric_colors(): + """``color(n)`` should return a formatting wrapper.""" + t = TestTerminal() + eq_(t.color(5)('smoo'), t.color(5) + 'smoo' + t.normal) + eq_(t.on_color(6)('smoo'), t.on_color(6) + 'smoo' + t.normal) + + +def test_null_callable_numeric_colors(): + """``color(n)`` should be a no-op on null terminals.""" + t = TestTerminal(stream=StringIO()) + eq_(t.color(5)('smoo'), 'smoo') + eq_(t.on_color(6)('smoo'), 'smoo') + + def test_formatting_functions(): """Test crazy-ass formatting wrappers, both simple and compound.""" t = TestTerminal() diff --git a/docs/conf.py b/docs/conf.py index 76d70b7..e31ae72 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -50,7 +50,7 @@ copyright = u'2011, Erik Rose' # built documents. # # The short X.Y version. -version = '1.2' +version = '1.3' # The full version, including alpha/beta/rc tags. release = version @@ -9,7 +9,7 @@ if sys.version_info >= (3,): setup( name='blessings', - version='1.2', + version='1.3', description='A thin, practical wrapper around terminal formatting, positioning, and more', long_description=open('README.rst').read(), author='Erik Rose', |