|
5 | 5 | import functools
|
6 | 6 | import os
|
7 | 7 | import random
|
| 8 | +import shutil |
8 | 9 | import socket
|
9 | 10 | import sys
|
10 | 11 | import warnings
|
@@ -134,15 +135,36 @@ def get_node_desc(platform, version_info):
|
134 | 135 | return 'platform {}, python {}'.format(platform, '{}.{}.{}-{}-{}'.format(*version_info[:5]))
|
135 | 136 |
|
136 | 137 | @staticmethod
|
137 |
| - def sep(stream, s, txt): |
| 138 | + def get_width(): |
| 139 | + width, _ = shutil.get_terminal_size(fallback=(80, 24)) |
| 140 | + # The Windows get_terminal_size may be bogus, let's sanify a bit. |
| 141 | + if width < 40: |
| 142 | + width = 80 |
| 143 | + # The goal is to have the line be as long as possible |
| 144 | + # under the condition that len(line) <= fullwidth. |
| 145 | + if sys.platform == 'win32': |
| 146 | + # If we print in the last column on windows we are on a |
| 147 | + # new line but there is no way to verify/neutralize this |
| 148 | + # (we may not know the exact line width). |
| 149 | + # So let's be defensive to avoid empty lines in the output. |
| 150 | + width -= 1 |
| 151 | + return width |
| 152 | + |
| 153 | + def sep(self, stream, s, txt): |
138 | 154 | if hasattr(stream, 'sep'):
|
139 | 155 | stream.sep(s, txt)
|
140 | 156 | else:
|
141 |
| - sep_total = max((70 - 2 - len(txt)), 2) |
142 |
| - sep_len = sep_total // 2 |
143 |
| - sep_extra = sep_total % 2 |
144 |
| - out = f'{s * sep_len} {txt} {s * (sep_len + sep_extra)}\n' |
145 |
| - stream.write(out) |
| 157 | + fullwidth = self.get_width() |
| 158 | + N = max((fullwidth - len(txt) - 2) // (2 * len(s)), 1) |
| 159 | + fill = s * N |
| 160 | + line = f'{fill} {txt} {fill}' |
| 161 | + # In some situations there is room for an extra sepchar at the right, |
| 162 | + # in particular if we consider that with a sepchar like "_ " the |
| 163 | + # trailing space is not important at the end of the line. |
| 164 | + if len(line) + len(s.rstrip()) <= fullwidth: |
| 165 | + line += s.rstrip() |
| 166 | + line += '\n' |
| 167 | + stream.write(line) |
146 | 168 |
|
147 | 169 | @_ensure_topdir
|
148 | 170 | def summary(self, stream):
|
|
0 commit comments