Bat: Wrapping causes problems with ANSI codes

Created on 16 May 2018  路  5Comments  路  Source: sharkdp/bat

The current implementation of character-level wrapping counts incoming characters and adds a new line if the length exceeds the terminal width.

This causes problems if the incoming data stream contains ANSI characters, for example:

image

There are several problems:

  • The lines are wrapped early due to the hidden ANSI chararacters
  • Some line-wraps occur in ANSI codes, causing artifacts like 8;2;255;255;255m] to be displayed.

@eth-p: I am mentioning you just for your information.

bug

All 5 comments

@sharkdp Good catch, I never considered that. I can think of two ways to fix this:

  1. Strip the ANSI codes with console::strip_ansi_codes.
  2. Attempt to parse the ANSI codes, making sure to not add them as part of the width counter.

I would definitely like to support incoming ANSI codes, just like cat or less. This will be useful if people try to alias cat=bat or use bat as a general-purpose pager, etc. - so stripping ANSI codes is not really an option for me.

Also, I would really like to avoid having to implement a ANSI-code parser within bat. The console trait seems to have a few useful functions, for example: measure_text_width. Maybe we could utilize those... but I'm not sure if this is enough.

Perhaps console::AnsiCodeIterator would work? I'll play around with it and see what I can do.

There is also truncate_str, which seems really helpful!

Unfortunately, truncate_str doesn't allow me to get the text that was truncated. The source code for it used AnsiCodeIterator under the hood, so I ended up just borrowing the logic from that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sharkdp picture sharkdp  路  3Comments

jkaan picture jkaan  路  3Comments

x70b1 picture x70b1  路  3Comments

scalp42 picture scalp42  路  3Comments

mjlbach picture mjlbach  路  3Comments