def format_line(line, first="", subsequent="", force_quiet=False): """Wrap a string at word boundaries and optionally indent the first line and/or subsequent lines with custom strings. Preserve newlines if the longest line is not longer than CONFIG['termWidth']. To force the preservation of newlines and indents, split the string into a list and feed it to format_line via format_list. @see: format_list() @type line: string @param line: text to format @type first: string @param first: text to prepend to the first line @type subsequent: string @param subsequent: text to prepend to subsequent lines @type force_quiet: boolean @rtype: string @return: A wrapped line """ if line: line = line.expandtabs().strip("\n").splitlines() else: if force_quiet: return else: return first + "None specified" if len(first) > len(subsequent): wider_indent = first else: wider_indent = subsequent widest_line_len = len(max(line, key=len)) + len(wider_indent) if widest_line_len > CONFIG['termWidth']: twrap = TextWrapper(width=CONFIG['termWidth'], expand_tabs=False, initial_indent=first, subsequent_indent=subsequent) line = " ".join(line) line = re.sub("\s+", " ", line) line = line.lstrip() result = twrap.fill(line) else: # line will fit inside CONFIG['termWidth'], so preserve whitespace and # newlines line[0] = first + line[0] # Avoid two newlines if len == 1 if len(line) > 1: line[0] = line[0] + "\n" for i in range(1, (len(line[1:-1]) + 1)): line[i] = subsequent + line[i] + "\n" line[-1] = subsequent + line[-1] # Avoid two newlines on last line if line[-1].isspace(): del line[-1] # Avoid trailing blank lines result = "".join(line) return result
def format_options_respect_newline(options, indent_c=25): """Like format_options, but respects newlines.""" wrapper = TextWrapper(width=gentoolkit.CONFIG['termWidth']) result = [] opts, descs = zip(*options) for opt, desc in zip(opts, descs): wrapper.initial_indent = gentoolkit.pprinter.emph(opt.ljust(indent_c)) wrapper.subsequent_indent = " " * indent_c for line in desc.splitlines(): result.append(wrapper.fill(line)) wrapper.initial_indent = ''.ljust(indent_c) return '\n'.join(result)
def format_options(options): """Format module options. @type options: list @param options: [('option 1', 'description 1'), ('option 2', 'des... )] @rtype: str @return: formatted options string """ result = [] twrap = TextWrapper(width=gentoolkit.CONFIG["termWidth"]) opts = (x[0] for x in options) descs = (x[1] for x in options) for opt, desc in zip(opts, descs): twrap.initial_indent = pp.emph(opt.ljust(25)) twrap.subsequent_indent = " " * 25 result.append(twrap.fill(desc)) return "\n".join(result)
def format_options(options): """Format module options. @type options: list @param options: [('option 1', 'description 1'), ('option 2', 'des... )] @rtype: str @return: formatted options string """ result = [] twrap = TextWrapper(width=gentoolkit.CONFIG['termWidth']) opts = (x[0] for x in options) descs = (x[1] for x in options) for opt, desc in zip(opts, descs): twrap.initial_indent = pp.emph(opt.ljust(25)) twrap.subsequent_indent = " " * 25 result.append(twrap.fill(desc)) return '\n'.join(result)
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)
class CpvValueWrapper: """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)
def display_useflags(output): """Print USE flag descriptions and statuses. @type output: list @param output: [(inuse, inused, flag, desc, restrict), ...] inuse (int) = 0 or 1; if 1, flag is set in make.conf inused (int) = 0 or 1; if 1, package is installed with flag enabled flag (str) = the name of the USE flag desc (str) = the flag's description restrict (str) = corresponds to the text of restrict in metadata """ maxflag_len = len(max([t[2] for t in output], key=len)) twrap = TextWrapper() twrap.width = CONFIG['termWidth'] twrap.subsequent_indent = " " * (maxflag_len + 8) markers = ("-", "+") color = ( partial(pp.useflag, enabled=False), partial(pp.useflag, enabled=True) ) for in_makeconf, in_installed, flag, desc, restrict in output: if CONFIG['verbose']: flag_name = "" if in_makeconf != in_installed: flag_name += pp.emph(" %s %s" % (markers[in_makeconf], markers[in_installed])) else: flag_name += (" %s %s" % (markers[in_makeconf], markers[in_installed])) flag_name += " " + color[in_makeconf](flag.ljust(maxflag_len)) flag_name += " : " # Strip initial whitespace at the start of the description # Bug 432530 if desc: desc = desc.lstrip() # print description if restrict: restrict = "(%s %s)" % (pp.emph("Restricted to"), pp.cpv(restrict)) twrap.initial_indent = flag_name pp.uprint(twrap.fill(restrict)) if desc: twrap.initial_indent = twrap.subsequent_indent pp.uprint(twrap.fill(desc)) else: print(" : <unknown>") else: if desc: twrap.initial_indent = flag_name desc = twrap.fill(desc) pp.uprint(desc) else: twrap.initial_indent = flag_name print(twrap.fill("<unknown>")) else: pp.uprint(markers[in_makeconf] + flag)
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)
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)