Skip to content

Commit

Permalink
[inspect] Address PR feedback on ASCII special characters' replacemen…
Browse files Browse the repository at this point in the history
…t with their escaped version
  • Loading branch information
olivierphi committed May 27, 2022
1 parent 74d7807 commit ce0b2ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
16 changes: 14 additions & 2 deletions rich/_inspect.py
Expand Up @@ -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
Expand Down Expand Up @@ -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)
12 changes: 6 additions & 6 deletions rich/control.py
Expand Up @@ -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",
Expand Down Expand Up @@ -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)

Expand Down
13 changes: 5 additions & 8 deletions 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


Expand All @@ -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():
Expand Down

0 comments on commit ce0b2ff

Please sign in to comment.