Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Exclude escape sequences about cursor and screen from text #2

Closed
wants to merge 1 commit into from

2 participants

moznion Randy Stauner
moznion

Hello.

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...
http://gyazo.com/ccd1c0f9b4cc309c417aadd04f0787fe.png

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?

moznion

Ping, could I please get your thoughts on this?

Randy Stauner
Owner

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
Owner

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

Randy Stauner
Owner

@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
moznion

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

Thank you very much.

moznion moznion deleted the branch
moznion

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/Tiny.pm
  2. +20 −0 t/html.t
28 lib/HTML/FromANSI/Tiny.pm
View
@@ -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
View
@@ -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';
+eq_or_diff
+ scalar $h->html("\e[2j\e[2Jfoo"),
+ q[foo],
+ 'with escape sequence to clear screen';
+
+eq_or_diff
+ scalar $h->html("\e[0k\e[0K\e[1k\e[1K\e[2k\e[2Kfoo"),
+ q[foo],
+ 'with escape sequence to clear row';
+
+eq_or_diff
+ scalar $h->html("\e[1;2h\e[10;20Hfoo"),
+ q[foo],
+ 'with escape sequence to move cursor by lengthwise and crosswise';
+
+eq_or_diff
+ 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';
+
done_testing;
Something went wrong with that request. Please try again.