|
17 | 17 | from prompt_toolkit.output import ColorDepth, Output
|
18 | 18 | from prompt_toolkit.styles import (
|
19 | 19 | Attrs,
|
20 |
| - DEFAULT_ATTRS, |
21 | 20 | BaseStyle,
|
22 | 21 | DummyStyleTransformation,
|
23 | 22 | StyleTransformation,
|
@@ -46,6 +45,7 @@ def _output_screen_diff(
|
46 | 45 | is_done: bool, # XXX: drop is_done
|
47 | 46 | full_screen: bool,
|
48 | 47 | attrs_for_style_string: "_StyleStringToAttrsCache",
|
| 48 | + style_string_has_style: "_StyleStringHasStyleCache", |
49 | 49 | size: Size,
|
50 | 50 | previous_width: int,
|
51 | 51 | ) -> Tuple[Point, Optional[str]]:
|
@@ -156,7 +156,7 @@ def get_max_column_index(row: Dict[int, Char]) -> int:
|
156 | 156 | numbers = [
|
157 | 157 | index
|
158 | 158 | for index, cell in row.items()
|
159 |
| - if cell.char != " " or attrs_for_style_string[cell.style] != DEFAULT_ATTRS |
| 159 | + if cell.char != " " or style_string_has_style[cell.style] |
160 | 160 | ]
|
161 | 161 | numbers.append(0)
|
162 | 162 | return max(numbers)
|
@@ -202,7 +202,7 @@ def get_max_column_index(row: Dict[int, Char]) -> int:
|
202 | 202 |
|
203 | 203 | # Loop over the columns.
|
204 | 204 | c = 0
|
205 |
| - while c < new_max_line_len + 1: |
| 205 | + while c <= new_max_line_len: |
206 | 206 | new_char = new_row[c]
|
207 | 207 | old_char = previous_row[c]
|
208 | 208 | char_width = new_char.width or 1
|
@@ -290,6 +290,34 @@ def __missing__(self, style_str: str) -> Attrs:
|
290 | 290 | return attrs
|
291 | 291 |
|
292 | 292 |
|
| 293 | +class _StyleStringHasStyleCache(Dict[str, bool]): |
| 294 | + """ |
| 295 | + Cache for remember which style strings don't render the default output |
| 296 | + style (default fg/bg, no underline and no reverse and no blink). That way |
| 297 | + we know that we should render these cells, even when they're empty (when |
| 298 | + they contain a space). |
| 299 | +
|
| 300 | + Note: we don't consider bold/italic/hidden because they don't change the |
| 301 | + output if there's no text in the cell. |
| 302 | + """ |
| 303 | + |
| 304 | + def __init__(self, style_string_to_attrs: Dict[str, Attrs]) -> None: |
| 305 | + self.style_string_to_attrs = style_string_to_attrs |
| 306 | + |
| 307 | + def __missing__(self, style_str: str) -> bool: |
| 308 | + attrs = self.style_string_to_attrs[style_str] |
| 309 | + is_default = bool( |
| 310 | + attrs.color |
| 311 | + or attrs.bgcolor |
| 312 | + or attrs.underline |
| 313 | + or attrs.blink |
| 314 | + or attrs.reverse |
| 315 | + ) |
| 316 | + |
| 317 | + self[style_str] = is_default |
| 318 | + return is_default |
| 319 | + |
| 320 | + |
293 | 321 | class CPR_Support(Enum):
|
294 | 322 | " Enum: whether or not CPR is supported. "
|
295 | 323 | SUPPORTED = "SUPPORTED"
|
@@ -339,6 +367,7 @@ def __init__(
|
339 | 367 |
|
340 | 368 | # Cache for the style.
|
341 | 369 | self._attrs_for_style: Optional[_StyleStringToAttrsCache] = None
|
| 370 | + self._style_string_has_style: Optional[_StyleStringHasStyleCache] = None |
342 | 371 | self._last_style_hash: Optional[Hashable] = None
|
343 | 372 | self._last_transformation_hash: Optional[Hashable] = None
|
344 | 373 | self._last_color_depth: Optional[ColorDepth] = None
|
@@ -605,11 +634,16 @@ def render(
|
605 | 634 | ):
|
606 | 635 | self._last_screen = None
|
607 | 636 | self._attrs_for_style = None
|
| 637 | + self._style_string_has_style = None |
608 | 638 |
|
609 | 639 | if self._attrs_for_style is None:
|
610 | 640 | self._attrs_for_style = _StyleStringToAttrsCache(
|
611 | 641 | self.style.get_attrs_for_style_str, app.style_transformation
|
612 | 642 | )
|
| 643 | + if self._style_string_has_style is None: |
| 644 | + self._style_string_has_style = _StyleStringHasStyleCache( |
| 645 | + self._attrs_for_style |
| 646 | + ) |
613 | 647 |
|
614 | 648 | self._last_style_hash = self.style.invalidation_hash()
|
615 | 649 | self._last_transformation_hash = app.style_transformation.invalidation_hash()
|
@@ -641,6 +675,7 @@ def render(
|
641 | 675 | is_done,
|
642 | 676 | full_screen=self.full_screen,
|
643 | 677 | attrs_for_style_string=self._attrs_for_style,
|
| 678 | + style_string_has_style=self._style_string_has_style, |
644 | 679 | size=size,
|
645 | 680 | previous_width=(self._last_size.columns if self._last_size else 0),
|
646 | 681 | )
|
|
0 commit comments