1
from __future__ import generators
5
from py.__.misc import terminal_helper
9
fullwidth = terminal_helper.terminal_width
10
def __init__(self, file):
11
self.file = py.io.dupfile(file)
13
def sep(self, sepchar, title=None, fullwidth=None):
15
fullwidth = self.fullwidth
16
# the goal is to have the line be as long as possible
17
# under the condition that len(line) <= fullwidth
19
# we want 2 + 2*len(fill) + len(title) <= fullwidth
20
# i.e. 2 + 2*len(sepchar)*N + len(title) <= fullwidth
21
# 2*len(sepchar)*N <= fullwidth - len(title) - 2
22
# N <= (fullwidth - len(title) - 2) // (2*len(sepchar))
23
N = (fullwidth - len(title) - 2) // (2*len(sepchar))
25
line = "%s %s %s" % (fill, title, fill)
27
# we want len(sepchar)*N <= fullwidth
28
# i.e. N <= fullwidth // len(sepchar)
29
line = sepchar * (fullwidth // len(sepchar))
30
# in some situations there is room for an extra sepchar at the right,
31
# in particular if we consider that with a sepchar like "_ " the
32
# trailing space is not important at the end of the line
33
if len(line) + len(sepchar.rstrip()) <= fullwidth:
34
line += sepchar.rstrip()
37
class TerminalOut(Out):
39
def __init__(self, file):
40
super(TerminalOut, self).__init__(file)
42
def sep(self, sepchar, title=None):
43
super(TerminalOut, self).sep(sepchar, title,
44
terminal_helper.get_terminal_width())
47
self.file.write(str(s))
52
self.file.write(s + '\n')
57
def rewrite(self, s=''):
58
#self.write('\x1b[u%s' % s) - this escape sequence does
59
# strange things, or nothing at all, on various terminals.
60
# XXX what is it supposed to do in the first place??
65
self.file.write(str(s))
70
self.file.write(str(s) + '\n')
75
def rewrite(self, s=''):
79
# XXX investigate further into terminal output, this is not enough
82
file = py.std.sys.stdout
83
elif hasattr(file, 'send'):
84
file = WriteFile(file.send)
86
file = WriteFile(file)
87
if hasattr(file, 'isatty') and file.isatty():
88
return TerminalOut(file)
92
class WriteFile(object):
93
def __init__(self, writemethod):
94
self.write = writemethod