|
48 | 48 | import math |
49 | 49 | import re |
50 | 50 | import time |
| 51 | +import functools |
51 | 52 |
|
52 | 53 | import struct |
53 | 54 | if platform.system() != 'Windows': |
@@ -116,18 +117,31 @@ def calculate_screen_lines(tokens, width, cursor=0): |
116 | 117 | pos %= width |
117 | 118 | return lines |
118 | 119 |
|
| 120 | +def forward_if_not_current(func): |
| 121 | + @functools.wraps(func) |
| 122 | + def newfunc(self, *args, **kwargs): |
| 123 | + dest = self.get_dest() |
| 124 | + if self is dest: |
| 125 | + return func(self, *args, **kwargs) |
| 126 | + else: |
| 127 | + return getattr(self.get_dest(), newfunc.__name__)(*args, **kwargs) |
| 128 | + return newfunc |
| 129 | + |
119 | 130 |
|
120 | 131 | class FakeStream(object): |
121 | 132 | """Provide a fake file object which calls functions on the interface |
122 | 133 | provided.""" |
123 | 134 |
|
124 | | - def __init__(self, interface): |
| 135 | + def __init__(self, interface, get_dest): |
125 | 136 | self.encoding = getpreferredencoding() |
126 | 137 | self.interface = interface |
| 138 | + self.get_dest = get_dest |
127 | 139 |
|
| 140 | + @forward_if_not_current |
128 | 141 | def write(self, s): |
129 | 142 | self.interface.write(s) |
130 | 143 |
|
| 144 | + @forward_if_not_current |
131 | 145 | def writelines(self, l): |
132 | 146 | for s in l: |
133 | 147 | self.write(s) |
@@ -1916,8 +1930,8 @@ def main_curses(scr, args, config, interactive=True, locals_=None, |
1916 | 1930 | clirepl._C = cols |
1917 | 1931 |
|
1918 | 1932 | sys.stdin = FakeStdin(clirepl) |
1919 | | - sys.stdout = FakeStream(clirepl) |
1920 | | - sys.stderr = FakeStream(clirepl) |
| 1933 | + sys.stdout = FakeStream(clirepl, lambda: sys.stdout) |
| 1934 | + sys.stderr = FakeStream(clirepl, lambda: sys.stderr) |
1921 | 1935 |
|
1922 | 1936 | if args: |
1923 | 1937 | exit_value = () |
|
0 commit comments