PHP Common

Working with command line interpreter

Have to make a pause?

COPY
use Tivins\Core\System\Terminal; require 'vendor/autoload.php'; Terminal::sleep(2); Terminal::sleep(2, "Remains: %.2f seconds."); echo "Done." . PHP_EOL;

Go up

COPY
use Tivins\Core\Chrono; use Tivins\Core\System\Terminal; require 'vendor/autoload.php'; $array = ['part1' => ['action1', 'action2', 'action3'], 'part2' => ['action4', 'action5']]; $chrono = (new Chrono())->start(); foreach (*todo*) { $actionChrono = (new Chrono())->start(); echo " 🔧 $partName\n"; foreach (*todo*) { echo " └─ processing $action ...\n"; usleep(rand(400000, 600000)); Terminal::goUpClean(1); } Terminal::goUpClean(1); echo " ✅ $partName (" . number_format($actionChrono->get(), 2) . " s.)\n"; } echo "All tasks finished in " . number_format($chrono->get(), 2) . " seconds.\n";

asciinema

COPY
use Tivins\Core\Proc\AsciiProcess; use Tivins\Core\Proc\Command; use Tivins\Core\System\File; require 'vendor/autoload.php'; $tmpFile = tempnam('/tmp', 'test'); File::save($tmpFile, '<?' . 'php' . "\n" . 'echo "Hello"; sleep(1); echo "Word\n";'); $cast = AsciiProcess::buildCast(new Command('php', $tmpFile)); echo $cast . PHP_EOL; File::delete($tmpFile);
Output
{"version":2,"width":80,"height":10,"timestamp":1674502452,"env":{"SHELL":"\/bin\/bash","TERM":"xterm-256color"}}
[0,"o","\u0007\u001b[01;32mme@hostname\u001b[00m:\u001b[01;34m\/\u001b[00m$ run.php\r\n"]
[0.03485393524169922,"o","Hello"]
[1.0348260402679443,"o","Word\r\n"]
[1.0506720542907715,"o","\u0007\u001b[01;32mme@hostname\u001b[00m:\u001b[01;34m\/\u001b[00m$ "]

proc

COPY
use Tivins\Core\Proc\Process; require 'vendor/autoload.php'; class MyProcess extends Process { public function onUpdate(array $status, array $received): void { $in = $received[Process::STDOUT]; if (!$in) { return; } echo $in; } } $cmd = new Tivins\Core\Proc\Command('top'); echo 'ok';