Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Exclude escape sequences about cursor and screen from text #2

wants to merge 1 commit into from

2 participants

moznion Randy Stauner


If we give text to html() like so;

my $w = HTML::FromANSI::Tiny->new(auto_reverse => 1, background => 'white', foreground => 'black');
$w->html("^[[1A^[[2KPhantomJS 1.9.7 (Linux): Executed 5 of 10^[[32m SUCCESS^[[39m (0 secs / 0.066 secs)^[[0m"),

then, we'll get...

In this case, [1A and [2K are escape sequences to control screen and cursor. I feel such as these are noisy.

So I wrote a patch to exclude them.

How do you think?
Is execute such processing at this module right? Or we should give pre-processed text to method?


Ping, could I please get your thoughts on this?

Randy Stauner

Sorry, I've had a lot of I/O lately and just got swamped with things.

I think logic for squashing those particular escape sequences is good,
but it probably belongs in Parse::ANSIColor::Tiny (maybe with an argument to enable/disable).

Then this module should just update its prereq on that.

In fact I do have TODO comments in there for precisely that.
I'll see if I can handle this in the other module this weekend and let you know in the next day or two what's up.

Just out of curiosity, do you know what the ^[[39m in your example (after SUCCESS) is supposed to do? I haven't found documentation for numbers after 7.
Trying it out on my terminal it appears that x8 does nothing and x9 resets the color,
but I have no idea if that's "standard" behavior.

Thanks very much for your effort so far!

Randy Stauner

I found something online that says 39/49 are foreground/background reset.

Randy Stauner

@moznion I have released new versions of Parse::ANSIColor::Tiny and HTML::FromANSI::Tiny.
I went with a much simpler solution for removing the escape sequences in the parsing module instead of this formatting one.
I appreciate the effort you put into it and I did reuse your test cases in the new releases.
So anyway, just upgrade your HTML::FromANSI::Tiny and let me know how that works for you!

Randy Stauner rwstauner closed this

Wow, great!! I'll try it :)

Thank you very much.

moznion moznion deleted the branch

It seems running well.
Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 1, 2014
  1. moznion
This page is out of date. Refresh to see the latest.
Showing with 48 additions and 0 deletions.
  1. +28 −0 lib/HTML/FromANSI/
  2. +20 −0 t/html.t
28 lib/HTML/FromANSI/
@@ -185,6 +185,9 @@ In scalar context returns a single string of concatenated HTML.
sub html {
my ($self, $text) = @_;
+ $text = $self->_exclude_cursor_escape_sequences($text);
$text = $self->ansi_parser->parse($text)
unless ref($text) eq 'ARRAY';
@@ -213,6 +216,31 @@ sub html {
return wantarray ? @html : join('', @html);
+sub _exclude_cursor_escape_sequences {
+ my ($self, $text) = @_;
+ $text =~ s/\e\[2j//gi; # 2J: clear screen
+ # 0K: clear row to the right end from current cursor position
+ # 1K: clear row to the left end from current cursor position
+ # 2K: clear row
+ for my $type (0..2) {
+ $text =~ s/\e\[${type}k//gi;
+ }
+ $text =~ s/\e\[\d+?;\d+?h//gi; # %d;%dH: move cursor by lengthwise and crosswise
+ # %dA: move cursor to above
+ # %dB: move cursor to below
+ # %dC: move cursor to right
+ # %dD: move cursor to left
+ for my $direction ('a'..'d') {
+ $text =~ s/\e\[\d+?${direction}//gi;
+ }
+ return $text;
=method html_encode
my $html = $hfat->html_encode($text);
20 t/html.t
@@ -49,4 +49,24 @@ eq_or_diff
q[hey <span class="t_on_white t_black">LOOK AT THIS</span>],
'with auto_reverse get default colors';
+ scalar $h->html("\e[2j\e[2Jfoo"),
+ q[foo],
+ 'with escape sequence to clear screen';
+ scalar $h->html("\e[0k\e[0K\e[1k\e[1K\e[2k\e[2Kfoo"),
+ q[foo],
+ 'with escape sequence to clear row';
+ scalar $h->html("\e[1;2h\e[10;20Hfoo"),
+ q[foo],
+ 'with escape sequence to move cursor by lengthwise and crosswise';
+ scalar $h->html("\e[10a\e[10A\e[10b\e[10B\e[10c\e[10C\e[10d\e[10Dfoo"),
+ q[foo],
+ 'with escape sequence to move cursor';
Something went wrong with that request. Please try again.