[BUG] Syntax.highlight adds newline
See original GitHub issueDescribe the bug
Highlighting some code that ends with a newline produces the same Text
as without a newline:
from rich.syntax import Syntax
Syntax("", "python").highlight("print('hello')")
# shows: <text "print('hello')\n" ...
Syntax("", "python").highlight("print('hello')\n")
# shows: <text "print('hello')\n" ...
Platform
Linux, LXTerminal
$ python -m rich.diagnose
โญโโโโโโโโโโโโโโโโโโโโโโโโโ <class 'rich.console.Console'> โโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ A high level console interface. โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ <console width=158 ColorSystem.TRUECOLOR> โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ color_system = 'truecolor' โ
โ encoding = 'utf-8' โ
โ file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> โ
โ height = 41 โ
โ 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=158, height=41), โ
โ legacy_windows=False, โ
โ min_width=1, โ
โ max_width=158, โ
โ is_terminal=True, โ
โ encoding='utf-8', โ
โ max_height=41, โ
โ justify=None, โ
โ overflow=None, โ
โ no_wrap=False, โ
โ highlight=None, โ
โ markup=None, โ
โ height=None โ
โ ) โ
โ quiet = False โ
โ record = False โ
โ safe_box = True โ
โ size = ConsoleDimensions(width=158, height=41) โ
โ soft_wrap = False โ
โ stderr = False โ
โ style = None โ
โ tab_size = 8 โ
โ width = 158 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
$ python -m rich._windows
platform="Linux"
WindowsConsoleFeatures(vt=False, truecolor=False)
$ pip freeze | grep rich
rich==10.16.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
alias including a newline is not highlighted anymore #677
With the config alias foobar='echo foo; echo bar' foobar at the head of the prompt should be highlighted like a command but not...
Read more >syntax highlighting breaks on escaped newlines / characters
It shows syntax error just because a line is broken into multiple lines, with newline's being escaped. I have noticed that syntax highlighting...
Read more >react-syntax-highlighter new line in Clojure code sample
The only solution that I've found is to do it this way: <SyntaxHighlighter language='clojure' style={darcula} showLineNumbers={true}ย ...
Read more >[kate] [Bug 448126] New: Syntax-Highlight chokes on line-break in ...
Bug ID: 448126 Summary: Syntax-Highlight chokes on line-break in bash ... 1. open attached bash script in kate and enjoy the syntax-highlight 2....
Read more >Syntax highlighting is applied to "No newline at end of ... - Jira
Create a file in a language for which syntax highlighting is available in SourceTree (I experience this bug with .sql files) and don't...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Keep me posted re progress. ๐
I was just about to put in a feature request related to this.
The โproblemโ noted by @davidbrochart is caused by https://github.com/Textualize/rich/blob/5f0219786bbaf994061011df087602d5edff5d66/rich/syntax.py#L522 Having an optional parameter to disable the addition of a newline character would be an easy way to take care of the original issue.
I will file a separate issue for the feature request.