Update the displayed markdown content. Args: text (str): The markdown text received so far final (bool): If True, this is the final update and we should clean up Splits the output into "stable" older lines and the "last few" lines which aren't consid
(self, text, final=False)
| 147 | pass # Ignore any errors during cleanup |
| 148 | |
| 149 | def update(self, text, final=False): |
| 150 | """Update the displayed markdown content. |
| 151 | |
| 152 | Args: |
| 153 | text (str): The markdown text received so far |
| 154 | final (bool): If True, this is the final update and we should clean up |
| 155 | |
| 156 | Splits the output into "stable" older lines and the "last few" lines |
| 157 | which aren't considered stable. They may shift around as new chunks |
| 158 | are appended to the markdown text. |
| 159 | |
| 160 | The stable lines emit to the console above the Live window. |
| 161 | The unstable lines emit into the Live window so they can be repainted. |
| 162 | |
| 163 | Markdown going to the console works better in terminal scrollback buffers. |
| 164 | The live window doesn't play nice with terminal scrollback. |
| 165 | """ |
| 166 | # On the first call, stop the spinner and start the Live renderer |
| 167 | if not getattr(self, "_live_started", False): |
| 168 | self.live = Live(Text(""), refresh_per_second=1.0 / self.min_delay) |
| 169 | self.live.start() |
| 170 | self._live_started = True |
| 171 | |
| 172 | now = time.time() |
| 173 | # Throttle updates to maintain smooth rendering |
| 174 | if not final and now - self.when < self.min_delay: |
| 175 | return |
| 176 | self.when = now |
| 177 | |
| 178 | # Measure render time and adjust min_delay to maintain smooth rendering |
| 179 | start = time.time() |
| 180 | lines = self._render_markdown_to_lines(text) |
| 181 | render_time = time.time() - start |
| 182 | |
| 183 | # Set min_delay to render time plus a small buffer |
| 184 | self.min_delay = min(max(render_time * 10, 1.0 / 20), 2) |
| 185 | |
| 186 | num_lines = len(lines) |
| 187 | |
| 188 | # How many lines have "left" the live window and are now considered stable? |
| 189 | # Or if final, consider all lines to be stable. |
| 190 | if not final: |
| 191 | num_lines -= self.live_window |
| 192 | |
| 193 | # If we have stable content to display... |
| 194 | if final or num_lines > 0: |
| 195 | # How many stable lines do we need to newly show above the live window? |
| 196 | num_printed = len(self.printed) |
| 197 | show = num_lines - num_printed |
| 198 | |
| 199 | # Skip if no new lines to show above live window |
| 200 | if show <= 0: |
| 201 | return |
| 202 | |
| 203 | # Get the new lines and display them |
| 204 | show = lines[num_printed:num_lines] |
| 205 | show = "".join(show) |
| 206 | show = Text.from_ansi(show) |