Beispiel #1
0
def output(name, status, cols=6):
    def colorize(data):
        status, value = data
        if status == 'GREEN':
            return '%s%s%s' % (COLORS[status], status, COLORS['BLACK'])

        return '%s%s - %s%s' % (COLORS[status], status, value, COLORS['BLACK'])

    num = len(status)

    print name
    print '=' * len(name)

    rows = []
    rows.append(status.keys())
    rows.append(map(colorize, status.values()))

    while num > cols:
        new_rows = [''] * 2
        rows[-1], new_rows[-1] = rows[-1][:cols], rows[-1][cols:]
        rows[-2], new_rows[-2] = rows[-2][:cols], rows[-2][cols:]
        rows.extend(new_rows)

        num -= cols

    while len(rows[-1]) < cols:
        rows[-1].append('')
        rows[-2].append('')

    printer = ColumnPrinter(prefix='= ')
    map(printer.append, rows)
    printer.output()
    def test_ansi(self):
        output = StringIO()
        sys.stdout = output

        printer = ColumnPrinter()
        printer.append(('Test', 'Test'))
        printer.append(('\033[92mTEST\033[0m', '5'))
        printer.output()

        self.assertEquals(
            output.getvalue(),
            'Test    Test    \n\x1b[92mTEST\x1b[0m    5       \n'
        )