Mercurial > p > roundup > code
diff roundup/support.py @ 5043:ac0c117cd24c
Remove wrap and associated functions from roundup.support
The wrap function duplicates functionality that is already provided by
the inbuilt textwrap module since python v2.3. The wrap function is only
used in one place so lets just replace it with the equivalent in-built
function.
| author | John Kristensen <john@jerrykan.com> |
|---|---|
| date | Thu, 12 Feb 2015 00:55:00 +1100 |
| parents | 0a05c4d9a221 |
| children | 64b05e24dbd8 |
line wrap: on
line diff
--- a/roundup/support.py Sun Jan 17 22:12:48 2016 +1100 +++ b/roundup/support.py Thu Feb 12 00:55:00 2015 +1100 @@ -118,97 +118,4 @@ sys.stdout.write(s + ' '*(75-len(s)) + '\r') sys.stdout.flush() -LEFT = 'left' -LEFTN = 'left no strip' -RIGHT = 'right' -CENTER = 'center' - -def align(line, width=70, alignment=LEFTN): - ''' Code from http://www.faqts.com/knowledge_base/view.phtml/aid/4476 ''' - if alignment == CENTER: - line = line.strip() - space = width - len(line) - return ' '*(space/2) + line + ' '*(space/2 + space%2) - elif alignment == RIGHT: - line = line.rstrip() - space = width - len(line) - return ' '*space + line - else: - if alignment == LEFT: - line = line.lstrip() - space = width - len(line) - return line + ' '*space - - -def format_line(columns, positions, contents, spacer=' | ', - collapse_whitespace=True, wsre=re.compile(r'\s+')): - ''' Fill up a single row with data from the contents ''' - l = [] - data = 0 - for i in range(len(columns)): - width, alignment = columns[i] - content = contents[i] - col = '' - while positions[i] < len(content): - word = content[positions[i]] - # if we hit a newline, honor it - if '\n' in word: - # chomp - positions[i] += 1 - break - - # make sure this word fits - if col and len(word) + len(col) > width: - break - - # no whitespace at start-of-line - if collapse_whitespace and wsre.match(word) and not col: - # chomp - positions[i] += 1 - continue - - col += word - # chomp - positions[i] += 1 - if col: - data = 1 - col = align(col, width, alignment) - l.append(col) - - if not data: - return '' - return spacer.join(l).rstrip() - - -def format_columns(columns, contents, spacer=' | ', collapse_whitespace=True, - splitre=re.compile(r'(\n|\r\n|\r|[ \t]+|\S+)')): - ''' Format the contents into columns, with 'spacing' between the - columns - ''' - assert len(columns) == len(contents), \ - 'columns and contents must be same length' - - # split the text into words, spaces/tabs and newlines - for i in range(len(contents)): - contents[i] = splitre.findall(contents[i]) - - # now process line by line - l = [] - positions = [0]*len(contents) - while 1: - l.append(format_line(columns, positions, contents, spacer, - collapse_whitespace)) - - # are we done? - for i in range(len(contents)): - if positions[i] < len(contents[i]): - break - else: - break - return '\n'.join(l) - -def wrap(text, width=75, alignment=LEFTN): - return format_columns(((width, alignment),), [text], - collapse_whitespace=False) - # vim: set et sts=4 sw=4 :
