From a55b1b855840054805835acaba1970ea98611819 Mon Sep 17 00:00:00 2001
From: Darren Burns <darrenb900@gmail.com>
Date: Tue, 12 Jul 2022 15:24:33 +0100
Subject: [PATCH 1/2] Dont record dynamic segments

---
 rich/console.py | 25 ++++++++++++++++++++++++-
 rich/control.py |  4 ++--
 rich/live.py    | 12 +++++++++---
 rich/segment.py |  8 ++++++++
 4 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/rich/console.py b/rich/console.py
index 8c6049a49..50f0c743b 100644
--- a/rich/console.py
+++ b/rich/console.py
@@ -60,7 +60,7 @@
 from .region import Region
 from .scope import render_scope
 from .screen import Screen
-from .segment import Segment
+from .segment import Segment, _NonRecordedSegment
 from .style import Style, StyleType
 from .styled import Styled
 from .terminal_theme import DEFAULT_TERMINAL_THEME, SVG_EXPORT_THEME, TerminalTheme
@@ -1975,6 +1975,15 @@ def _check_buffer(self) -> None:
                     self._record_buffer.extend(self._buffer[:])
 
             if self._buffer_index == 0:
+                if self.record:
+                    with self._record_buffer_lock:
+                        self._record_buffer.extend(
+                            [
+                                segment
+                                for segment in self._buffer[:]
+                                if not isinstance(segment, _NonRecordedSegment)
+                            ]
+                        )
 
                 if self.is_jupyter:  # pragma: no cover
                     from .jupyter import display
@@ -2516,6 +2525,20 @@ def _svg_hash(svg_main_code: str) -> str:
     return str(zlib.adler32(svg_main_code.encode()))
 
 
+class HideFromRecord:
+    """Wraps a renderable and prevents it from being written to the record buffer."""
+
+    def __init__(self, renderable: ConsoleRenderable):
+        self.renderable = renderable
+
+    def __rich_console__(
+        self, console: Console, options: ConsoleOptions
+    ) -> RenderResult:
+        rendered = console.render(self.renderable, options)
+        for segment in rendered:
+            yield _NonRecordedSegment(*segment)
+
+
 if __name__ == "__main__":  # pragma: no cover
     console = Console(record=True)
 
diff --git a/rich/control.py b/rich/control.py
index a8a912553..8188cc5fe 100644
--- a/rich/control.py
+++ b/rich/control.py
@@ -7,7 +7,7 @@
 else:
     from typing_extensions import Final  # pragma: no cover
 
-from .segment import ControlCode, ControlType, Segment
+from .segment import ControlCode, ControlType, Segment, _NonRecordedSegment
 
 if TYPE_CHECKING:
     from .console import Console, ConsoleOptions, RenderResult
@@ -181,7 +181,7 @@ def __rich_console__(
         self, console: "Console", options: "ConsoleOptions"
     ) -> "RenderResult":
         if self.segment.text:
-            yield self.segment
+            yield _NonRecordedSegment(*self.segment)
 
 
 def strip_control_codes(
diff --git a/rich/live.py b/rich/live.py
index e635fe5c9..b1d1d5e3b 100644
--- a/rich/live.py
+++ b/rich/live.py
@@ -1,10 +1,16 @@
 import sys
 from threading import Event, RLock, Thread
 from types import TracebackType
-from typing import IO, Any, Callable, List, Optional, TextIO, Type, cast
+from typing import IO, Any, Callable, List, Optional, Type, cast
 
 from . import get_console
-from .console import Console, ConsoleRenderable, RenderableType, RenderHook
+from .console import (
+    Console,
+    ConsoleRenderable,
+    HideFromRecord,
+    RenderableType,
+    RenderHook,
+)
 from .control import Control
 from .file_proxy import FileProxy
 from .jupyter import JupyterMixin
@@ -257,7 +263,7 @@ def process_renderables(
                     if self._alt_screen
                     else self._live_render.position_cursor()
                 )
-                renderables = [reset, *renderables, self._live_render]
+                renderables = [reset, *renderables, HideFromRecord(self._live_render)]
         elif (
             not self._started and not self.transient
         ):  # if it is finished render the final output for files or dumb_terminals
diff --git a/rich/segment.py b/rich/segment.py
index 20ccedf8a..fa4114312 100644
--- a/rich/segment.py
+++ b/rich/segment.py
@@ -705,6 +705,14 @@ def __rich_console__(
                 yield from line
 
 
+class _NonRecordedSegment(Segment):
+    """A specialised Segment which will not be written to the record
+    buffer and therefore will not appear in exported output. Useful for
+    live updating renderables like progress bars."""
+
+    pass
+
+
 if __name__ == "__main__":  # pragma: no cover
     from rich.console import Console
     from rich.syntax import Syntax

From 96fc0a6cd5248049dd16d4effb6978a929c748f4 Mon Sep 17 00:00:00 2001
From: Darren Burns <darrenb900@gmail.com>
Date: Tue, 12 Jul 2022 15:26:58 +0100
Subject: [PATCH 2/2] Fix a merge issue

---
 rich/console.py | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/rich/console.py b/rich/console.py
index 50f0c743b..5f84dd312 100644
--- a/rich/console.py
+++ b/rich/console.py
@@ -1972,19 +1972,15 @@ def _check_buffer(self) -> None:
         with self._lock:
             if self.record:
                 with self._record_buffer_lock:
-                    self._record_buffer.extend(self._buffer[:])
+                    self._record_buffer.extend(
+                        [
+                            segment
+                            for segment in self._buffer[:]
+                            if not isinstance(segment, _NonRecordedSegment)
+                        ]
+                    )
 
             if self._buffer_index == 0:
-                if self.record:
-                    with self._record_buffer_lock:
-                        self._record_buffer.extend(
-                            [
-                                segment
-                                for segment in self._buffer[:]
-                                if not isinstance(segment, _NonRecordedSegment)
-                            ]
-                        )
-
                 if self.is_jupyter:  # pragma: no cover
                     from .jupyter import display
 
