diff --git a/demos/interactive/console.php b/demos/interactive/console.php index c8fbca77ce..0d3dcde1ad 100644 --- a/demos/interactive/console.php +++ b/demos/interactive/console.php @@ -88,15 +88,11 @@ public function generateReport() ]); $message = Message::addTo($vp, ['This demo may not work', 'type' => 'warning']); $message->text->addParagraph('This demo requires Linux OS and will display error otherwise.'); - $console = Console::addTo($vp)->set(static function (Console $console) { + Console::addTo($vp)->set(static function (Console $console) { $console->exec('/sbin/ping', ['-c', '5', '-i', '1', '192.168.0.1']); $console->exec('/sbin/ping', ['-c', '5', '-i', '2', '8.8.8.8']); $console->exec('/bin/no-such-command'); }); - $console->js(true, new \Atk4\Ui\Js\JsExpression(<<<'JS' - console.log([l]); - atk.elementRemoveObserver.addHandler($('#' + [id])[ 0], function () { console.log([ul]); }); - JS, ['l' => 'loaded', 'ul' => 'unloaded - working!', 'id' => $console->getHtmlId()])); }); $tab = $tabs->addTab('composer update', static function (VirtualPage $vp) { diff --git a/src/Console.php b/src/Console.php index 554d0a02b5..aaa386baec 100644 --- a/src/Console.php +++ b/src/Console.php @@ -271,47 +271,44 @@ public function exec(string $command, array $args = []): ?bool return null; } - usleep(250_000); - $this->output('sleep(0.25)'); - -// [$proc, $pipes] = $this->execRaw($command, $args); -// -// stream_set_blocking($pipes[1], false); -// stream_set_blocking($pipes[2], false); -// // $pipes contain streams that are still open and not EOF -// while ($pipes) { // @TODO this condition is always true -// $read = $pipes; -// $j1 = null; -// $j2 = null; -// if (stream_select($read, $j1, $j2, 2) === false) { -// throw new Exception('Unexpected stream_select() result'); -// } -// -// $status = proc_get_status($proc); -// if (!$status['running']) { -// proc_close($proc); -// -// break; -// } -// -// foreach ($read as $f) { -// $data = rtrim((string) fgets($f)); -// if ($data === '') { -// // TODO fix coverage stability, add test with explicit empty string -// // @codeCoverageIgnoreStart -// continue; -// // @codeCoverageIgnoreEnd -// } -// -// if ($f === $pipes[2]) { // stderr -// $this->warning($data); -// } else { // stdout -// $this->output($data); -// } -// } -// } -// -// $this->lastExitCode = $status['exitcode']; + [$proc, $pipes] = $this->execRaw($command, $args); + + stream_set_blocking($pipes[1], false); + stream_set_blocking($pipes[2], false); + // $pipes contain streams that are still open and not EOF + while ($pipes) { // @TODO this condition is always true + $read = $pipes; + $j1 = null; + $j2 = null; + if (stream_select($read, $j1, $j2, 2) === false) { + throw new Exception('Unexpected stream_select() result'); + } + + $status = proc_get_status($proc); + if (!$status['running']) { + proc_close($proc); + + break; + } + + foreach ($read as $f) { + $data = rtrim((string) fgets($f)); + if ($data === '') { + // TODO fix coverage stability, add test with explicit empty string + // @codeCoverageIgnoreStart + continue; + // @codeCoverageIgnoreEnd + } + + if ($f === $pipes[2]) { // stderr + $this->warning($data); + } else { // stdout + $this->output($data); + } + } + } + + $this->lastExitCode = $status['exitcode']; return $this->lastExitCode ? false : true; }