Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/rich/containers.py: 58%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

89 statements  

1from itertools import zip_longest 

2from typing import TYPE_CHECKING, Iterable, Iterator, TypeVar, overload 

3 

4if TYPE_CHECKING: 

5 from .console import ( 

6 Console, 

7 ConsoleOptions, 

8 JustifyMethod, 

9 OverflowMethod, 

10 RenderResult, 

11 RenderableType, 

12 ) 

13 from .text import Text 

14 

15from .cells import cell_len 

16from .measure import Measurement 

17 

18T = TypeVar("T") 

19 

20 

21class Renderables: 

22 """A list subclass which renders its contents to the console.""" 

23 

24 def __init__(self, renderables: Iterable["RenderableType"] | None = None) -> None: 

25 self._renderables: list["RenderableType"] = ( 

26 list(renderables) if renderables is not None else [] 

27 ) 

28 

29 def __rich_console__( 

30 self, console: "Console", options: "ConsoleOptions" 

31 ) -> "RenderResult": 

32 """Console render method to insert line-breaks.""" 

33 yield from self._renderables 

34 

35 def __rich_measure__( 

36 self, console: "Console", options: "ConsoleOptions" 

37 ) -> "Measurement": 

38 dimensions = [ 

39 Measurement.get(console, options, renderable) 

40 for renderable in self._renderables 

41 ] 

42 if not dimensions: 

43 return Measurement(1, 1) 

44 _min = max(dimension.minimum for dimension in dimensions) 

45 _max = max(dimension.maximum for dimension in dimensions) 

46 return Measurement(_min, _max) 

47 

48 def append(self, renderable: "RenderableType") -> None: 

49 self._renderables.append(renderable) 

50 

51 def __iter__(self) -> Iterable["RenderableType"]: 

52 return iter(self._renderables) 

53 

54 

55class Lines: 

56 """A list subclass which can render to the console.""" 

57 

58 def __init__(self, lines: Iterable["Text"] = ()) -> None: 

59 self._lines: list["Text"] = list(lines) 

60 

61 def __repr__(self) -> str: 

62 return f"Lines({self._lines!r})" 

63 

64 def __iter__(self) -> Iterator["Text"]: 

65 return iter(self._lines) 

66 

67 @overload 

68 def __getitem__(self, index: int) -> "Text": ... 

69 

70 @overload 

71 def __getitem__(self, index: slice) -> list["Text"]: ... 

72 

73 def __getitem__(self, index: slice | int) -> "Text | list[Text]": 

74 return self._lines[index] 

75 

76 def __setitem__(self, index: int, value: "Text") -> "Lines": 

77 self._lines[index] = value 

78 return self 

79 

80 def __len__(self) -> int: 

81 return self._lines.__len__() 

82 

83 def __rich_console__( 

84 self, console: "Console", options: "ConsoleOptions" 

85 ) -> "RenderResult": 

86 """Console render method to insert line-breaks.""" 

87 yield from self._lines 

88 

89 def append(self, line: "Text") -> None: 

90 self._lines.append(line) 

91 

92 def extend(self, lines: Iterable["Text"]) -> None: 

93 self._lines.extend(lines) 

94 

95 def pop(self, index: int = -1) -> "Text": 

96 return self._lines.pop(index) 

97 

98 def justify( 

99 self, 

100 console: "Console", 

101 width: int, 

102 justify: "JustifyMethod" = "left", 

103 overflow: "OverflowMethod" = "fold", 

104 ) -> None: 

105 """Justify and overflow text to a given width. 

106 

107 Args: 

108 console (Console): Console instance. 

109 width (int): Number of cells available per line. 

110 justify (str, optional): Default justify method for text: "left", "center", "full" or "right". Defaults to "left". 

111 overflow (str, optional): Default overflow for text: "crop", "fold", or "ellipsis". Defaults to "fold". 

112 

113 """ 

114 from .text import Text 

115 

116 if justify == "left": 

117 for line in self._lines: 

118 line.truncate(width, overflow=overflow, pad=True) 

119 elif justify == "center": 

120 for line in self._lines: 

121 line.rstrip() 

122 line.truncate(width, overflow=overflow) 

123 line.pad_left((width - cell_len(line.plain)) // 2) 

124 line.pad_right(width - cell_len(line.plain)) 

125 elif justify == "right": 

126 for line in self._lines: 

127 line.rstrip() 

128 line.truncate(width, overflow=overflow) 

129 line.pad_left(width - cell_len(line.plain)) 

130 elif justify == "full": 

131 for line_index, line in enumerate(self._lines): 

132 if line_index == len(self._lines) - 1: 

133 break 

134 words = line.split(" ") 

135 words_size = sum(cell_len(word.plain) for word in words) 

136 num_spaces = len(words) - 1 

137 spaces = [1 for _ in range(num_spaces)] 

138 index = 0 

139 if spaces: 

140 while words_size + num_spaces < width: 

141 spaces[len(spaces) - index - 1] += 1 

142 num_spaces += 1 

143 index = (index + 1) % len(spaces) 

144 tokens: list[Text] = [] 

145 for index, (word, next_word) in enumerate( 

146 zip_longest(words, words[1:]) 

147 ): 

148 tokens.append(word) 

149 if index < len(spaces): 

150 style = word.get_style_at_offset(console, -1) 

151 next_style = next_word.get_style_at_offset(console, 0) 

152 space_style = style if style == next_style else line.style 

153 tokens.append(Text(" " * spaces[index], style=space_style)) 

154 self[line_index] = Text("").join(tokens)