Skip to content

Commit a103f63

Browse files
committed
Add --theme and --refresh-interval options
1 parent 48c7975 commit a103f63

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Fix `StartCondition::isCanceled()` when a previous process has been canceled
44
- Add `SummaryTheme`
5+
- Add `--theme` option
6+
- Add `--refresh-interval` option
57

68
### [0.7.2](../../compare/0.7.1...0.7.2) - 2022-03-08
79

src/Console/Application/ParallelProcessesApplication.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
use Steevanb\ParallelProcess\{
88
Console\Application\Theme\DefaultTheme,
9+
Console\Application\Theme\SummaryTheme,
910
Console\Application\Theme\ThemeInterface,
1011
Console\Output\ConsoleBufferedOutput,
12+
Exception\ParallelProcessException,
1113
Process\Process,
1214
Process\ProcessArray
1315
};
1416
use Symfony\Component\Console\{
1517
Command\SignalableCommandInterface,
1618
Input\InputInterface,
19+
Input\InputOption,
1720
Output\OutputInterface,
1821
SingleCommandApplication
1922
};
@@ -146,13 +149,36 @@ public function handleSignal(int $signal): void
146149
}
147150
}
148151

152+
protected function configure(): void
153+
{
154+
parent::configure();
155+
156+
$this
157+
->addOption(
158+
'theme',
159+
't',
160+
InputOption::VALUE_REQUIRED,
161+
'Name or FQCN of the theme to use (examples: default, summary, ' . DefaultTheme::class . ')'
162+
)
163+
->addOption(
164+
'refresh-interval',
165+
'r',
166+
InputOption::VALUE_REQUIRED,
167+
'Refresh interval in microseconds (example: 100000 for 100ms)'
168+
);
169+
}
170+
149171
protected function isCanceled(): bool
150172
{
151173
return $this->canceled;
152174
}
153175

154176
protected function runProcessesInParallel(InputInterface $input, OutputInterface $output): int
155177
{
178+
$this
179+
->defineThemeFromInput($input)
180+
->defineRefreshIntervalFromInput($input);
181+
156182
$this->getTheme()->outputStart($output, $this->getProcesses());
157183

158184
$this
@@ -164,6 +190,53 @@ protected function runProcessesInParallel(InputInterface $input, OutputInterface
164190
return $this->getExitCode();
165191
}
166192

193+
protected function defineThemeFromInput(InputInterface $input): self
194+
{
195+
$theme = $input->getOption('theme');
196+
if (is_string($theme)) {
197+
switch ($theme) {
198+
case 'default':
199+
$this->setTheme(new DefaultTheme());
200+
break;
201+
case 'summary':
202+
$this->setTheme(new SummaryTheme());
203+
break;
204+
default:
205+
if (class_exists($theme) === false) {
206+
throw new ParallelProcessException('Theme "' . $theme . '" not found.');
207+
}
208+
209+
$themeObject = new $theme();
210+
if ($themeObject instanceof ThemeInterface === false) {
211+
throw new ParallelProcessException(
212+
'Theme "' . $theme . '" should implements ' . ThemeInterface::class . '.'
213+
);
214+
}
215+
216+
$this->setTheme($themeObject);
217+
break;
218+
}
219+
}
220+
221+
return $this;
222+
}
223+
224+
protected function defineRefreshIntervalFromInput(InputInterface $input): self
225+
{
226+
$interval = $input->getOption('refresh-interval');
227+
if (is_string($interval)) {
228+
if (is_numeric($interval) === false || (string) abs(intval($interval)) !== $interval) {
229+
throw new ParallelProcessException(
230+
'Refresh interval "' . $interval . '" should be an integer value in microseconds.'
231+
);
232+
}
233+
234+
$this->setRefreshInterval((int) $interval);
235+
}
236+
237+
return $this;
238+
}
239+
167240
protected function startProcesses(): self
168241
{
169242
return $this->startReadyProcesses();

tests/Processes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
use Symfony\Component\Console\Input\ArgvInput;
1717

1818
require $_ENV['COMPOSER_GLOBAL_AUTOLOAD_FILE_NAME'];
19+
require __DIR__ . '/../vendor/autoload.php';
1920

2021
$rootDir = dirname(__DIR__, 2);
2122

22-
$process1 = new Process(['sleep', '1'], $rootDir);
23+
$process1 = new Process(['pwd'], $rootDir);
2324

2425
$process2 = new Process(['pwd'], $rootDir);
2526
$process2->getStartCondition()->addProcessSuccessful($process1);
@@ -32,5 +33,4 @@
3233
->addProcess($process2)
3334
->addProcess($process3)
3435
->setRefreshInterval(1)
35-
->setTheme(new \Steevanb\ParallelProcess\Console\Application\Theme\SummaryTheme())
36-
->run(new ArgvInput($argv));
36+
->run();

0 commit comments

Comments
 (0)