Coverage for jsonschema_diff/color/abstraction.py: 88%
8 statements
« prev ^ index » next coverage.py v7.10.5, created at 2025-08-25 07:00 +0000
« prev ^ index » next coverage.py v7.10.5, created at 2025-08-25 07:00 +0000
1from typing import List, Protocol, Sequence
3from rich.text import Text
5"""
6Abstraction for line-based high-lighters
7=======================================
9This module defines :class:`LineHighlighter`, a thin
10:class:`typing.Protocol` that specifies the minimal contract required by the
11colour pipeline implemented in :pyfile:`base.py`.
13Implementors are expected to decorate :class:`rich.text.Text` objects **in
14place**; therefore methods must not alter the underlying string content –
15only styling metadata.
16"""
19class LineHighlighter(Protocol):
20 """Protocol for single-line high-lighters.
22 Concrete implementations *may* also override
23 :meth:`colorize_lines` for bulk operations, but
24 :meth:`colorize_line` is the only mandatory method.
25 """
27 def colorize_line(self, line: Text) -> Text:
28 """Stylise one line **in-place** and return it.
30 Parameters
31 ----------
32 line :
33 A single :class:`rich.text.Text` instance to be colour-styled.
35 Returns
36 -------
37 rich.text.Text
38 The **same** `Text` object, now containing style spans.
40 Raises
41 ------
42 NotImplementedError
43 Always here; concrete subclasses must override this method.
44 """
45 raise NotImplementedError("LineHighlighter.colorize_line должен быть переопределен")
47 def colorize_lines(self, lines: Sequence[Text]) -> List[Text]:
48 """Vectorised helper that stylises a *sequence* of lines.
50 The naïve fallback simply delegates to :meth:`colorize_line`.
52 Parameters
53 ----------
54 lines :
55 Ordered collection of :class:`rich.text.Text` objects.
57 Returns
58 -------
59 list[rich.text.Text]
60 The original objects, now styled in place.
61 """
62 return [self.colorize_line(t) for t in lines]