@@ -1385,9 +1385,14 @@ def render_lines(
1385
1385
extra_lines = render_options .height - len (lines )
1386
1386
if extra_lines > 0 :
1387
1387
pad_line = [
1388
- [Segment (" " * render_options .max_width , style ), Segment ("\n " )]
1389
- if new_lines
1390
- else [Segment (" " * render_options .max_width , style )]
1388
+ (
1389
+ [
1390
+ Segment (" " * render_options .max_width , style ),
1391
+ Segment ("\n " ),
1392
+ ]
1393
+ if new_lines
1394
+ else [Segment (" " * render_options .max_width , style )]
1395
+ )
1391
1396
]
1392
1397
lines .extend (pad_line * extra_lines )
1393
1398
@@ -1436,9 +1441,11 @@ def render_str(
1436
1441
rich_text .overflow = overflow
1437
1442
else :
1438
1443
rich_text = Text (
1439
- _emoji_replace (text , default_variant = self ._emoji_variant )
1440
- if emoji_enabled
1441
- else text ,
1444
+ (
1445
+ _emoji_replace (text , default_variant = self ._emoji_variant )
1446
+ if emoji_enabled
1447
+ else text
1448
+ ),
1442
1449
justify = justify ,
1443
1450
overflow = overflow ,
1444
1451
style = style ,
@@ -1989,6 +1996,20 @@ def log(
1989
1996
):
1990
1997
buffer_extend (line )
1991
1998
1999
+ def on_broken_pipe (self ) -> None :
2000
+ """This function is called when a `BrokenPipeError` is raised.
2001
+
2002
+ This can occur when piping Textual output in Linux and macOS.
2003
+ The default implementation is to exit the app, but you could implement
2004
+ this method in a subclass to change the behavior.
2005
+
2006
+ See https://docs.python.org/3/library/signal.html#note-on-sigpipe for details.
2007
+ """
2008
+ self .quiet = True
2009
+ devnull = os .open (os .devnull , os .O_WRONLY )
2010
+ os .dup2 (devnull , sys .stdout .fileno ())
2011
+ raise SystemExit (1 )
2012
+
1992
2013
def _check_buffer (self ) -> None :
1993
2014
"""Check if the buffer may be rendered. Render it if it can (e.g. Console.quiet is False)
1994
2015
Rendering is supported on Windows, Unix and Jupyter environments. For
@@ -1998,6 +2019,19 @@ def _check_buffer(self) -> None:
1998
2019
if self .quiet :
1999
2020
del self ._buffer [:]
2000
2021
return
2022
+
2023
+ try :
2024
+ self ._write_buffer ()
2025
+ except BrokenPipeError :
2026
+ self .on_broken_pipe ()
2027
+
2028
+ def _write_buffer (self ) -> None :
2029
+ """Check if the buffer may be rendered. Render it if it can (e.g. Console.quiet is False)
2030
+ Rendering is supported on Windows, Unix and Jupyter environments. For
2031
+ legacy Windows consoles, the win32 API is called directly.
2032
+ This method will also record what it renders if recording is enabled via Console.record.
2033
+ """
2034
+
2001
2035
with self ._lock :
2002
2036
if self .record :
2003
2037
with self ._record_buffer_lock :
0 commit comments