/
input_dialog.py
66 lines (55 loc) · 1.85 KB
/
input_dialog.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
60
61
62
63
64
65
66
from asyncio import Future
from prompt_toolkit.application.current import get_app
from prompt_toolkit.key_binding.defaults import load_key_bindings
from prompt_toolkit.layout.containers import (
HSplit,
)
from prompt_toolkit.layout.dimension import D
from prompt_toolkit.widgets import (
Button,
Dialog,
Label,
)
from text_editor import TextEditor
class InputDialog:
"""
Message with text input and OK / cancel buttons
"""
def __init__(self, title="", label_text="", completer=None):
self.future = Future()
def accept_text(buf) -> bool:
"""
Change focus to OK button after user pressed enter on text field.
"""
get_app().layout.focus(ok_button)
buf.complete_state = None
return True
def accept():
"""
Set future result to text from text field if user pressed OK
button.
"""
self.future.set_result(self.text_area.text)
def cancel():
"""
Set future result to None if user pressed cancel button.
"""
self.future.set_result(None)
self.text_area = TextEditor(
completer=completer,
multiline=False,
width=D(preferred=40),
accept_handler=accept_text,
key_bindings=load_key_bindings(),
)
ok_button = Button(text="OK", handler=accept)
cancel_button = Button(text="Cancel", handler=cancel)
self.dialog = Dialog(
title=title,
body=HSplit([Label(text=label_text), self.text_area]),
buttons=[ok_button, cancel_button],
width=D(preferred=80),
modal=True,
)
def __pt_container__(self):
return self.dialog