|
1 | 1 | from __future__ import absolute_import
|
2 | 2 |
|
3 | 3 | import code
|
| 4 | +import collections |
4 | 5 | import io
|
5 | 6 | import logging
|
6 | 7 | import sys
|
@@ -91,10 +92,44 @@ def main(args=None, locals_=None, banner=None, welcome_message=None):
|
91 | 92 | return extract_exit_value(exit_value)
|
92 | 93 |
|
93 | 94 |
|
| 95 | +def _combined_events(event_provider, paste_threshold): |
| 96 | + """Combines consecutive keypress events into paste events.""" |
| 97 | + timeout = (yield 'nonsense_event') # so send can be used |
| 98 | + queue = collections.deque() |
| 99 | + while True: |
| 100 | + e = event_provider.send(timeout) |
| 101 | + if isinstance(e, curtsies.events.Event): |
| 102 | + timeout = (yield e) |
| 103 | + continue |
| 104 | + elif e is None: |
| 105 | + continue |
| 106 | + else: |
| 107 | + queue.append(e) |
| 108 | + e = event_provider.send(0) |
| 109 | + while not (e is None or isinstance(e, curtsies.events.Event)): |
| 110 | + queue.append(e) |
| 111 | + e = event_provider.send(0) |
| 112 | + if len(queue) >= paste_threshold: |
| 113 | + paste = curtsies.events.PasteEvent() |
| 114 | + paste.events.extend(queue) |
| 115 | + queue.clear() |
| 116 | + yield paste |
| 117 | + else: |
| 118 | + while len(queue): |
| 119 | + yield queue.popleft() |
| 120 | + |
| 121 | + |
| 122 | +def combined_events(event_provider, paste_threshold=10): |
| 123 | + g = _combined_events(event_provider, paste_threshold) |
| 124 | + next(g) |
| 125 | + return g |
| 126 | + |
| 127 | + |
94 | 128 | def mainloop(config, locals_, banner, interp=None, paste=None,
|
95 | 129 | interactive=True):
|
96 |
| - with curtsies.input.Input(keynames='curtsies', sigint_event=True) as \ |
97 |
| - input_generator: |
| 130 | + with curtsies.input.Input(keynames='curtsies', |
| 131 | + sigint_event=True, |
| 132 | + paste_threshold=None) as input_generator: |
98 | 133 | with curtsies.window.CursorAwareWindow(
|
99 | 134 | sys.stdout,
|
100 | 135 | sys.stdin,
|
@@ -180,12 +215,13 @@ def process_event(e):
|
180 | 215 |
|
181 | 216 | # do a display before waiting for first event
|
182 | 217 | process_event(None)
|
| 218 | + inputs = combined_events(input_generator) |
183 | 219 | for unused in find_iterator:
|
184 |
| - e = input_generator.send(0) |
| 220 | + e = inputs.send(0) |
185 | 221 | if e is not None:
|
186 | 222 | process_event(e)
|
187 | 223 |
|
188 |
| - for e in input_generator: |
| 224 | + for e in inputs: |
189 | 225 | process_event(e)
|
190 | 226 |
|
191 | 227 |
|
|
0 commit comments