Method: rich.console.Console.export_text
Calls: 17, Exceptions: 0, Paths: 2Back
Path 1: 16 calls (0.94)
True (16)
False (16)
'foo\n' (2) '┌───┬───┐\n│ 1 │ 2 │\n│ 3 │ 4 │\n└───┴───┘\n' (2) ' ╷ \n 1 │ 2 \n 3 │ 4 \n ╵ \n' (2) 'ABC\nHello\n' (1) 'hello world\n' (...
1def export_text(self, *, clear: bool = True, styles: bool = False) -> str:
2 """Generate text from console contents (requires record=True argument in constructor).
3
4 Args:
5 clear (bool, optional): Clear record buffer after exporting. Defaults to ``True``.
6 styles (bool, optional): If ``True``, ansi escape codes will be included. ``False`` for plain text.
7 Defaults to ``False``.
8
9 Returns:
10 str: String containing console contents.
11
12 """
13 assert (
14 self.record
15 ), "To export console contents set record=True in the constructor or instance"
16
17 with self._record_buffer_lock:
18 if styles:
19 text = "".join(
20 (style.render(text) if style else text)
21 for text, style, _ in self._record_buffer
22 )
23 else:
24 text = "".join(
25 segment.text
26 for segment in self._record_buffer
27 if not segment.control
28 )
29 if clear:
30 del self._record_buffer[:]
31 return text
Path 2: 1 calls (0.06)
True (1)
True (1)
'\x1b[1;4mWhere\x1b[0m\x1b[4m there is \x1b[0m\n\x1b[4ma \x1b[0m\x1b[3;4mWill\x1b[0m\x1b[4m, there \x1b[0m\n\x1b[4mis a Way.\x1b[0m\n' (1)
1def export_text(self, *, clear: bool = True, styles: bool = False) -> str:
2 """Generate text from console contents (requires record=True argument in constructor).
3
4 Args:
5 clear (bool, optional): Clear record buffer after exporting. Defaults to ``True``.
6 styles (bool, optional): If ``True``, ansi escape codes will be included. ``False`` for plain text.
7 Defaults to ``False``.
8
9 Returns:
10 str: String containing console contents.
11
12 """
13 assert (
14 self.record
15 ), "To export console contents set record=True in the constructor or instance"
16
17 with self._record_buffer_lock:
18 if styles:
19 text = "".join(
20 (style.render(text) if style else text)
21 for text, style, _ in self._record_buffer
22 )
23 else:
24 text = "".join(
25 segment.text
26 for segment in self._record_buffer
27 if not segment.control
28 )
29 if clear:
30 del self._record_buffer[:]
31 return text