Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Escape vs trailing backslash #2987

Closed
kfsone opened this issue Jun 2, 2023 · 2 comments · Fixed by #3063
Closed

[BUG] Escape vs trailing backslash #2987

kfsone opened this issue Jun 2, 2023 · 2 comments · Fixed by #3063

Comments

@kfsone
Copy link

kfsone commented Jun 2, 2023

  • [☑️ ] 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>
@github-actions
Copy link

github-actions bot commented Jun 2, 2023

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@github-actions
Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant