1818
1919
2020class _ThemeSyntax (Protocol ):
21+ """Protocol for theme objects that map tag names to SGR escape strings."""
2122 def __getitem__ (self , key : str , / ) -> str : ...
2223
2324
@@ -173,9 +174,11 @@ def _compose(self) -> tuple[RenderLine, ...]:
173174 "overlays must be sorted by ascending y"
174175 )
175176 if overlay .insert :
177+ # Splice overlay lines in, pushing existing content down.
176178 lines [adjusted_y :adjusted_y ] = overlay .lines
177179 y_offset += len (overlay .lines )
178180 else :
181+ # Replace existing lines at the overlay position.
179182 target_len = adjusted_y + len (overlay .lines )
180183 if len (lines ) < target_len :
181184 lines .extend ([EMPTY_RENDER_LINE ] * (target_len - len (lines )))
@@ -250,10 +253,13 @@ class LineUpdate:
250253 y : int
251254 start_cell : int
252255 start_x : int
256+ """Screen x-coordinate where the update begins. Used for cursor positioning."""
253257 cells : tuple [RenderCell , ...]
254258 char_width : int = 0
255259 clear_eol : bool = False
256260 reset_to_margin : bool = False
261+ """If True, the console must resync the cursor position after writing
262+ (needed when cells contain non-SGR escape sequences that may move the cursor)."""
257263 text : str = field (init = False , default = "" )
258264
259265 def __post_init__ (self ) -> None :
@@ -273,6 +279,14 @@ def render_cells(
273279 cells : Sequence [RenderCell ],
274280 visual_style : str | None = None ,
275281) -> str :
282+ """Render a sequence of cells into a terminal string with SGR escapes.
283+
284+ Tracks the active SGR state to emit resets only when the style
285+ actually changes, minimizing output bytes.
286+
287+ If *visual_style* is given (used by redraw visualization), it is appended
288+ to every cell's style.
289+ """
276290 rendered : list [str ] = []
277291 active_escape = ""
278292 for cell in cells :
@@ -305,6 +319,8 @@ def diff_render_lines(old: RenderLine, new: RenderLine) -> LineDiff | None:
305319 start_x = 0
306320 max_prefix = min (len (old .cells ), len (new .cells ))
307321 while prefix < max_prefix and old .cells [prefix ] == new .cells [prefix ]:
322+ # Stop at any cell with non-SGR controls, since those might affect
323+ # cursor position and must be re-emitted.
308324 if old .cells [prefix ].controls :
309325 break
310326 start_x += old .cells [prefix ].width
0 commit comments