Coverage for jsonschema_diff/color/abstraction.py: 86%

7 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-15 18:01 +0000

1""" 

2Abstraction for line-based high-lighters 

3======================================= 

4 

5This module defines :class:`LineHighlighter`, a thin 

6:class:`typing.Protocol` that specifies the minimal contract required by the 

7colour pipeline implemented in :pyfile:`base.py`. 

8 

9Implementors are expected to decorate :class:`rich.text.Text` objects **in 

10place**; therefore methods must not alter the underlying string content – 

11only styling metadata. 

12""" 

13 

14from typing import List, Protocol, Sequence 

15 

16from rich.text import Text 

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 The **same** `Text` object, now containing style spans. 

38 

39 Raises 

40 ------ 

41 NotImplementedError 

42 Always here; concrete subclasses must override this method. 

43 """ 

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

45 

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

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

48 

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

50 

51 Parameters 

52 ---------- 

53 lines : 

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

55 

56 Returns 

57 ------- 

58 The original objects, now styled in place. 

59 """ 

60 return [self.colorize_line(t) for t in lines]