Skip to content

Commit 1886366

Browse files
Accept style parameter in print_container utility.
1 parent bb3693a commit 1886366

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

prompt_toolkit/shortcuts/utils.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,6 @@ def print_formatted_text(
9595
"""
9696
assert not (output and file)
9797

98-
# Build/merge style.
99-
styles = [default_ui_style()]
100-
if include_default_pygments_style:
101-
styles.append(default_pygments_style())
102-
if style:
103-
styles.append(style)
104-
105-
merged_style = merge_styles(styles)
106-
10798
# Create Output object.
10899
if output is None:
109100
if file:
@@ -137,7 +128,9 @@ def to_text(val: Any) -> StyleAndTextTuples:
137128
renderer_print_formatted_text(
138129
output,
139130
fragments,
140-
merged_style,
131+
_create_merged_style(
132+
style, include_default_pygments_style=include_default_pygments_style
133+
),
141134
color_depth=color_depth,
142135
style_transformation=style_transformation,
143136
)
@@ -147,7 +140,12 @@ def to_text(val: Any) -> StyleAndTextTuples:
147140
output.flush()
148141

149142

150-
def print_container(container: "Container", file: Optional[TextIO] = None) -> None:
143+
def print_container(
144+
container: "Container",
145+
file: Optional[TextIO] = None,
146+
style: Optional[BaseStyle] = None,
147+
include_default_pygments_style: bool = True,
148+
) -> None:
151149
"""
152150
Print any layout to the output in a non-interactive way.
153151
@@ -168,11 +166,31 @@ def exit_immediately() -> None:
168166
get_event_loop().call_soon(lambda: app.exit())
169167

170168
app: Application[None] = Application(
171-
layout=Layout(container=container), output=output, input=DummyInput()
169+
layout=Layout(container=container),
170+
output=output,
171+
input=DummyInput(),
172+
style=_create_merged_style(
173+
style, include_default_pygments_style=include_default_pygments_style
174+
),
172175
)
173176
app.run(pre_run=exit_immediately)
174177

175178

179+
def _create_merged_style(
180+
style: Optional[BaseStyle], include_default_pygments_style: bool
181+
) -> BaseStyle:
182+
"""
183+
Merge user defined style with built-in style.
184+
"""
185+
styles = [default_ui_style()]
186+
if include_default_pygments_style:
187+
styles.append(default_pygments_style())
188+
if style:
189+
styles.append(style)
190+
191+
return merge_styles(styles)
192+
193+
176194
def clear() -> None:
177195
"""
178196
Clear the screen.

0 commit comments

Comments
 (0)