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

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

68 statements  

1from .align import AlignMethod 

2from .cells import cell_len, set_cell_size 

3from .console import Console, ConsoleOptions, RenderResult 

4from .jupyter import JupyterMixin 

5from .measure import Measurement 

6from .style import Style 

7from .text import Text 

8 

9 

10class Rule(JupyterMixin): 

11 """A console renderable to draw a horizontal rule (line). 

12 

13 Args: 

14 title (str | Text, optional): Text to render in the rule. Defaults to "". 

15 characters (str, optional): Character(s) used to draw the line. Defaults to "─". 

16 style (StyleType, optional): Style of Rule. Defaults to "rule.line". 

17 end (str, optional): Character at end of Rule. defaults to "\\\\n" 

18 align (str, optional): How to align the title, one of "left", "center", or "right". Defaults to "center". 

19 """ 

20 

21 def __init__( 

22 self, 

23 title: str | Text = "", 

24 *, 

25 characters: str = "─", 

26 style: str | Style = "rule.line", 

27 end: str = "\n", 

28 align: AlignMethod = "center", 

29 ) -> None: 

30 if cell_len(characters) < 1: 

31 raise ValueError( 

32 "'characters' argument must have a cell width of at least 1" 

33 ) 

34 if align not in ("left", "center", "right"): 

35 raise ValueError( 

36 f'invalid value for align, expected "left", "center", "right" (not {align!r})' 

37 ) 

38 self.title = title 

39 self.characters = characters 

40 self.style = style 

41 self.end = end 

42 self.align = align 

43 

44 def __repr__(self) -> str: 

45 return f"Rule({self.title!r}, {self.characters!r})" 

46 

47 def __rich_console__( 

48 self, console: Console, options: ConsoleOptions 

49 ) -> RenderResult: 

50 width = options.max_width 

51 

52 characters = ( 

53 "-" 

54 if (options.ascii_only and not self.characters.isascii()) 

55 else self.characters 

56 ) 

57 

58 chars_len = cell_len(characters) 

59 if not self.title: 

60 yield self._rule_line(chars_len, width) 

61 return 

62 

63 if isinstance(self.title, Text): 

64 title_text = self.title 

65 else: 

66 title_text = console.render_str(self.title, style="rule.text") 

67 

68 title_text.plain = title_text.plain.replace("\n", " ") 

69 title_text.expand_tabs() 

70 

71 required_space = 4 if self.align == "center" else 2 

72 truncate_width = max(0, width - required_space) 

73 if not truncate_width: 

74 yield self._rule_line(chars_len, width) 

75 return 

76 

77 rule_text = Text(end=self.end) 

78 if self.align == "center": 

79 title_text.truncate(truncate_width, overflow="ellipsis") 

80 side_width = (width - cell_len(title_text.plain)) // 2 

81 left = Text(characters * (side_width // chars_len + 1)) 

82 left.truncate(side_width - 1) 

83 right_length = width - cell_len(left.plain) - cell_len(title_text.plain) 

84 right = Text(characters * (side_width // chars_len + 1)) 

85 right.truncate(right_length) 

86 rule_text.append(left.plain + " ", self.style) 

87 rule_text.append(title_text) 

88 rule_text.append(" " + right.plain, self.style) 

89 elif self.align == "left": 

90 title_text.truncate(truncate_width, overflow="ellipsis") 

91 rule_text.append(title_text) 

92 rule_text.append(" ") 

93 rule_text.append(characters * (width - rule_text.cell_len), self.style) 

94 elif self.align == "right": 

95 title_text.truncate(truncate_width, overflow="ellipsis") 

96 rule_text.append(characters * (width - title_text.cell_len - 1), self.style) 

97 rule_text.append(" ") 

98 rule_text.append(title_text) 

99 

100 rule_text.plain = set_cell_size(rule_text.plain, width) 

101 yield rule_text 

102 

103 def _rule_line(self, chars_len: int, width: int) -> Text: 

104 rule_text = Text(self.characters * ((width // chars_len) + 1), self.style) 

105 rule_text.truncate(width) 

106 rule_text.plain = set_cell_size(rule_text.plain, width) 

107 return rule_text 

108 

109 def __rich_measure__( 

110 self, console: Console, options: ConsoleOptions 

111 ) -> Measurement: 

112 return Measurement(1, 1) 

113 

114 

115if __name__ == "__main__": # pragma: no cover 

116 import sys 

117 

118 from rich.console import Console 

119 

120 try: 

121 text = sys.argv[1] 

122 except IndexError: 

123 text = "Hello, World" 

124 console = Console() 

125 console.print(Rule(title=text)) 

126 

127 console = Console() 

128 console.print(Rule("foo"), width=4)