sources for out.py [rev. 38799]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from __future__ import generators
import sys
import os
import py
from py.__.misc import terminal_helper
class Out(object):
    tty = False
    fullwidth = terminal_helper.terminal_width
    def __init__(self, file):
        self.file = py.io.dupfile(file)
    def sep(self, sepchar, title=None, fullwidth=None):
        if not fullwidth:
            fullwidth = self.fullwidth
        # the goal is to have the line be as long as possible
        # under the condition that len(line) <= fullwidth
        if title is not None:
            # we want 2 + 2*len(fill) + len(title) <= fullwidth
            # i.e.    2 + 2*len(sepchar)*N + len(title) <= fullwidth
            #         2*len(sepchar)*N <= fullwidth - len(title) - 2
            #         N <= (fullwidth - len(title) - 2) // (2*len(sepchar))
            N = (fullwidth - len(title) - 2) // (2*len(sepchar))
            fill = sepchar * N
            line = "%s %s %s" % (fill, title, fill)
        else:
            # we want len(sepchar)*N <= fullwidth
            # i.e.    N <= fullwidth // len(sepchar)
            line = sepchar * (fullwidth // len(sepchar))
        # in some situations there is room for an extra sepchar at the right,
        # in particular if we consider that with a sepchar like "_ " the
        # trailing space is not important at the end of the line
        if len(line) + len(sepchar.rstrip()) <= fullwidth:
            line += sepchar.rstrip()
        self.line(line)
class TerminalOut(Out):
    tty = True
    def __init__(self, file):
        super(TerminalOut, self).__init__(file)
    def sep(self, sepchar, title=None):
        super(TerminalOut, self).sep(sepchar, title,
                                     terminal_helper.get_terminal_width())
    def write(self, s):
        self.file.write(str(s))
        self.file.flush()
    def line(self, s=''):
        if s:
            self.file.write(s + '\n')
        else:
            self.file.write('\n')
        self.file.flush()
    def rewrite(self, s=''):
        #self.write('\x1b[u%s' % s) - this escape sequence does
        # strange things, or nothing at all, on various terminals.
        # XXX what is it supposed to do in the first place??
        self.write(s)
class FileOut(Out):
    def write(self, s):
        self.file.write(str(s))
        self.file.flush()
    def line(self, s=''):
        if s:
            self.file.write(str(s) + '\n')
        else:
            self.file.write('\n')
        self.file.flush()
    def rewrite(self, s=''):
        self.write(s)
def getout(file):
    # XXX investigate further into terminal output, this is not enough
    #
    if file is None: 
        file = py.std.sys.stdout 
    elif hasattr(file, 'send'):
        file = WriteFile(file.send) 
    elif callable(file):
        file = WriteFile(file)
    if hasattr(file, 'isatty') and file.isatty(): 
        return TerminalOut(file)
    else:
        return FileOut(file)
class WriteFile(object): 
    def __init__(self, writemethod): 
        self.write = writemethod 
    def flush(self): 
        return