Skip to content

[BUG] Escape vs trailing backslash #2987

@kfsone

Description

@kfsone
  • [☑️ ] I've checked docs and closed issues for possible solutions.
  • [☑️ ] I can't find my issue in the FAQ.

Description
Using escape to print something with a trailing slash does not escape the trailing slash:

from rich import print; from rich.markup import escape
value = "C:\\"; print(value)
rich.print(f"[red]{value}[/red]")  # <-- [ gets escaped
rich.print(f"[red]{escape(value)}[/red]")  # <-- [ still gets escaped

outputs

C:\
C:[/red]
C:[/red]

Possible solution

# Previous character is NOT a slash: (?<![\\])
# some number of escaped slashes: (?:[\\][\\])*
# followed by a lone slash at end-of-string: [\\]$
In [40]: _odd_trailing_escape_re = r'(?<![\\])(?:[\\][\\])*[\\]$'
    ...: def escape(text: str, trailing_slash_check = re.compile(_odd_trailing_escape_re, flags=re.S).search) -> str:
    ...:
    ...:   if text.endswith('\\') and trailing_slash_check(text):
    ...:     text += '\\'
    ...:   return text
    ...:

In [41]: escape("C:\\")
Out[41]: 'C:\\\\'

In [42]: escape("C:\\\\")
Out[42]: 'C:\\\\'

In [43]: escape("C:\\\\\\")
Out[43]: 'C:\\\\\\\\'```

**Platform**
Using Rich in a terminal:

Win|PS> python -m rich.diagnose ; pip freeze | sls rich
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface. │
│ │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│ │
│ color_system = 'truecolor' │
│ encoding = 'utf-8' │
│ file = <_io.TextIOWrapper name='' mode='w' encoding='utf-8'> │
│ height = 30 │
│ is_alt_screen = False │
│ is_dumb_terminal = False │
│ is_interactive = True │
│ is_jupyter = False │
│ is_terminal = True │
│ legacy_windows = False │
│ no_color = False │
│ options = ConsoleOptions( │
│ size=ConsoleDimensions(width=118, height=30), │
│ legacy_windows=False, │
│ min_width=1, │
│ max_width=118, │
│ is_terminal=True, │
│ encoding='utf-8', │
│ max_height=30, │
│ justify=None, │
│ overflow=None, │
│ no_wrap=False, │
│ highlight=None, │
│ markup=None, │
│ height=None │
│ ) │
│ quiet = False │
│ record = False │
│ safe_box = True │
│ size = ConsoleDimensions(width=118, height=30) │
│ soft_wrap = False │
│ stderr = False │
│ style = None │
│ tab_size = 8 │
│ width = 118 │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭── <class 'rich._windows.WindowsConsoleFeatures'> ───╮
│ Windows features available. │
│ │
│ ╭─────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=True, truecolor=True) │ │
│ ╰─────────────────────────────────────────────────╯ │
│ │
│ truecolor = True │
│ vt = True │
╰─────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ { │
│ 'TERM': None, │
│ 'COLORTERM': None, │
│ 'CLICOLOR': None, │
│ 'NO_COLOR': None, │
│ 'TERM_PROGRAM': None, │
│ 'COLUMNS': None, │
│ 'LINES': None, │
│ 'JUPYTER_COLUMNS': None, │
│ 'JUPYTER_LINES': None, │
│ 'JPY_PARENT_PID': None, │
│ 'VSCODE_VERBOSE_LOGGING': None │
│ } │
╰────────────────────────────────────╯
platform="Windows"

rich @ file:///home/conda/feedstock_root/build_artifacts/rich-split_1685565049610/work/dist

</details>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions