PHP code example of aspectus / terminal

1. Go to this page and download the library: Download aspectus/terminal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aspectus / terminal example snippets


use Aspectus\Terminal\TerminalDevice;

$device = new TerminalDevice();

$device->write("This and that");    // will output "This and that" in STDOUT
$device->error("There was some error");     // will output in STDERR


$device->subscribe(
    InputEvent::class,
    function (InputEvent $event) {
        $event->device->write('Read: ' . bin2hex($event->data) . PHP_EOL);
    }
);

$device->subscribe(
    InputEvent::class,
    function (InputEvent $event) {
        if ($event->data === 'q') {
            exit();
        }
    }
);

interface EventFactoryInterface
{
    /**
     * Optionally creates an event to be dispatched
     *
     * @param string $received
     * @return InputEvent|null
     */
    public function createEvent(string $received): ?InputEvent;
}

$xterm = new \Aspectus\Terminal\Xterm();

$xterm
    // we reset colors in the beginning
    ->default()
    ->bgDefault()
    ->normal()
    ->eraseDisplay()

    // position for first message
    ->moveCursorTo(5,5)

    // set style
    ->red()
    ->bgWhite()

    // write a message
    ->write('Hello world!')

    // position for second message
    ->moveCursorTo(8, 10)

    // set style
    ->bold()
    ->brightYellow()
    ->bgBlue()

    // return instructions
    ->write('This is an Xterm abstraction!')

    // write the whole buffer
    ->flush()