mirror of
https://github.com/Textualize/rich.git
synced 2025-08-04 18:18:22 +00:00
[inspect] Address PR feedback on ASCII special characters' replacement with their escaped version
This commit is contained in:
parent
236096ea8f
commit
25d0c0e898
3 changed files with 25 additions and 16 deletions
|
@ -5,7 +5,7 @@ from inspect import cleandoc, getdoc, getfile, isclass, ismodule, signature
|
|||
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 @@ class Inspect(JupyterMixin):
|
|||
)
|
||||
|
||||
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)
|
||||
|
|
|
@ -23,7 +23,7 @@ _CONTROL_STRIP_TRANSLATE: Final = {
|
|||
_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)
|
||||
|
||||
|
|
|
@ -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():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue