Add option to remove ANSI escape sequences from bat's input.#2999
Merged
eth-p merged 5 commits intosharkdp:masterfrom Jun 18, 2024
Merged
Add option to remove ANSI escape sequences from bat's input.#2999eth-p merged 5 commits intosharkdp:masterfrom
eth-p merged 5 commits intosharkdp:masterfrom
Conversation
3f81db6 to
87b0c2e
Compare
87b0c2e to
7724541
Compare
7724541 to
a678878
Compare
Collaborator
Author
|
Rebased to c264ecd and fixed some clippy warnings. |
keith-hall
approved these changes
Jun 17, 2024
Collaborator
keith-hall
left a comment
There was a problem hiding this comment.
Nice work, I'm sure lots of users will find this extra functionality useful
| 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 |
Collaborator
There was a problem hiding this comment.
Interesting idea to add a hint about version in the readme, especially for features not part of a release yet 🙂
Collaborator
Author
There was a problem hiding this comment.
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 :)
When using `auto`, escape sequences will be stripped unless printing plain text.
a678878 to
90dfa7f
Compare
tmeijn
pushed a commit
to tmeijn/dotfiles
that referenced
this pull request
Jan 8, 2025
This MR contains the following updates: | Package | Update | Change | |---|---|---| | [sharkdp/bat](https://github.com/sharkdp/bat) | minor | `v0.24.0` -> `v0.25.0` | MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot). **Proposed changes to behavior should be submitted there as MRs.** --- ### Release Notes <details> <summary>sharkdp/bat (sharkdp/bat)</summary> ### [`v0.25.0`](https://github.com/sharkdp/bat/blob/HEAD/CHANGELOG.md#v0250) [Compare Source](sharkdp/bat@v0.24.0...v0.25.0) #### Features - Set terminal title to file names when Paging is not Paging::Never [#​2807](sharkdp/bat#2807) ([@​Oliver-Looney](https://github.com/Oliver-Looney)) - `bat --squeeze-blank`/`bat -s` will now squeeze consecutive empty lines, see [#​1441](sharkdp/bat#1441) ([@​eth-p](https://github.com/eth-p)) and [#​2665](sharkdp/bat#2665) ([@​einfachIrgendwer0815](https://github.com/einfachIrgendwer0815)) - `bat --squeeze-limit` to set the maximum number of empty consecutive when using `--squeeze-blank`, see [#​1441](sharkdp/bat#1441) ([@​eth-p](https://github.com/eth-p)) and [#​2665](sharkdp/bat#2665) ([@​einfachIrgendwer0815](https://github.com/einfachIrgendwer0815)) - `PrettyPrinter::squeeze_empty_lines` to support line squeezing for bat as a library, see [#​1441](sharkdp/bat#1441) ([@​eth-p](https://github.com/eth-p)) and [#​2665](sharkdp/bat#2665) ([@​einfachIrgendwer0815](https://github.com/einfachIrgendwer0815)) - Syntax highlighting for JavaScript files that start with `#!/usr/bin/env bun` [#​2913](sharkdp/bat#2913) ([@​sharunkumar](https://github.com/sharunkumar)) - `bat --strip-ansi={never,always,auto}` to remove ANSI escape sequences from bat's input, see [#​2999](sharkdp/bat#2999) ([@​eth-p](https://github.com/eth-p)) - Add or remove individual style components without replacing all styles [#​2929](sharkdp/bat#2929) ([@​eth-p](https://github.com/eth-p)) - Automatically choose theme based on the terminal's color scheme, see [#​2896](sharkdp/bat#2896) ([@​bash](https://github.com/bash)) - Add option `--binary=as-text` for printing binary content, see issue [#​2974](sharkdp/bat#2974) and MR [#​2976](sharkdp/bat#2976) ([@​einfachIrgendwer0815](https://github.com/einfachIrgendwer0815)) - Make shell completions available via `--completion <shell>`, see issue [#​2057](sharkdp/bat#2057) and MR [#​3126](sharkdp/bat#3126) ([@​einfachIrgendwer0815](https://github.com/einfachIrgendwer0815)) - Syntax highlighting for puppet code blocks within Markdown files, see [#​3152](sharkdp/bat#3152) ([@​liliwilson](https://github.com/liliwilson)) #### Bugfixes - Fix long file name wrapping in header, see [#​2835](sharkdp/bat#2835) ([@​FilipRazek](https://github.com/FilipRazek)) - Fix `NO_COLOR` support, see [#​2767](sharkdp/bat#2767) ([@​acuteenvy](https://github.com/acuteenvy)) - Fix handling of inputs with OSC ANSI escape sequences, see [#​2541](sharkdp/bat#2541) and [#​2544](sharkdp/bat#2544) ([@​eth-p](https://github.com/eth-p)) - Fix handling of inputs with combined ANSI color and attribute sequences, see [#​2185](sharkdp/bat#2185) and [#​2856](sharkdp/bat#2856) ([@​eth-p](https://github.com/eth-p)) - Fix panel width when line 10000 wraps, see [#​2854](sharkdp/bat#2854) ([@​eth-p](https://github.com/eth-p)) - Fix compile issue of `time` dependency caused by standard library regression [#​3045](sharkdp/bat#3045) ([@​cyqsimon](https://github.com/cyqsimon)) - Fix override behavior of --plain and --paging, see issue [#​2731](sharkdp/bat#2731) and MR [#​3108](sharkdp/bat#3108) ([@​einfachIrgendwer0815](https://github.com/einfachIrgendwer0815)) - Fix bugs in `$LESSOPEN` support, see [#​2805](sharkdp/bat#2805) ([@​Anomalocaridid](https://github.com/Anomalocaridid)) #### Other - Upgrade to Rust 2021 edition [#​2748](sharkdp/bat#2748) ([@​cyqsimon](https://github.com/cyqsimon)) - Refactor and cleanup build script [#​2756](sharkdp/bat#2756) ([@​cyqsimon](https://github.com/cyqsimon)) - Checks changelog has been written to for MRs in CI [#​2766](sharkdp/bat#2766) ([@​cyqsimon](https://github.com/cyqsimon)) - Use GitHub API to get correct MR submitter [#​2791](sharkdp/bat#2791) ([@​cyqsimon](https://github.com/cyqsimon)) - Minor benchmark script improvements [#​2768](sharkdp/bat#2768) ([@​cyqsimon](https://github.com/cyqsimon)) - Update Arch Linux package URL in README files [#​2779](sharkdp/bat#2779) ([@​brunobell](https://github.com/brunobell)) - Update and improve `zsh` completion, see [#​2772](sharkdp/bat#2772) ([@​okapia](https://github.com/okapia)) - More extensible syntax mapping mechanism [#​2755](sharkdp/bat#2755) ([@​cyqsimon](https://github.com/cyqsimon)) - Use proper Architecture for Debian packages built for musl, see [#​2811](sharkdp/bat#2811) ([@​Enselic](https://github.com/Enselic)) - Pull in fix for unsafe-libyaml security advisory, see [#​2812](sharkdp/bat#2812) ([@​dtolnay](https://github.com/dtolnay)) - Update git-version dependency to use Syn v2, see [#​2816](sharkdp/bat#2816) ([@​dtolnay](https://github.com/dtolnay)) - Update git2 dependency to v0.18.2, see [#​2852](sharkdp/bat#2852) ([@​eth-p](https://github.com/eth-p)) - Improve performance when color output disabled, see [#​2397](sharkdp/bat#2397) and [#​2857](sharkdp/bat#2857) ([@​eth-p](https://github.com/eth-p)) - Relax syntax mapping rule restrictions to allow brace expansion [#​2865](sharkdp/bat#2865) ([@​cyqsimon](https://github.com/cyqsimon)) - Apply clippy fixes [#​2864](sharkdp/bat#2864) ([@​cyqsimon](https://github.com/cyqsimon)) - Faster startup by offloading glob matcher building to a worker thread [#​2868](sharkdp/bat#2868) ([@​cyqsimon](https://github.com/cyqsimon)) - Display which theme is the default one in basic output (no colors), see [#​2937](sharkdp/bat#2937) ([@​sblondon](https://github.com/sblondon)) - Display which theme is the default one in colored output, see [#​2838](sharkdp/bat#2838) ([@​sblondon](https://github.com/sblondon)) - Add aarch64-apple-darwin ("Apple Silicon") binary tarballs to releases, see [#​2967](sharkdp/bat#2967) ([@​someposer](https://github.com/someposer)) - Update the Lisp syntax, see [#​2970](sharkdp/bat#2970) ([@​ccqpein](https://github.com/ccqpein)) - Use bat's ANSI iterator during tab expansion, see [#​2998](sharkdp/bat#2998) ([@​eth-p](https://github.com/eth-p)) - Support 'statically linked binary' for aarch64 in 'Release' page, see [#​2992](sharkdp/bat#2992) ([@​tzq0301](https://github.com/tzq0301)) - Update options in shell completions and the man page of `bat`, see [#​2995](sharkdp/bat#2995) ([@​akinomyoga](https://github.com/akinomyoga)) - Update nix dev-dependency to v0.29.0, see [#​3112](sharkdp/bat#3112) ([@​decathorpe](https://github.com/decathorpe)) - Bump MSRV to [1.74](https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html), see [#​3154](sharkdp/bat#3154) ([@​keith-hall](https://github.com/keith-hall)) - Update clircle dependency to remove winapi transitive dependency, see [#​3113](sharkdp/bat#3113) ([@​niklasmohrin](https://github.com/niklasmohrin)) #### Syntaxes - `cmd-help`: scope subcommands followed by other terms, and other misc improvements, see [#​2819](sharkdp/bat#2819) ([@​victor-gp](https://github.com/victor-gp)) - Upgrade JQ syntax, see [#​2820](sharkdp/bat#2820) ([@​dependabot](https://github.com/dependabot)\[bot]) - Add syntax mapping for quadman quadlets [#​2866](sharkdp/bat#2866) ([@​cyqsimon](https://github.com/cyqsimon)) - Map containers .conf files to TOML syntax [#​2867](sharkdp/bat#2867) ([@​cyqsimon](https://github.com/cyqsimon)) - Associate `.xsh` files with `xonsh` syntax that is Python, see [#​2840](sharkdp/bat#2840) ([@​anki-code](https://github.com/anki-code)) - Associate JSON with Comments `.jsonc` with `json` syntax, see [#​2795](sharkdp/bat#2795) ([@​mxaddict](https://github.com/mxaddict)) - Associate JSON-LD `.jsonld` files with `json` syntax, see [#​3037](sharkdp/bat#3037) ([@​vorburger](https://github.com/vorburger)) - Associate `.textproto` files with `ProtoBuf` syntax, see [#​3038](sharkdp/bat#3038) ([@​vorburger](https://github.com/vorburger)) - Associate GeoJSON `.geojson` files with `json` syntax, see [#​3084](sharkdp/bat#3084) ([@​mvaaltola](https://github.com/mvaaltola)) - Associate `.aws/{config,credentials}`, see [#​2795](sharkdp/bat#2795) ([@​mxaddict](https://github.com/mxaddict)) - Associate Wireguard config `/etc/wireguard/*.conf`, see [#​2874](sharkdp/bat#2874) ([@​cyqsimon](https://github.com/cyqsimon)) - Add support for [CFML](https://www.adobe.com/products/coldfusion-family.html), see [#​3031](sharkdp/bat#3031) ([@​brenton-at-pieces](https://github.com/brenton-at-pieces)) - Map `*.mkd` files to `Markdown` syntax, see issue [#​3060](sharkdp/bat#3060) and MR [#​3061](sharkdp/bat#3061) ([@​einfachIrgendwer0815](https://github.com/einfachIrgendwer0815)) - Add syntax mapping for CITATION.cff, see [#​3103](sharkdp/bat#3103) ([@​Ugzuzg](https://github.com/Ugzuzg)) - Add syntax mapping for kubernetes config files [#​3049](sharkdp/bat#3049) ([@​cyqsimon](https://github.com/cyqsimon)) - Adds support for pipe delimiter for CSV [#​3115](sharkdp/bat#3115) ([@​pratik-m](https://github.com/pratik-m)) - Add syntax mapping for `/etc/pacman.conf` [#​2961](sharkdp/bat#2961) ([@​cyqsimon](https://github.com/cyqsimon)) - Associate `uv.lock` with `TOML` syntax, see [#​3132](sharkdp/bat#3132) ([@​fepegar](https://github.com/fepegar)) #### Themes - Patched/improved themes for better Manpage syntax highlighting support, see [#​2994](sharkdp/bat#2994) ([@​keith-hall](https://github.com/keith-hall)). #### `bat` as a library - Changes to `syntax_mapping::SyntaxMapping` [#​2755](sharkdp/bat#2755) ([@​cyqsimon](https://github.com/cyqsimon)) - `SyntaxMapping::get_syntax_for` is now correctly public - \[BREAKING] `SyntaxMapping::{empty,builtin}` are removed; use `SyntaxMapping::new` instead - \[BREAKING] `SyntaxMapping::mappings` is replaced by `SyntaxMapping::{builtin,custom,all}_mappings` - Make `Controller::run_with_error_handler`'s error handler `FnMut`, see [#​2831](sharkdp/bat#2831) ([@​rhysd](https://github.com/rhysd)) - Improve compile time by 20%, see [#​2815](sharkdp/bat#2815) ([@​dtolnay](https://github.com/dtolnay)) - Add `theme::theme` for choosing an appropriate theme based on the terminal's color scheme, see [#​2896](sharkdp/bat#2896) ([@​bash](https://github.com/bash)) - \[BREAKING] Remove `HighlightingAssets::default_theme`. Use `theme::default_theme` instead. - Add `PrettyPrinter::print_with_writer` for custom output destinations, see [#​3070](sharkdp/bat#3070) ([@​kojix2](https://github.com/kojix2)) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS45MS40IiwidXBkYXRlZEluVmVyIjoiMzkuOTEuNCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
This was referenced Jan 16, 2025
Closed
This was referenced Jan 16, 2025
Open
Closed
Merged
Closed
Closed
Open
Open
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces a new
--strip-ansioption tobat.This option tells
batwhether 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
No significant difference.
Enabled, Regular File
A minor reduction in performance.
Disabled, Pre-highlighted File
No significant difference.
Enabled, Pre-highlighted File
It's certainly an improvement.
Auto Mode
The detection part of
--strip-ansi=autocauses 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=autorelies on detecting the syntax in order to determine if a file is plain text or not (and thus whether to strip the ANSI sequences).TODO
SimplePrinter(maybe. thoughts?)Wait for Update bat's tab expansion preprocessor to use bat's ANSI escape sequence iterator. #2998.It contains the changes made in b4fe182, which this PR depends on.
Updatelong_helpintegration testWrite testsBenchmarksRuncargo fmtAllow--strip-ansito be passed multiple times (so it can be set to auto in config and overridden on the cli)