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

Add option to remove ANSI escape sequences from bat's input. #2999

Merged
merged 5 commits into from
Jun 18, 2024

Conversation

eth-p
Copy link
Collaborator

@eth-p eth-p commented Jun 11, 2024

This pull request introduces a new --strip-ansi option to bat.

This option tells bat whether to strip ANSI/VT escape sequences from the input file. The following values are supported:

  • always: Always strip escape sequences.
  • never: Never strip escape sequences. (default)
  • auto: Strip escape sequences when printing any language that is not "Plain Text".
    This preserves bat's ability to print piped commands (or pre-highlighted files) and keep their existing colors.

Benchmarks

Disabled, Regular File

image

No significant difference.

Enabled, Regular File

image

A minor reduction in performance.

Disabled, Pre-highlighted File

image

No significant difference.

Enabled, Pre-highlighted File

image

It's certainly an improvement.

Auto Mode

image

The detection part of --strip-ansi=auto causes no significant difference when syntax highlighting is used.

However, it does add a few milliseconds of startup time when used with --color=never. This is because --strip-ansi=auto relies on detecting the syntax in order to determine if a file is plain text or not (and thus whether to strip the ANSI sequences).

image

TODO

@eth-p eth-p added feature-request New feature or request rust Pull requests that update Rust code labels Jun 11, 2024
@eth-p eth-p changed the title Add option to strip ANSI escape sequences before syntax highlighting. Add option to remove ANSI escape sequences from bat's input. Jun 11, 2024
@eth-p eth-p force-pushed the strip-ansi-from-input-option branch 4 times, most recently from 3f81db6 to 87b0c2e Compare June 16, 2024 01:05
@eth-p eth-p marked this pull request as ready for review June 16, 2024 01:47
@eth-p eth-p force-pushed the strip-ansi-from-input-option branch from 87b0c2e to 7724541 Compare June 16, 2024 23:48
@eth-p eth-p force-pushed the strip-ansi-from-input-option branch from 7724541 to a678878 Compare June 16, 2024 23:49
@eth-p
Copy link
Collaborator Author

eth-p commented Jun 16, 2024

Rebased to c264ecd and fixed some clippy warnings.

Copy link
Collaborator

@keith-hall keith-hall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, I'm sure lots of users will find this extra functionality useful

src/bin/bat/clap_app.rs Outdated Show resolved Hide resolved
@@ -759,9 +759,14 @@ bat() {

If an input file contains color codes or other ANSI escape sequences or control characters, `bat` will have problems
performing syntax highlighting and text wrapping, and thus the output can become garbled.
When displaying such files it is recommended to disable both syntax highlighting and wrapping by

If your version of `bat` supports the `--strip-ansi=auto` option, it can be used to remove such sequences
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea to add a hint about version in the readme, especially for features not part of a release yet 🙂

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was relevant to the section, and I didn't want to forget to add it later. Had to word it so it didn't seem like it was already available, and that's what I came up with :)

@eth-p eth-p force-pushed the strip-ansi-from-input-option branch from a678878 to 90dfa7f Compare June 18, 2024 01:27
@eth-p eth-p merged commit a7a9727 into sharkdp:master Jun 18, 2024
24 checks passed
@eth-p eth-p deleted the strip-ansi-from-input-option branch June 18, 2024 01:37
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 rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants