Skip to content

Commit d0cc1d3

Browse files
committed
Formatting of output doesn't belong in executor
Moved output formatting from EvalbotExecuter to Evalbot. The strange placement of formatting in EvalbotExecuter made the code confusing and caused Issue #2.
1 parent fc2b25b commit d0cc1d3

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

evalbot.pl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ package Evalbot;
4848
use File::Temp qw(tempfile);
4949
use Carp qw(confess);
5050
use Scalar::Util qw(reftype);
51+
use charnames qw(:full);
5152
my $prefix = '';
5253
my $postfix = qr/:\s/;
5354

5455
my $home = glob '~';
56+
my $max_output_len = 290;
5557

5658
my %aliases = (
5759
nom => 'rakudo',
@@ -190,6 +192,8 @@ package Evalbot;
190192
my $evalbot_version = get_revision();
191193

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

194198
sub help {
195199
return "Usage: <$regex \$perl6_program>";
@@ -223,8 +227,7 @@ package Evalbot;
223227
}
224228
my $result = '';
225229
while (my ($text, $names) = each %results){
226-
$result .= join(', ', @$names);
227-
$result .= sprintf(": %s\n", $text);
230+
$result .= format_output(join(', ', @$names), $text);
228231
}
229232
return $result;
230233
}
@@ -238,7 +241,7 @@ package Evalbot;
238241
if (reftype($e) eq 'HASH' && $e->{revision}){
239242
$revision = ' ' . $e->{revision}->();
240243
}
241-
return sprintf "%s%s: %s", $eval_name, $revision, $result;
244+
return format_output("$eval_name$revision", $result);
242245
} elsif ( $message =~ m/\Aevalbot\s*control\s+(\w+)/) {
243246
my $command = $1;
244247
if ($command eq 'restart'){
@@ -261,6 +264,27 @@ package Evalbot;
261264
return;
262265
}
263266

267+
sub format_output {
268+
my ($prefix, $response) = @_;
269+
270+
if (!length $response) {
271+
return sprintf $format_nores, $response;
272+
}
273+
274+
my $newline = '';
275+
my $null = "\N{SYMBOL FOR NULL}";
276+
$response =~ s/\n/$newline/g;
277+
$response =~ s/\x00/$null/g;
278+
279+
my $format_len = bytes::length(sprintf $format_res, $prefix, '');
280+
if (bytes::length($response) + $format_len > $max_output_len){
281+
my $target = $max_output_len - 3 - $format_len;
282+
$response = substr $response, 0, $target;
283+
$response .= '';
284+
}
285+
return sprintf $format_res, $prefix, $response;
286+
}
287+
264288
sub get_revision {
265289
qx/git log --pretty=%h -1/;
266290
}
@@ -321,7 +345,7 @@ package main;
321345
$revision = ' ' . $e->{revision}->();
322346
}
323347
binmode STDOUT, ':utf8';
324-
printf "%s%s: %s\n", $eval_name, $revision, $result;
348+
print Evalbot::format_output("$eval_name$revision", $result);
325349
exit 0;
326350
}
327351

lib/EvalbotExecuter.pm

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,7 @@ sub run {
9494
return 'file not found'
9595
};
9696
}
97-
my $response = _fork_and_eval($program, $executer, $ename);
98-
if (!length $response){
99-
$response = ' ( no output )';
100-
} else {
101-
$response = "OUTPUT«$response»";
102-
}
103-
my $newline = '';
104-
my $null = "\N{SYMBOL FOR NULL}";
105-
$response =~ s/\n/$newline/g;
106-
$response =~ s/\x00/$null/g;
107-
my $prefix_len = bytes::length($ename) + 2;
108-
if ($executer->{revision}) { $prefix_len += (1 + bytes::length($executer->{revision}->())); }
109-
if (bytes::length($response) + $prefix_len > $max_output_len){
110-
my $target = $max_output_len - 3 - $prefix_len;
111-
$response = substr $response, 0, $target;
112-
$response .= '';
113-
}
114-
return $response;
97+
return _fork_and_eval($program, $executer, $ename);
11598
}
11699

117100
sub _fork_and_eval {

0 commit comments

Comments
 (0)