From e680f6a3acb1dd4af0f1d85532116ec7ed7855de Mon Sep 17 00:00:00 2001 From: Brian Dolbec Date: Tue, 25 Jan 2011 00:28:32 -0800 Subject: [PATCH] Add a new cpv and values textwrapping class --- pym/gentoolkit/formatters.py | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/pym/gentoolkit/formatters.py b/pym/gentoolkit/formatters.py index 413a1c8..c0a1747 100644 --- a/pym/gentoolkit/formatters.py +++ b/pym/gentoolkit/formatters.py @@ -97,3 +97,42 @@ def format_timestamp(timestamp): return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(timestamp))) +class CpvValueWrapper(object): + """Format a cpv and linewrap pre-formatted values""" + + def __init__(self, cpv_width=None, width=None): + self.cpv_width = cpv_width + if width is None: + width = gentoolkit.CONFIG['termWidth'] + self.twrap = TextWrapper(width=width) + #self.init_indent = len(self.spacer) + + def _format_values(self, key, values): + """Format entry values ie. USE flags, keywords,... + + @type key: str + @param key: a pre-formatted cpv + @type values: list of pre-formatted strings + @param values: ['flag1', 'flag2',...] + @rtype: str + @return: formatted options string + """ + + result = [] + if self.cpv_width > 1: + _cpv = pp.cpv(key+'.'*(self.cpv_width-len(key))) + if not len(values): + return _cpv + self.twrap.initial_indent = _cpv + self.twrap.subsequent_indent = " " * (self.cpv_width+1) + else: + _cpv = pp.cpv(key+' ') + if not len(values): + return _cpv + self.twrap.initial_indent = _cpv + self.twrap.subsequent_indent = " " * (len(key)+1) + + result.append(self.twrap.fill(values)) + return '\n'.join(result) + + -- 2.26.2