summaryrefslogtreecommitdiffstats
path: root/pym
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2007-12-15 21:32:25 +0000
committerZac Medico <zmedico@gentoo.org>2007-12-15 21:32:25 +0000
commitcc6144473a8ee9e926318c5221993d4c4a9f851e (patch)
tree404fee67ea0a9e453823a24e9255086e9b95229b /pym
parent9b77b3ec5f1cf90e98aef26d2ce8b55afefbbc72 (diff)
downloadportage-cc6144473a8ee9e926318c5221993d4c4a9f851e.tar.gz
portage-cc6144473a8ee9e926318c5221993d4c4a9f851e.tar.bz2
portage-cc6144473a8ee9e926318c5221993d4c4a9f851e.zip
Move the reusable ConsoleStyleFile and StyleWriter classes into
the ouput module. svn path=/main/trunk/; revision=8934
Diffstat (limited to 'pym')
-rw-r--r--pym/portage/output.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/pym/portage/output.py b/pym/portage/output.py
index 9eae2ee87..148fddd0f 100644
--- a/pym/portage/output.py
+++ b/pym/portage/output.py
@@ -6,6 +6,7 @@ __docformat__ = "epytext"
import commands
import errno
+import formatter
import os
import re
import shlex
@@ -314,6 +315,57 @@ def create_color_func(color_key):
for c in compat_functions_colors:
globals()[c] = create_color_func(c)
+class ConsoleStyleFile(object):
+ """
+ A file-like object that behaves something like
+ the colorize() function. Style identifiers
+ passed in via the new_styles() method will be used to
+ apply console codes to output.
+ """
+ def __init__(self, f):
+ self._file = f
+ self._styles = None
+ self.write_listener = None
+
+ def new_styles(self, styles):
+ self._styles = styles
+
+ def write(self, s):
+ if self._styles:
+ for style in self._styles:
+ self._file.write(codes[style])
+ self._file.write(s)
+ self._file.write(codes["reset"])
+ else:
+ self._file.write(s)
+ if self.write_listener:
+ self.write_listener.write(s)
+
+ def writelines(self, lines):
+ for s in lines:
+ self.write(s)
+
+ def flush(self):
+ self._file.flush()
+
+ def close(self):
+ self._file.close()
+
+class StyleWriter(formatter.DumbWriter):
+ """
+ This is just a DumbWriter with a hook in the new_styles() method
+ that passes a styles tuple as a single argument to a callable
+ style_listener attribute.
+ """
+ def __init__(self, **kwargs):
+ formatter.DumbWriter.__init__(self, **kwargs)
+ self.style_listener = None
+
+ def new_styles(self, styles):
+ formatter.DumbWriter.new_styles(self, styles)
+ if self.style_listener:
+ self.style_listener(styles)
+
def get_term_size():
"""
Get the number of lines and columns of the tty that is connected to