Skip to content

Commit

Permalink
Fix Formatter.columns not taking indent into account.
Browse files Browse the repository at this point in the history
  • Loading branch information
epsy committed Jan 23, 2015
1 parent ff12381 commit aaff1b6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
13 changes: 6 additions & 7 deletions clize/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,16 @@ def show_full_usage(self, name):

def show_arguments(self):
f = util.Formatter()
with f.columns() as cols:
with f.columns(indent=2) as cols:
for label, section in self.sections.items():
if not section: continue
f.new_paragraph()
f.append(label)
with f.indent():
for argname, (param, text) in section.items():
self.show_argument(
param,
text, self.after.get(argname, ()),
f, cols)
for argname, (param, text) in section.items():
self.show_argument(
param,
text, self.after.get(argname, ()),
f, cols)
return f

def show_argument(self, param, desc, after, f, cols):
Expand Down
15 changes: 15 additions & 0 deletions clize/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,18 @@ def columns_nowrap_multiline(self, f):
with f.columns() as cols:
cols.append('row1col1 is long', 'word word word')
cols.append('col1', 'word word word word')


@equal(' lll1 c c c c c c c c c c c c c c c c c\n'
' c c c c c\n'
' lll2 c c c c c c c c c c c c c c c c c\n'
' c c c c c'
)
def columns_wrap_indented(self, f):
with f.indent(10):
with f.columns() as cols:
cols.append('lll1',
'c c c c c c c c c c c c c c c c c c c c c c')
with f.indent(10):
cols.append('lll2',
'c c c c c c c c c c c c c c c c c c c c c c')
16 changes: 10 additions & 6 deletions clize/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ def process_widths(widths, max_width):

class _FormatterColumns(object):
def __init__(self, formatter, num, spacing, align,
wrap, min_widths, max_widths):
wrap, min_widths, max_widths, indent):
self.formatter = formatter
self.indent = indent
self.num = num
self.spacing = spacing
self.align = align or '<' * num
Expand All @@ -156,14 +157,14 @@ def append(self, *cells):
self.num, len(cells)))
row = _FormatterRow(self, cells)
self.rows.append(row)
self.formatter.append_raw(row)
self.formatter.append_raw(row, -self.formatter._indent)

def __exit__(self, exc_type, exc_val, exc_tb):
self.finished = True
self.widths = list(self.compute_widths())

def compute_widths(self):
used = len(self.spacing) * (self.num - 1)
used = len(self.spacing) * (self.num - 1) + self.indent
space_left = self.formatter.max_width - used
min_widths = list(process_widths(self.min_widths, space_left))
max_widths = list(process_widths(self.max_widths, space_left))
Expand All @@ -186,7 +187,8 @@ def compute_widths(self):

def format_cells(self, cells):
wcells = (self.format_cell(*args) for args in enumerate(cells))
return (self.spacing.join(cline).rstrip()
indent = ' ' * self.indent
return (indent + self.spacing.join(cline).rstrip()
for cells in zip_longest(*wcells)
for cline in self.match_lines(cells)
)
Expand Down Expand Up @@ -278,10 +280,12 @@ def indent(self, indent=2):
return _FormatterIndent(self, indent)

def columns(self, num=2, spacing=' ', align=None,
wrap=None, min_widths=None, max_widths=None):
wrap=None, min_widths=None, max_widths=None,
indent=None):
return _FormatterColumns(
self, num, spacing, align,
wrap, min_widths, max_widths)
wrap, min_widths, max_widths,
self._indent if indent is None else indent)

def __str__(self):
if self.lines and not self.lines[-1][1]:
Expand Down

0 comments on commit aaff1b6

Please sign in to comment.