|
1 | 1 | #!/usr/bin/env python
|
2 |
| -"""Example usage of terminaltables using colorclass. |
| 2 | +"""Example usage of terminaltables with colorclass. |
3 | 3 |
|
4 | 4 | Just prints sample text and exits.
|
5 | 5 | """
|
|
11 | 11 | from terminaltables import SingleTable
|
12 | 12 |
|
13 | 13 |
|
14 |
| -Windows.enable(auto_colors=True, reset_atexit=True) # Does nothing if not on Windows. |
15 |
| - |
16 |
| -table_data = [ |
17 |
| - [Color('{autogreen}<10ms{/autogreen}'), '192.168.0.100, 192.168.0.101'], |
18 |
| - [Color('{autoyellow}10ms <= 100ms{/autoyellow}'), '192.168.0.102, 192.168.0.103'], |
19 |
| - [Color('{autored}>100ms{/autored}'), '192.168.0.105'], |
20 |
| -] |
21 |
| -table = SingleTable(table_data) |
22 |
| -table.inner_heading_row_border = False |
23 |
| -print() |
24 |
| -print(table.table) |
25 |
| - |
26 |
| -table.title = '192.168.0.105' |
27 |
| -table.justify_columns = {0: 'center', 1: 'center', 2: 'center'} |
28 |
| -table.inner_row_border = True |
29 |
| -table.table_data = [ |
30 |
| - [Color('Low Space'), Color('{autocyan}Nominal Space{/autocyan}'), Color('Excessive Space')], |
31 |
| - [Color('Low Load'), Color('Nominal Load'), Color('{autored}High Load{/autored}')], |
32 |
| - [Color('{autocyan}Low Free RAM{/autocyan}'), Color('Nominal Free RAM'), Color('High Free RAM')], |
33 |
| -] |
34 |
| -print() |
35 |
| -print(table.table) |
36 |
| - |
37 |
| -table.title = None |
38 |
| -table.outer_border = False |
39 |
| -table.table_data = [['A', 'B'], ['C', 'D']] |
40 |
| -print() |
41 |
| -print(table.table) |
42 |
| - |
43 |
| -table.outer_border = True |
44 |
| -table.inner_row_border = False |
45 |
| -table.inner_column_border = False |
46 |
| -print() |
47 |
| -print(table.table) |
48 |
| - |
49 |
| -table = SingleTable([['Obey Obey Obey Obey']], 'Instructions') |
50 |
| -print() |
51 |
| -print(table.table) |
52 |
| - |
53 |
| -print() |
| 14 | +def table_server_timings(): |
| 15 | + """Return table string to be printed.""" |
| 16 | + table_data = [ |
| 17 | + [Color('{autogreen}<10ms{/autogreen}'), '192.168.0.100, 192.168.0.101'], |
| 18 | + [Color('{autoyellow}10ms <= 100ms{/autoyellow}'), '192.168.0.102, 192.168.0.103'], |
| 19 | + [Color('{autored}>100ms{/autored}'), '192.168.0.105'], |
| 20 | + ] |
| 21 | + table_instance = SingleTable(table_data) |
| 22 | + table_instance.inner_heading_row_border = False |
| 23 | + return table_instance.table |
| 24 | + |
| 25 | + |
| 26 | +def table_server_status(): |
| 27 | + """Return table string to be printed.""" |
| 28 | + table_data = [ |
| 29 | + [Color('Low Space'), Color('{autocyan}Nominal Space{/autocyan}'), Color('Excessive Space')], |
| 30 | + [Color('Low Load'), Color('Nominal Load'), Color('{autored}High Load{/autored}')], |
| 31 | + [Color('{autocyan}Low Free RAM{/autocyan}'), Color('Nominal Free RAM'), Color('High Free RAM')], |
| 32 | + ] |
| 33 | + table_instance = SingleTable(table_data, '192.168.0.105') |
| 34 | + table_instance.inner_heading_row_border = False |
| 35 | + table_instance.inner_row_border = True |
| 36 | + table_instance.justify_columns = {0: 'center', 1: 'center', 2: 'center'} |
| 37 | + return table_instance.table |
| 38 | + |
| 39 | + |
| 40 | +def table_abcd(): |
| 41 | + """Return table string to be printed. Two tables on one line.""" |
| 42 | + table_instance = SingleTable([['A', 'B'], ['C', 'D']]) |
| 43 | + |
| 44 | + # Get first table lines. |
| 45 | + table_instance.outer_border = False |
| 46 | + table_inner_borders = table_instance.table.splitlines() |
| 47 | + |
| 48 | + # Get second table lines. |
| 49 | + table_instance.outer_border = True |
| 50 | + table_instance.inner_heading_row_border = False |
| 51 | + table_instance.inner_column_border = False |
| 52 | + table_outer_borders = table_instance.table.splitlines() |
| 53 | + |
| 54 | + # Combine. |
| 55 | + smallest, largest = sorted([table_inner_borders, table_outer_borders], key=len) |
| 56 | + smallest += [''] * (len(largest) - len(smallest)) # Make both same size. |
| 57 | + combined = list() |
| 58 | + for i, row in enumerate(largest): |
| 59 | + combined.append(row.ljust(10) + ' ' + smallest[i]) |
| 60 | + return '\n'.join(combined) |
| 61 | + |
| 62 | + |
| 63 | +def main(): |
| 64 | + """Main function.""" |
| 65 | + Windows.enable(auto_colors=True, reset_atexit=True) # Does nothing if not on Windows. |
| 66 | + |
| 67 | + # Server timings. |
| 68 | + print(table_server_timings()) |
| 69 | + print() |
| 70 | + |
| 71 | + # Server status. |
| 72 | + print(table_server_status()) |
| 73 | + print() |
| 74 | + |
| 75 | + # Two A B C D tables. |
| 76 | + print(table_abcd()) |
| 77 | + print() |
| 78 | + |
| 79 | + # Instructions. |
| 80 | + table_instance = SingleTable([['Obey Obey Obey Obey']], 'Instructions') |
| 81 | + print(table_instance.table) |
| 82 | + print() |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + main() |
0 commit comments