jgm/doclayout

Ignore ANSI terminal escape sequences in length calculations

Closed this issue · 4 comments

tarleb commented

Ignoring ANSI escape sequences would allow to layout marked up text in the terminal. Escape sequences for text markup all follow the pattern \ESC[([0-9;]*m.

Initial discussion: hslua/hslua-module-doclayout#2

jgm commented

@Xitian9 - you wrote the fairly complex code for computing real lengths. How hard would it be to have it ignore ANSI escape sequences? I think that for the majority of widely used ones it would suffice to start the match on 'ESC [' and end on any ascii letter.

This is an issue I'm fairly familiar with, as we've tried different ways of dealing with it in hledger. Dealing with malformed ANSI escape sequences is what makes this especially tricky.

I currently think that the best approach is to not keep your ANSI markup in the string itself, but keep it separated in a data type and only stick it all together after you're done with any need to measure its length. See for example here: https://github.com/muesli4/table-layout/blob/master/src/Text/Layout/Table/Cell/Formatted.hs. You might be interested in using the Cell typeclass from the table-layout library.

I believe this can be closed as of #25 and #26.

Impressive work, @silby. Thank you!