I don't see why you're using a hash here. Your keys form a simple number sequence. Why not use an array? Also - why sort the keys? First of all, you're sorting alphanumerically, so it's not doing what you likely expected. Secondly, the order in which you apply the substitutions doesn't matter, so even a numerical sort would just be wasted effort. There is also no reason to initialize the arrays over and over each time you call the sub.
Finally, the name isn't very well chosen; I'd suggest Term::ANSI::Remove (for the current incarnation) instead.
Everything put together, I propose this as a starting point:
package Term::ANSI::Remove; use strict; use warnings; use vars qw(@ISA @EXPORT); use Exporter; @ISA = qw/Exporter/; @EXPORT = qw/escape_ansi/; my @remove = qw( \x1b\x5b\d+A \x1b\x5b\d+B \x1b\x5b\d+C \x1b\x5b\d+D \x1b\x5bK \x1bD \x1bM \x1b7 \x1b8 \x1b\x233 \x1b\x234 \x1b\x235 \x1b\x236 \x1b\x5b\d+\x3b[01457]+m \x1b\x5b0K \x1b\x5b2K \x1b\x5bJ \x1b\x5b0J \x1b\x5b2J \x1b\x5b\d\x3b[01234]+q \x1b[\x28\x29]A \x1b[\x28\x29]B \x1b[\x28\x29]0 \x1b[\x28\x29]1 \x1b[\x28\x29]2 \x1bK\d+\x38\d+r \x1bH \x1b\x5bg \x1b\x5b0g \x1b\x5b3g \x1b\x5b20h \x1b\x5b20l \x1b\x5b\x3f1h \x1b\x5b\x3f1l \x1b\x5b\x3f2l \x1b\x5b\x3f3h \x1b\x5b\x3f3l \x1b\x5b\x3f4h \x1b\x5b\x3f4l \x1b\x5b\x3f5h \x1b\x5b\x3f5l \x1b\x5b\x3f6h \x1b\x5b\x3f6l \x1b\x5b\x3f7h \x1b\x5b\x3f7l \x1b\x5b\x3f8h \x1b\x5b\x3f8l \x1b\x5b\x3f9h \x1b\x5b\x3f9l \x1b\x5b6n \x1b\x5b\d+\x3b(\d+)R \x1b\x5b5n \x1b\x5bc \x1b\x5b0c \x1b\x5bc \x1b\x5b0c \x1b\x5b\x3f1\x3b[0-7]c \x1bc \x1b\x238 \x1b\x5b2\x3b\d{1,3}y \x1bA \x1bB \x1b[A-DF-KZ12<>=] \x1bA[\x3e\x3d\x3c] \x1bY[\000-\377]{2} ); my %replace = ( qr/\x1b\x5b\d+\x3b\d+[fH]/ => '', qr/\x1b\x5b1/ => '', ); my $remove_rx = join '|', @remove; $remove_rx = qr/(?:$remove_rx)/; sub escape_ansi { my $buf_ref = shift; my ($ansi, $replace); $$buf_ref =~ s/$remove_rx//; $$buf_ref =~ s/$ansi/$replace/g while ($ansi, $replace) = each %replace; } 1;
Makeshifts last the longest.
In reply to Re: clean Telnet ANSI escape codes: VT52 and VT100
by Aristotle
in thread clean Telnet ANSI escape codes: VT52 and VT100
by JamesNC
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |