summaryrefslogtreecommitdiff
path: root/blessings/__init__.py
diff options
context:
space:
mode:
authorErik Rose <erik@mozilla.com>2011-12-09 19:32:47 -0500
committerErik Rose <erik@mozilla.com>2011-12-09 19:32:47 -0500
commit8151e6fe390046a41a2153f4004baec8f20f35d4 (patch)
tree731c34fe17c3d6bf3ef1638db2f94e6ab586d1b4 /blessings/__init__.py
parenta06790b3b9e017d08ea194b3737be7a2c864592c (diff)
downloadblessings-8151e6fe390046a41a2153f4004baec8f20f35d4.tar.gz
Add number_of_colors attr. Bring release notes up to date.
Diffstat (limited to 'blessings/__init__.py')
-rw-r--r--blessings/__init__.py26
1 files changed, 25 insertions, 1 deletions
diff --git a/blessings/__init__.py b/blessings/__init__.py
index f46ee66..7ebd8ad 100644
--- a/blessings/__init__.py
+++ b/blessings/__init__.py
@@ -1,6 +1,6 @@
from collections import defaultdict
import curses
-from curses import tigetstr, setupterm, tparm
+from curses import tigetstr, tigetnum, setupterm, tparm
from fcntl import ioctl
try:
from io import UnsupportedOperation as IOUnsupportedOperation
@@ -208,6 +208,30 @@ class Terminal(object):
"""
return ParametrizingString(self._background_color, self.normal)
+ @property
+ def number_of_colors(self):
+ """Return the number of colors the terminal supports.
+
+ Common values are 0, 8, 16, 88, and 256.
+
+ Though the underlying capability returns -1 when there is no color
+ support, we return 0. This lets you test more Pythonically::
+
+ if term.number_of_colors:
+ ...
+
+ We also return 0 if the terminal won't tell us how many colors it
+ supports, which I think is rare.
+
+ """
+ # This is actually the only remotely useful numeric capability. We
+ # 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.
+ #self.__dict__['colors'] = ret # Cache it. It's not changing. (Doesn't work.)
+ return colors if colors >= 0 else 0
+
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: