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

When reading from stdin, bat should use --file-name (if present) to pick the syntax highlighter #891

Closed
anntzer opened this issue Mar 26, 2020 · 3 comments · Fixed by #892
Assignees
Labels
feature-request New feature or request
Milestone

Comments

@anntzer
Copy link

anntzer commented Mar 26, 2020

Per discussion at #237 (comment).

When reading from stdin, bat can't perform highlighting as it doesn't know the filename... unless it is given with --file-name. (We could even always use --file-name (if present) to pick the syntax even if the real filename is available, but that may run into issues if that name is inconsistent with the real name.)

@anntzer anntzer added the feature-request New feature or request label Mar 26, 2020
@neuronull
Copy link
Contributor

I would like to implement this.

@sharkdp
Copy link
Owner

sharkdp commented Mar 26, 2020

Thank you for your request.

When reading from stdin, bat can't perform highlighting as it doesn't know the filename... unless it is given with --file-name.

That's not quite correct. bat can also perform syntax detection by reading the first line. This also works for input coming from stdin, for example

echo "<?php\nprint 'hi';?>" | bat # highlighted as PHP
echo "#\!/bin/bash\necho hi" | bat # highlighted as Bash

We should make sure that this still works.

For normal input from files, the filename has precedence. This means that a file named test.cpp will always be highlighted as C++, even if the first line would indicate, that it is something else. I'm actually not sure if that behavior should be changed?

We could even always use --file-name (if present) to pick the syntax even if the real filename is available, but that may run into issues if that name is inconsistent with the real name.

Yes, that should be discussed first.

I would like to implement this.

Sounds great! 👍

@neuronull
Copy link
Contributor

For normal input from files, the filename has precedence. This means that a file named test.cpp will always be highlighted as C++, even if the first line would indicate, that it is something else. I'm actually not sure if that behavior should be changed?

From what I saw experimentally, there seem to be limits to the first-line detection. IMHO it makes sense to prioritize the filename/extension.

neuronull pushed a commit to neuronull/bat that referenced this issue Mar 26, 2020
sharkdp pushed a commit to neuronull/bat that referenced this issue Apr 11, 2020
@sharkdp sharkdp added this to the v0.14 milestone Apr 11, 2020
sharkdp pushed a commit to neuronull/bat that referenced this issue Apr 11, 2020
sharkdp pushed a commit that referenced this issue Apr 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants