Skip to content

Commit

Permalink
Formatting of output doesn't belong in executor
Browse files Browse the repository at this point in the history
Moved output formatting from EvalbotExecuter to Evalbot. The strange
placement of formatting in EvalbotExecuter made the code confusing and
caused Issue #2.
  • Loading branch information
Mouq committed Sep 15, 2013
1 parent fc2b25b commit d0cc1d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
32 changes: 28 additions & 4 deletions evalbot.pl
Expand Up @@ -48,10 +48,12 @@ package Evalbot;
use File::Temp qw(tempfile);
use Carp qw(confess);
use Scalar::Util qw(reftype);
use charnames qw(:full);
my $prefix = '';
my $postfix = qr/:\s/;

my $home = glob '~';
my $max_output_len = 290;

my %aliases = (
nom => 'rakudo',
Expand Down Expand Up @@ -190,6 +192,8 @@ package Evalbot;
my $evalbot_version = get_revision();

my $regex = $prefix . '(' . join('|', keys(%impls), keys(%aliases)) . ")$postfix";
my $format_res = "%s: OUTPUT«%s»\n";
my $format_nores = "%s: ( no output )\n";

sub help {
return "Usage: <$regex \$perl6_program>";
Expand Down Expand Up @@ -223,8 +227,7 @@ package Evalbot;
}
my $result = '';
while (my ($text, $names) = each %results){
$result .= join(', ', @$names);
$result .= sprintf(": %s\n", $text);
$result .= format_output(join(', ', @$names), $text);
}
return $result;
}
Expand All @@ -238,7 +241,7 @@ package Evalbot;
if (reftype($e) eq 'HASH' && $e->{revision}){
$revision = ' ' . $e->{revision}->();
}
return sprintf "%s%s: %s", $eval_name, $revision, $result;
return format_output("$eval_name$revision", $result);
} elsif ( $message =~ m/\Aevalbot\s*control\s+(\w+)/) {
my $command = $1;
if ($command eq 'restart'){
Expand All @@ -261,6 +264,27 @@ package Evalbot;
return;
}

sub format_output {
my ($prefix, $response) = @_;

if (!length $response) {
return sprintf $format_nores, $response;
}

my $newline = '';
my $null = "\N{SYMBOL FOR NULL}";
$response =~ s/\n/$newline/g;
$response =~ s/\x00/$null/g;

my $format_len = bytes::length(sprintf $format_res, $prefix, '');
if (bytes::length($response) + $format_len > $max_output_len){
my $target = $max_output_len - 3 - $format_len;
$response = substr $response, 0, $target;
$response .= '';
}
return sprintf $format_res, $prefix, $response;
}

sub get_revision {
qx/git log --pretty=%h -1/;
}
Expand Down Expand Up @@ -321,7 +345,7 @@ package main;
$revision = ' ' . $e->{revision}->();
}
binmode STDOUT, ':utf8';
printf "%s%s: %s\n", $eval_name, $revision, $result;
print Evalbot::format_output("$eval_name$revision", $result);
exit 0;
}

Expand Down
19 changes: 1 addition & 18 deletions lib/EvalbotExecuter.pm
Expand Up @@ -94,24 +94,7 @@ sub run {
return 'file not found'
};
}
my $response = _fork_and_eval($program, $executer, $ename);
if (!length $response){
$response = ' ( no output )';
} else {
$response = "OUTPUT«$response»";
}
my $newline = '';
my $null = "\N{SYMBOL FOR NULL}";
$response =~ s/\n/$newline/g;
$response =~ s/\x00/$null/g;
my $prefix_len = bytes::length($ename) + 2;
if ($executer->{revision}) { $prefix_len += (1 + bytes::length($executer->{revision}->())); }
if (bytes::length($response) + $prefix_len > $max_output_len){
my $target = $max_output_len - 3 - $prefix_len;
$response = substr $response, 0, $target;
$response .= '';
}
return $response;
return _fork_and_eval($program, $executer, $ename);
}

sub _fork_and_eval {
Expand Down

0 comments on commit d0cc1d3

Please sign in to comment.