theZiz / aha

Ansi HTML Adapter
Other
907 stars 88 forks source link

Processing Backspaces Instead of Throwing Them Out #55

Closed Craie closed 5 years ago

Craie commented 5 years ago

It would be nice if AHA could process backspaces found in the text instead of just throwing them out as it seems to currently be doing. I'm using the program to pretty-print scripts my students have prepared with either their own or GCC's ANSI coloring in them. (That is a typescript from the script program.) They regularly backspace while scripting and/or use the arrows to scroll through their command history. The results I've seen so far look like such codes are just being thrown out. Could you please process them?

theZiz commented 5 years ago

Unfortunately no, I can't. I process the input character by character. If a control character is identified, I process it, otherwise I just print the character to stdout (with some exception as e.g. backspace as you noticeed). When I would reach the backspace, the character before – which I would need to remove – is already printed to stdout. I could "cache" a line or even the whole file and therefore move in this reresentation if an arrow-character or backspace is found, but I would need to keep track on all already "printed" style changes as they may be removed or a new one added in between.

I guess the best idea is to use a different script and/or program (sed?) which processes your control signs and afterwards piping the result to aha.

mxmlnkn commented 5 years ago

Using recursive regexes, or better supported a sed loop this might be possible with this:

printf 'ab\bcdefg\b\bhij\nk\b\b\b\blmnop\n' |
sed -r ':a; s|.\x08||g; t a; s|\x08||' |
hexdump -C

Output without sed:

acdehij
lmnop

Output with sed command inbetween (forwhich hexdump shows that no backspaces remain):

acdehij
lmnop
theZiz commented 5 years ago

Woah...

As said, somehow a regular expression will save us here. :smile: