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

1from typing import List, Protocol, Sequence 

2 

3from rich.text import Text 

4 

5""" 

6Abstraction for line-based high-lighters 

7======================================= 

8 

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`. 

12 

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""" 

17 

18 

19class LineHighlighter(Protocol): 

20 """Protocol for single-line high-lighters. 

21 

22 Concrete implementations *may* also override 

23 :meth:`colorize_lines` for bulk operations, but 

24 :meth:`colorize_line` is the only mandatory method. 

25 """ 

26 

27 def colorize_line(self, line: Text) -> Text: 

28 """Stylise one line **in-place** and return it. 

29 

30 Parameters 

31 ---------- 

32 line : 

33 A single :class:`rich.text.Text` instance to be colour-styled. 

34 

35 Returns 

36 ------- 

37 rich.text.Text 

38 The **same** `Text` object, now containing style spans. 

39 

40 Raises 

41 ------ 

42 NotImplementedError 

43 Always here; concrete subclasses must override this method. 

44 """ 

45 raise NotImplementedError("LineHighlighter.colorize_line должен быть переопределен") 

46 

47 def colorize_lines(self, lines: Sequence[Text]) -> List[Text]: 

48 """Vectorised helper that stylises a *sequence* of lines. 

49 

50 The naïve fallback simply delegates to :meth:`colorize_line`. 

51 

52 Parameters 

53 ---------- 

54 lines : 

55 Ordered collection of :class:`rich.text.Text` objects. 

56 

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]