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] Spaces at the end lines when using Markdown #3154

Closed
2 tasks done
samuelcolvin opened this issue Oct 13, 2023 · 3 comments
Closed
2 tasks done

[BUG] Spaces at the end lines when using Markdown #3154

samuelcolvin opened this issue Oct 13, 2023 · 3 comments

Comments

@samuelcolvin
Copy link

Describe the bug

Running the following code prints out nice pretty markdown, which is great 👍 .

The only problem is that when I copy the code from the terminal, every line is right padded with spaces to the full width of the terminal, there's also one space to the left of the code.

import rich
from rich.markdown import Markdown

markdown = """\
This is some code:

```sql
SELECT
  toStartOfHour(event_time) AS Event_Hour,
  COUNT() AS Event_Count
FROM events
WHERE event_time >= now() - INTERVAL 1 DAY
GROUP BY Event_Hour
ORDER BY Event_Hour DESC;
```
"""

rich.print(Markdown(markdown))

Hard to show that output here, so here's a screenshot from pycharm showing the spaces (as dots)
Copied SQL:
image

It would be great if there was a way to:

  1. Avoid the space to the left of the code
  2. Avoid adding spaces to the of the lines.

Platform

Click to expand

What platform (Win/Linux/Mac) are you running on? What terminal software are you using? Mac, I think I saw the same on Ubuntu.

I may ask you to copy and paste the output of the following commands. It may save some time if you do it now.

If you're using Rich in a terminal:

python -m rich.diagnose
pip freeze | grep rich
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=122 ColorSystem.EIGHT_BIT>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = '256'                                                         │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 55                                                            │
│    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=122, height=55),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=122,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=55,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=122, height=55)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 122                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭──────── Environment Variables ────────╮
│ {                                     │
│     'TERM': 'xterm-256color',         │
│     'COLORTERM': None,                │
│     'CLICOLOR': None,                 │
│     'NO_COLOR': None,                 │
│     'TERM_PROGRAM': 'Apple_Terminal', │
│     'COLUMNS': None,                  │
│     'LINES': None,                    │
│     'JUPYTER_COLUMNS': None,          │
│     'JUPYTER_LINES': None,            │
│     'JPY_PARENT_PID': None,           │
│     'VSCODE_VERBOSE_LOGGING': None    │
│ }                                     │
╰───────────────────────────────────────╯
platform="Darwin"
rich==13.6.0

==

@willmcgugan
Copy link
Collaborator

willmcgugan commented Oct 13, 2023

There isn't a parameter to control that, but you can customize how the code fence is rendered. Something like this:

import rich
from rich.markdown import Markdown


markdown = """\
This is some code:

```sql
SELECT
  toStartOfHour(event_time) AS Event_Hour,
  COUNT() AS Event_Count
FROM events
WHERE event_time >= now() - INTERVAL 1 DAY
GROUP BY Event_Hour
ORDER BY Event_Hour DESC;
```
"""

from rich.markdown import CodeBlock
from rich.syntax import Syntax


class SimpleCodeBlock(CodeBlock):
    def __rich_console__(self, console, options):
        code = str(self.text).rstrip()
        syntax = Syntax(
            code,
            self.lexer_name,
            theme="ansi_light",
            word_wrap=True,
            background_color="default",
        )
        yield syntax


Markdown.elements["fence"] = SimpleCodeBlock

rich.print(Markdown(markdown))

But that doesn't guarantee it will be cut and pastable. It will still hard wrap if the code is wider than the terminal.

@samuelcolvin
Copy link
Author

Thanks so much, I'll try that.

hard wrapping hasn't been a problem so far.

I've found myself using aicli (which uses rich markdown to print the response) more and more during development, hence needing to copy the code from markdown frequently.

@github-actions
Copy link

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

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

No branches or pull requests

2 participants