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

Recognise shebangs #19

Open
jalanb opened this issue Jun 6, 2023 · 1 comment
Open

Recognise shebangs #19

jalanb opened this issue Jun 6, 2023 · 1 comment

Comments

@jalanb
Copy link

jalanb commented Jun 6, 2023

Syntax highlighting seems to br triggered by file types, i.e. file extensions. So that fred.py is highligghted as Python syntax. Unfortunately this means that a file called just fred, which is executable and has #! /usr/bin/env python on the first line is not recognised, hence not highlighted.

@juftin
Copy link
Owner

juftin commented Jun 7, 2023

Oh this is a good idea. I'm using the rich.syntax.Syntax.guess_lexer method from Rich to handle this currently since the Syntax class is handling almost everything else. In turn it uses pygments to handle it.

https://rich.readthedocs.io/en/stable/reference/syntax.html#rich.syntax.Syntax.guess_lexer

browsr/browsr/browsr.py

Lines 175 to 184 in 26a1bf3

code = "\n".join(code_lines[:1000])
lexer = Syntax.guess_lexer(str(document), code=code)
return Syntax(
code=code,
lexer=lexer,
line_numbers=self.linenos,
word_wrap=False,
indent_guides=False,
theme=self.rich_themes[self.theme_index],
)

I wonder if checking for a shebang would be a welcome addition to the upstream libraries too.

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

No branches or pull requests

2 participants