Skip to content

Commit 80fd016

Browse files
authored
refactor: example runner to consume less vertical space (#201)
1 parent 42c2a97 commit 80fd016

File tree

1 file changed

+31
-23
lines changed

1 file changed

+31
-23
lines changed

example

+31-23
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<?php
33

44
use Symfony\Component\Console\Command\Command;
5+
use Symfony\Component\Console\Helper\Table;
56
use Symfony\Component\Console\Input\InputInterface;
6-
use Symfony\Component\Console\Output\ConsoleSectionOutput;
7-
use Symfony\Component\Console\Output\OutputInterface;
7+
use Symfony\Component\Console\Output\ConsoleOutput;
88
use Symfony\Component\Console\SingleCommandApplication;
99
use Symfony\Component\Console\Style\SymfonyStyle;
1010
use Symfony\Component\Finder\Finder;
@@ -15,7 +15,7 @@ require_once __DIR__.'/vendor/autoload.php';
1515

1616
$app = (new SingleCommandApplication('LLM Chain Example Runner'))
1717
->setDescription('Runs all LLM Chain examples in folder examples/')
18-
->setCode(function (InputInterface $input, OutputInterface $output) {
18+
->setCode(function (InputInterface $input, ConsoleOutput $output) {
1919
$io = new SymfonyStyle($input, $output);
2020
$io->title('LLM Chain Examples');
2121

@@ -25,44 +25,52 @@ $app = (new SingleCommandApplication('LLM Chain Example Runner'))
2525
->sortByName()
2626
->files();
2727

28-
/** @var array{example: SplFileInfo, section: ConsoleSectionOutput, process: Process, state: 'running'|'finished'} $exampleRuns */
28+
/** @var array{example: SplFileInfo, process: Process} $exampleRuns */
2929
$exampleRuns = [];
3030
foreach ($examples as $example) {
3131
$exampleRuns[] = [
3232
'example' => $example,
33-
'section' => $section = $output->section(),
3433
'process' => $process = new Process(['php', $example->getRealPath()]),
35-
'state' => 'running',
3634
];
37-
3835
$process->start();
39-
$section->writeln(sprintf('Example %s: <comment>Running</comment>', $example->getFilename()));
4036
}
4137

4238
$examplesRunning = fn () => array_reduce($exampleRuns, fn ($running, $example) => $running || $example['process']->isRunning(), false);
4339
$examplesSuccessful = fn () => array_reduce($exampleRuns, fn ($successful, $example) => $successful && $example['process']->isSuccessful(), true);
4440

45-
while ($examplesRunning()) {
46-
sleep(1);
41+
$section = $output->section();
42+
$renderTable = function () use ($exampleRuns, $section) {
43+
$section->clear();
44+
$table = new Table($section);
45+
$table->setHeaders(['Example', 'State', 'Output']);
4746
foreach ($exampleRuns as $run) {
48-
if ('running' === $run['state'] && !$run['process']->isRunning()) {
49-
$emptyOutput = 0 === strlen(trim($run['process']->getOutput()));
50-
$success = $run['process']->isSuccessful() && !$emptyOutput;
51-
$result = $success ? '<info>Finished</info>'
47+
/** @var SplFileInfo $example */
48+
/** @var Process $process */
49+
['example' => $example, 'process' => $process] = $run;
50+
51+
$output = str_replace(PHP_EOL, ' ', $process->getOutput());
52+
$output = strlen($output) <= 100 ? $output : substr($output, 0, 100).'...';
53+
$emptyOutput = 0 === strlen(trim($output));
54+
55+
$state = 'Running';
56+
if ($process->isTerminated()) {
57+
$success = $process->isSuccessful() && !$emptyOutput;
58+
$state = $success ? '<info>Finished</info>'
5259
: (1 === $run['process']->getExitCode() || $emptyOutput ? '<error>Failed</error>' : '<comment>Skipped</comment>');
53-
$run['section']->overwrite(sprintf('Example %s: %s', $run['example']->getFilename(), $result));
54-
$run['state'] = 'finished';
55-
if ($output->isVerbose()) {
56-
$exampleOutput = $emptyOutput ? 'Output was empty' : $run['process']->getOutput();
57-
$exampleOutput = strlen($exampleOutput) <= 100 ? $exampleOutput : substr($exampleOutput, 0, 100).'...';
58-
$run['section']->writeln(
59-
sprintf('<%s>%s</>', $success ? 'fg=#999999' : 'fg=red', trim($exampleOutput))
60-
);
61-
}
6260
}
61+
62+
$table->addRow([$example->getFilename(), $state, $output]);
6363
}
64+
$table->render();
65+
};
66+
67+
while ($examplesRunning()) {
68+
$renderTable();
69+
sleep(1);
6470
}
6571

72+
$renderTable();
73+
6674
$io->newLine();
6775
if (!$examplesSuccessful()) {
6876
$io->error('Some examples failed or were skipped!');

0 commit comments

Comments
 (0)