diff --git a/rich/_inspect.py b/rich/_inspect.py index bb88946150..af44034070 100644 --- a/rich/_inspect.py +++ b/rich/_inspect.py @@ -5,7 +5,7 @@ from typing import Any, Iterable, Optional, Tuple from .console import Group, RenderableType -from .control import make_control_codes_readable +from .control import escape_control_codes from .highlighter import ReprHighlighter from .jupyter import JupyterMixin from .panel import Panel @@ -212,10 +212,22 @@ def safe_getattr(attr_name: str) -> Tuple[Any, Any]: ) def _get_formatted_doc(self, object_: Any) -> Optional[str]: + """ + Extract the docstring of an object, process it and returns it. + The processing consists in cleaning up the doctring's indentation, + taking only its 1st paragraph if `self.help` is not True, + and escape its control codes. + + Args: + object_ (Any): the object to get the docstring from. + + Returns: + Optional[str]: the processed docstring, or None if no docstring was found. + """ docs = getdoc(object_) if docs is None: return None docs = cleandoc(docs).strip() if not self.help: docs = _first_paragraph(docs) - return make_control_codes_readable(docs) + return escape_control_codes(docs) diff --git a/rich/control.py b/rich/control.py index 8d56324ae7..a8a912553a 100644 --- a/rich/control.py +++ b/rich/control.py @@ -23,7 +23,7 @@ _codepoint: None for _codepoint in STRIP_CONTROL_CODES } -_CONTROL_MAKE_READABLE_TRANSLATE: Final = { +CONTROL_ESCAPE: Final = { 7: "\\a", 8: "\\b", 11: "\\v", @@ -198,18 +198,18 @@ def strip_control_codes( return text.translate(_translate_table) -def make_control_codes_readable( +def escape_control_codes( text: str, - _translate_table: Dict[int, str] = _CONTROL_MAKE_READABLE_TRANSLATE, + _translate_table: Dict[int, str] = CONTROL_ESCAPE, ) -> str: - """Replace control codes with their "readable" equivalent in the given text. + """Replace control codes with their "escaped" equivalent in the given text. (e.g. "\b" becomes "\\b") Args: - text (str): A string possibly contain control codes. + text (str): A string possibly containing control codes. Returns: - str: String with control codes replaced with a readable version. + str: String with control codes replaced with their escaped version. """ return text.translate(_translate_table) diff --git a/tests/test_control.py b/tests/test_control.py index aec42ce033..d2704e9808 100644 --- a/tests/test_control.py +++ b/tests/test_control.py @@ -1,4 +1,4 @@ -from rich.control import Control, make_control_codes_readable, strip_control_codes +from rich.control import Control, escape_control_codes, strip_control_codes from rich.segment import ControlType, Segment @@ -13,13 +13,10 @@ def test_strip_control_codes(): assert strip_control_codes("Fear is the mind killer") == "Fear is the mind killer" -def test_make_control_codes_readable(): - assert make_control_codes_readable("") == "" - assert make_control_codes_readable("foo\rbar") == "foo\\rbar" - assert ( - make_control_codes_readable("Fear is the mind killer") - == "Fear is the mind killer" - ) +def test_escape_control_codes(): + assert escape_control_codes("") == "" + assert escape_control_codes("foo\rbar") == "foo\\rbar" + assert escape_control_codes("Fear is the mind killer") == "Fear is the mind killer" def test_control_move_to():