Skip to content

Commit 43fda5e

Browse files
tsvikasionelmc
authored andcommitted
use correct width
1 parent a131cf2 commit 43fda5e

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/pytest_cov/engine.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import functools
66
import os
77
import random
8+
import shutil
89
import socket
910
import sys
1011
import warnings
@@ -134,15 +135,36 @@ def get_node_desc(platform, version_info):
134135
return 'platform {}, python {}'.format(platform, '{}.{}.{}-{}-{}'.format(*version_info[:5]))
135136

136137
@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):
138154
if hasattr(stream, 'sep'):
139155
stream.sep(s, txt)
140156
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)
146168

147169
@_ensure_topdir
148170
def summary(self, stream):

0 commit comments

Comments
 (0)