-
Notifications
You must be signed in to change notification settings - Fork 1
/
text_editor.py
128 lines (116 loc) · 4.75 KB
/
text_editor.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from typing import List, Optional
from prompt_toolkit.filters import (
Condition,
FilterOrBool,
has_focus,
is_done,
is_true,
to_filter,
)
from prompt_toolkit.formatted_text import (
AnyFormattedText,
)
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.layout.containers import (
Window,
)
from prompt_toolkit.layout.controls import (
BufferControl,
GetLinePrefixCallable,
)
from prompt_toolkit.layout.dimension import AnyDimension
from prompt_toolkit.layout.dimension import D
from prompt_toolkit.layout.dimension import Dimension as D
from prompt_toolkit.layout.margins import NumberedMargin, ScrollbarMargin
from prompt_toolkit.layout.processors import (
AppendAutoSuggestion,
BeforeInput,
ConditionalProcessor,
PasswordProcessor,
Processor,
)
from prompt_toolkit.lexers import DynamicLexer
from prompt_toolkit.widgets import (
SearchToolbar,
TextArea,
)
class TextEditor(TextArea):
"""
Wrapper of default prompt toolkit textarea to
redefine buffer control component init with provided keybindings
"""
def __init__(self, multiline: FilterOrBool = True,
password: FilterOrBool = False,
focusable: FilterOrBool = True,
focus_on_click: FilterOrBool = False,
width: AnyDimension = None,
height: AnyDimension = None,
dont_extend_height: FilterOrBool = False,
dont_extend_width: FilterOrBool = False,
line_numbers: bool = False,
get_line_prefix: Optional[GetLinePrefixCallable] = None,
scrollbar: bool = False, style: str = "",
search_field: Optional[SearchToolbar] = None,
preview_search: FilterOrBool = True,
prompt: AnyFormattedText = "",
input_processors: Optional[List[Processor]] = None,
key_bindings: KeyBindings = None, **kwargs) -> None:
super().__init__(multiline=multiline, password=password,
focusable=focusable, focus_on_click=focus_on_click,
width=width, height=height,
dont_extend_height=dont_extend_height,
dont_extend_width=dont_extend_width,
line_numbers=line_numbers,
get_line_prefix=get_line_prefix,
scrollbar=scrollbar, style=style,
search_field=search_field,
preview_search=preview_search, prompt=prompt,
input_processors=input_processors, **kwargs)
if search_field is None:
search_control = None
elif isinstance(search_field, SearchToolbar):
search_control = search_field.control
if input_processors is None:
input_processors = []
self.control = BufferControl(
buffer=self.buffer,
lexer=DynamicLexer(lambda: self.lexer),
input_processors=[
ConditionalProcessor(
AppendAutoSuggestion(),
has_focus(self.buffer) & ~is_done
),
ConditionalProcessor(
processor=PasswordProcessor(),
filter=to_filter(password)
),
BeforeInput(prompt,
style="class:text-area.prompt"),
] + input_processors,
search_buffer_control=search_control,
preview_search=preview_search,
focusable=focusable,
focus_on_click=focus_on_click,
key_bindings=key_bindings
)
if multiline:
right_margins = [ScrollbarMargin(display_arrows=True)] \
if scrollbar else []
left_margins = [NumberedMargin()] if line_numbers else []
else:
height = D.exact(1)
left_margins = []
right_margins = []
style = "class:text-area " + style
self.window = Window(
height=height,
width=width,
dont_extend_height=dont_extend_height,
dont_extend_width=dont_extend_width,
content=self.control,
style=style,
wrap_lines=Condition(lambda: is_true(self.wrap_lines)),
left_margins=left_margins,
right_margins=right_margins,
get_line_prefix=get_line_prefix
)