-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipipe_pt.py
59 lines (49 loc) · 2.03 KB
/
ipipe_pt.py
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
from __future__ import print_function, unicode_literals
from prompt_toolkit import Application, CommandLineInterface
from prompt_toolkit.layout.controls import FillControl
from prompt_toolkit.shortcuts import (
Buffer, BufferControl, DEFAULT_BUFFER, KeyBindingManager, Keys, Window,
HSplit, Token, LayoutDimension as D, create_eventloop)
from traceback import print_exc
def instapipe(runner, init_cmd, instant=False, input_lines=1, input_deco=''):
layout = HSplit([
Window(content=BufferControl(buffer_name=DEFAULT_BUFFER),
height=D.exact(1)),
Window(content=FillControl('-', token=Token.Line), height=D.exact(1)),
Window(content=BufferControl(buffer_name='OUTPUT'))
])
buffers, keybinds = _setup_buffers(runner, layout.children[-1], instant)
app = Application(layout=layout, buffers=buffers, mouse_support=True,
key_bindings_registry=keybinds, use_alternate_screen=True)
eventloop = create_eventloop()
final_cmd = ''
try:
final_cmd = CommandLineInterface(application=app, eventloop=eventloop).run()
except:
print_exc()
finally:
eventloop.close()
return final_cmd
def _setup_buffers(runner, output_window, instant_mode):
input_buffer = Buffer(is_multiline=False)
output_buffer = Buffer(is_multiline=True)
def _run(_):
n_head = output_window.render_info.window_height
result, returncode = runner(input_buffer.text, n_head)
output_buffer.text = unicode(result, 'utf8')
reg = KeyBindingManager().registry
if instant_mode:
# run the command on every edit
input_buffer.on_text_changed += _run
else:
# run after each carriage return
reg.add_binding(Keys.ControlJ, eager=True)(_run)
@reg.add_binding(Keys.ControlC, eager=True)
@reg.add_binding(Keys.ControlQ, eager=True)
def _exit(event):
event.cli.set_return_value(None)
@reg.add_binding(Keys.ControlD, eager=True)
def _exit_with_value(event):
event.cli.set_return_value(input_buffer.text)
bufmap = {DEFAULT_BUFFER: input_buffer, 'OUTPUT': output_buffer}
return bufmap, reg