Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved text logic to Ascii2Html class

  • Loading branch information...
commit e6b02089d713bfdc9c8b078f6751b22ff7f91c38 1 parent b78f5e2
@vti authored
View
19 lib/Handler.pm
@@ -4,9 +4,8 @@ use strict;
use warnings;
use Terminal;
-use Terminal::Color;
-use Encode ();
-use JSON ();
+use Terminal::Ascii2Html;
+use JSON ();
my $ESCAPE = pack('C', 0x1B);
@@ -16,7 +15,7 @@ sub new {
my $self = {};
bless $self, $class;
- $self->{color} = Terminal::Color->new;
+ $self->{ascii2html} = Terminal::Ascii2Html->new;
return $self;
}
@@ -34,15 +33,19 @@ sub run {
on_row_changed => sub {
my ($terminal, $row, $text) = @_;
- $text = Encode::decode_utf8($text);
-
- $text =~ s/ / /g;
- $text = $handler->{color}->colorize($text);
+ $text = $handler->{ascii2html}->htmlify($text);
my $message = JSON->new->encode(
{type => 'row', row => $row, text => $text});
$self->send_message($message);
},
+ on_cursor_move => sub {
+ my ($terminal, $x, $y) = @_;
+
+ my $message =
+ JSON->new->encode({type => 'cursor', x => $x, y => $y});
+ $self->send_message($message);
+ },
on_finished => sub {
my $terminal = shift;
View
12 lib/Terminal.pm
@@ -27,6 +27,10 @@ sub new {
$self->{cmd} ||= '/bin/sh';
+ $self->{on_row_chainged} ||= sub { };
+ $self->{on_cursor_move} ||= sub { };
+ $self->{on_finished} ||= sub { };
+
$self->init;
return $self;
@@ -36,6 +40,10 @@ sub on_row_changed {
@_ > 1 ? $_[0]->{on_row_changed} = $_[1] : $_[0]->{on_row_changed};
}
+sub on_cursor_move {
+ @_ > 1 ? $_[0]->{on_cursor_move} = $_[1] : $_[0]->{on_cursor_move};
+}
+
sub on_finished {
@_ > 1 ? $_[0]->{on_finished} = $_[1] : $_[0]->{on_finished};
}
@@ -166,6 +174,8 @@ sub key {
}
$self->write($buffer);
+
+ $self->on_cursor_move->($self, $self->vt->x, $self->vt->y);
}
sub left { shift->move('left') }
@@ -196,6 +206,8 @@ sub move {
}
$self->write($buffer);
+
+ $self->on_cursor_move->($self, $self->vt->x, $self->vt->y);
}
sub _build_handle {
View
24 lib/Terminal/Color.pm → lib/Terminal/Ascii2Html.pm
@@ -1,8 +1,10 @@
-package Terminal::Color;
+package Terminal::Ascii2Html;
use strict;
use warnings;
+use Encode ();
+
my $ESCAPE = pack('C', 0x1B);
my $COLORS = {
@@ -35,7 +37,6 @@ my $COLORS = {
47 => 'bg-white',
};
-
sub new {
my $class = shift;
@@ -45,6 +46,25 @@ sub new {
return $self;
}
+sub htmlify {
+ my $self = shift;
+ my ($text) = @_;
+
+ $text = Encode::decode('UTF-8', $text);
+
+ $text =~ s/&/&/g;
+ $text =~ s/</&lt;/g;
+ $text =~ s/>/&gt;/g;
+ $text =~ s/"/&quot;/g;
+ $text =~ s/'/&#39;/g;
+
+ $text =~ s/ /&nbsp;/g;
+
+ $text = $self->colorize($text);
+
+ return $text;
+}
+
sub colorize {
my $self = shift;
my ($text) = @_;
View
13 t/ascii2html.t
@@ -0,0 +1,13 @@
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+use_ok('Terminal::Ascii2Html');
+
+my $a2h = Terminal::Ascii2Html->new;
+
+my $string = pack('H*', '1b5b6d1b5b33313b34306d1b5b316d73681b5b6d');
+is($a2h->colorize($string), '<span class="fg-red"><span class="bg-black"><span class="bright">sh</span></span></span>');
+
+is($a2h->htmlify('1 > 2'), '1&nbsp;&gt;&nbsp;2');
View
11 t/color.t
@@ -1,11 +0,0 @@
-use strict;
-use warnings;
-
-use Test::More tests => 1;
-
-use_ok('Terminal::Color');
-
-my $color = Terminal::Color->new;
-
-my $string = pack('H*', '1b5b6d1b5b33313b34306d1b5b316d73681b5b6d');
-is($color->colorize($string), '<span class="fg-red"><span class="bg-black"><span class="bright">sh</span></span></span>');
Please sign in to comment.
Something went wrong with that request. Please try again.