~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to py/test/terminal/out.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import generators
 
2
import sys
 
3
import os
 
4
import py
 
5
from py.__.misc import terminal_helper
 
6
 
 
7
class Out(object):
 
8
    tty = False
 
9
    fullwidth = terminal_helper.terminal_width
 
10
    def __init__(self, file):
 
11
        self.file = py.io.dupfile(file)
 
12
 
 
13
    def sep(self, sepchar, title=None, fullwidth=None):
 
14
        if not fullwidth:
 
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
 
18
        if title is not None:
 
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))
 
24
            fill = sepchar * N
 
25
            line = "%s %s %s" % (fill, title, fill)
 
26
        else:
 
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()
 
35
        self.line(line)
 
36
 
 
37
class TerminalOut(Out):
 
38
    tty = True
 
39
    def __init__(self, file):
 
40
        super(TerminalOut, self).__init__(file)
 
41
 
 
42
    def sep(self, sepchar, title=None):
 
43
        super(TerminalOut, self).sep(sepchar, title,
 
44
                                     terminal_helper.get_terminal_width())
 
45
 
 
46
    def write(self, s):
 
47
        self.file.write(str(s))
 
48
        self.file.flush()
 
49
 
 
50
    def line(self, s=''):
 
51
        if s:
 
52
            self.file.write(s + '\n')
 
53
        else:
 
54
            self.file.write('\n')
 
55
        self.file.flush()
 
56
 
 
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??
 
61
        self.write(s)
 
62
 
 
63
class FileOut(Out):
 
64
    def write(self, s):
 
65
        self.file.write(str(s))
 
66
        self.file.flush()
 
67
 
 
68
    def line(self, s=''):
 
69
        if s:
 
70
            self.file.write(str(s) + '\n')
 
71
        else:
 
72
            self.file.write('\n')
 
73
        self.file.flush()
 
74
 
 
75
    def rewrite(self, s=''):
 
76
        self.write(s)
 
77
 
 
78
def getout(file):
 
79
    # XXX investigate further into terminal output, this is not enough
 
80
    #
 
81
    if file is None: 
 
82
        file = py.std.sys.stdout 
 
83
    elif hasattr(file, 'send'):
 
84
        file = WriteFile(file.send) 
 
85
    elif callable(file):
 
86
        file = WriteFile(file)
 
87
    if hasattr(file, 'isatty') and file.isatty(): 
 
88
        return TerminalOut(file)
 
89
    else:
 
90
        return FileOut(file)
 
91
 
 
92
class WriteFile(object): 
 
93
    def __init__(self, writemethod): 
 
94
        self.write = writemethod 
 
95
    def flush(self): 
 
96
        return 
 
97