~jkakar/commandant/trunk

15.1.11 by Jamu Kakar
- Add copyright notice to commandant.formatting module.
1
# Commandant is a framework for building command-oriented tools.
40.1.3 by Jamu Kakar
- Updated copyright statement to include 2010 in the list of years.
2
# Copyright (C) 2009-2010 Jamshed Kakar.
15.1.11 by Jamu Kakar
- Add copyright notice to commandant.formatting module.
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with this program; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
22.1.1 by Jamu Kakar
- Refactored help command to show bzrlib-generated help text for
18
"""Infrastructure for pretty-printing formatted output."""
19
43.2.1 by Gavin Panella
Fix lint, as reported by flake8 --max-line-length=80.
20
15.1.8 by Jamu Kakar
- New commandant.formatting module contains a print_columns function
21
def print_columns(outf, rows, shrink_index=None, max_width=78, padding=2):
22
    """Calculate optimal column widths and print C{rows} to C{outf}.
23
24
    @param outf: The stream to write to.
25
    @param rows: A list of rows to print.  Each row is a tuple of columns.
26
        All rows must contain the same number of columns.
27
    @param shrink_index: The index of the column to shrink, if the columns
28
        provided exceed C{max_width}.  Shrinking is disabled by default.
29
    @param max_width: The maximum number of characters per line.  Defaults to
30
        78, though it isn't enforced unless C{shrink_index} is specified.
31
    @param padding: The number of blank characters to output between columns.
32
        Defaults to 2.
33
    """
34
    if not rows:
35
        return
36
37
    widths = []
38
    for row in rows:
39
        if not widths:
40
            widths = [len(column) for i, column in enumerate(row)]
41
        else:
42
            widths = [
43
                max(widths[i], len(column)) for i, column in enumerate(row)]
15.1.11 by Jamu Kakar
- Add copyright notice to commandant.formatting module.
44
15.1.8 by Jamu Kakar
- New commandant.formatting module contains a print_columns function
45
    if shrink_index is not None:
46
        fixed_width = sum(width + padding for i, width in enumerate(widths)
47
                          if i != shrink_index)
48
        if fixed_width + widths[shrink_index] > max_width:
49
            widths[shrink_index] = max_width - fixed_width
50
51
    padding_space = "".ljust(padding)
52
    for row in rows:
53
        output = []
54
        for i, column in enumerate(row):
15.1.9 by Jamu Kakar
- The last column output by print_columns is stripped, to avoid
55
            text = column[:widths[i]].ljust(widths[i])
56
            if (i + 1 == len(row)):
57
                text = text.strip()
58
            output.append(text)
15.1.8 by Jamu Kakar
- New commandant.formatting module contains a print_columns function
59
        print >>outf, padding_space.join(output)