source: rtems-tools/rtemstoolkit/textbox.py @ 925448a

Last change on this file since 925448a was 925448a, checked in by Chris Johns <chrisj@…>, on May 5, 2019 at 12:46:46 AM

rtemstoolkit: columns are only ints.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2017 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-tools'.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are met:
10#
11# 1. Redistributions of source code must retain the above copyright notice,
12# this list of conditions and the following disclaimer.
13#
14# 2. Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31#
32# Manage paths locally. The internally the path is in Unix or shell format and
33# we convert to the native format when performing operations at the Python
34# level. This allows macro expansion to work.
35#
36
37from __future__ import print_function
38
39import copy
40import os
41
42from rtemstoolkit import error
43
44def line(cols, line = '-', marker = '|', indent = 0, linesep = os.linesep):
45    s = ' ' * indent + marker
46    for c in cols:
47        s += line[0] * int((c - 1)) + marker
48    return s + linesep
49
50def row(cols, data, indent = 0, marker = '|', linesep = os.linesep):
51    if len(cols) != len(data):
52        raise error.internal('data size (%d) does not' \
53                             ' match columns (%d)' % (len(data), len(cols)))
54    s = ' ' * indent + '|'
55    for c in range(0, len(cols)):
56        if c < len(cols) - 1:
57            m = marker
58        else:
59            m = '|'
60        s += '%-*s%s' % (int(cols[c] - 1), str(data[c]), m)
61    return s + linesep
62
63def even_columns(cols, width = 80):
64    per_col = int(width / cols)
65    columns = [per_col for c in range(0, cols)]
66    for remainder in range(0, int(width - (per_col * cols))):
67        if remainder % 2 == 0:
68            columns[remainder] += 1
69        else:
70            columns[len(columns) - remainder] += 1
71    return columns
72
73def merge_columns(columns):
74    columns = copy.deepcopy(columns)
75    cols = []
76    while True:
77        empty = True
78        for c in columns:
79            if len(c) != 0:
80                empty = False
81                break
82        if empty:
83            break
84        lowest = 0
85        lowest_size = 99999
86        for c in range(0, len(columns)):
87            if len(columns[c]) > 0 and columns[c][0] < lowest_size:
88                lowest = c
89                lowest_size = columns[c][0]
90        cols += [lowest_size]
91        columns[lowest] = columns[lowest][1:]
92        for c in range(0, len(columns)):
93            if len(columns[c]) > 0 and c != lowest:
94                if columns[c][0] != lowest_size:
95                    columns[c][0] -= lowest_size
96                else:
97                    columns[c] = columns[c][1:]
98    return cols
99
100if __name__ == '__main__':
101    width = 75
102    cols_1 = [width]
103    cols_2 = [10, width - 10]
104    cols_3 = even_columns(3, width = width)
105    cols_4 = even_columns(4, width = width)
106    print(line(cols_1, marker = 'X', linesep = ''))
107    print(line(cols_1, marker = '+', indent = 1, linesep = ''))
108    print(line(cols_1, marker = '+', indent = 2, linesep = ''))
109    print(line(cols_2, marker = '+', indent = 2, linesep = ''))
110    print(line(cols_3, marker = '+', indent = 2, linesep = ''))
111    print(line(cols_4, marker = '+', indent = 2, linesep = ''))
112    print(row(cols_4, [' %d' % (i) for i in [1, 2, 3, 4]], indent = 2, linesep = ''))
113    print(line(cols_4, marker = '+', indent = 2, linesep = ''))
114    print(line(merge_columns([cols_2, cols_3, cols_4]), indent = 2, linesep = ''))
Note: See TracBrowser for help on using the repository browser.