PHP code example of decodelabs / terminus

1. Go to this page and download the library: Download decodelabs/terminus 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/ */

    

decodelabs / terminus example snippets


use DecodeLabs\Terminus as Cli;

Cli::write('Normal text'); // no newline
Cli::writeLine(' - end of line'); // with newline

use DecodeLabs\Terminus as Cli;

Cli::writeError('Error text'); // no newline
Cli::writeErrorLine(' - end of line'); // with newline

use DecodeLabs\Terminus as Cli;

$data = Cli::read(3); // Read 3 bytes
$line = Cli::readLine();

use DecodeLabs\Terminus as Cli;

Cli::toggleInputBuffer(false);
Cli::writeLine('Yes or no?')
$char = Cli::read(1); // y or n
Cli::toggleInputBuffer(true);

use DecodeLabs\Terminus as Cli;

Cli::{'blue'}('This is blue ');
Cli::{'yellow'}('This is yellow ');
Cli::{'red|green|underline'}(' This is red on green, underlined');
Cli::{'+'}('This starts on a new line');
Cli::{'.'}('- this ends on a new line');
Cli::{'>>'}('This is tabbed, twice!');
Cli::{'<'}(' - this backspaces the last character');
Cli::writeLine();
Cli::{'++>..:146|#CCC|bold|underline'}('A whole mix of parameters');

use DecodeLabs\Terminus as Cli;

if(Cli::isAnsi()) {
    // do stuff
}

use DecodeLabs\Terminus as Cli;

Cli::newLine(); // Write to a new line
Cli::newLine(5); // Write 5 new lines
Cli::deleteLine(); // Delete the previous line
Cli::clearLine(); // Clear the current line
Cli::clearLineBefore(); // Clear the current line from cursor to start
Cli::clearLineAfter(); // Clear the current line from cursor to end
Cli::backspace(); // Clear the previous character
Cli::tab(); // Write \t to output

Cli::cursorUp(); // Move cursor up vertically
Cli::cursorLineUp(); // Move cursor up to start of previous line
Cli::cursorDown(); // Move cursor down vertically
Cli::cursorLineDown(); // Move cursor down to start of next line
Cli::cursorLeft(); // Move cursor left
Cli::cursorRight(); // Move cursor right

Cli::setCursor(5); // Set cursor horizontally to index 5
Cli::setCursorLine(30, 10); // Set absolute cursor position

[$line, $pos] = Cli::getCursor(); // Attempt to get absolute cursor position
$pos = Cli::getCursorH(); // Attempt to get horizontal cursor position
$line = Cli::getCursorV(); // Attempt to get vertical cursor position

Cli::saveCursor(); // Store cursor position in terminal memory
Cli::restoreCursor(); // Attempt to restore cursor position from terminal memory

$width = Cli::getWidth(); // Get line width of terminal
$height = Cli::getHeight(); // Get line height of terminal

use DecodeLabs\Terminus as Cli;

Cli::toggleInputEcho(false); // Hide input characters
Cli::toggleInputBuffer(false); // Don't wait on return key for input

use DecodeLabs\Terminus as Cli;

if(Cli::hasStty()) {
    $snapshot = Cli::snapshotStty(); // Take a snapshot of current settings
    Cli::toggleInputEcho(false);
    // do some stuff

    Cli::restoreStty($snapshot); // Restore settings
    // or
    Cli::resetStty(); // Reset to original settings at the start of execution
}

use DecodeLabs\Terminus as Cli;

$answer = Cli::newQuestion('How are you?')
    ->setOptions('Great', 'Fine', 'OK')
    ->setDefaultValue('great')
    ->prompt();


// Or direct..
$answer = Cli::ask('How are you?', 'great');

Cli::{'..green'}('You are: '.$answer);

$password = Cli::newPasswordQuestion('Now enter a password...')
    ->setRequired(true)
    ->setRepeat(true)
    ->prompt();

// Or direct
$password = Cli::askPassword('Now enter a password...', true, true);

Cli::{'..green'}('Your password is: '.$password);

use DecodeLabs\Terminus as Cli;

if (Cli::confirm('Do you like green?', true)) {
    Cli::{'..brightGreen'}('Awesome!');
} else {
    Cli::{'..brightRed'}('Boo!');
}

use DecodeLabs\Terminus as Cli;

Cli::{'.'}('Progress spinner: ');
$spinner = Cli::newSpinner();

for ($i = 0; $i < 60; $i++) {
    usleep(20000);
    $spinner->advance();
}

$spinner->complete('Done!');

use DecodeLabs\Terminus as Cli;

Cli::{'.'}('Progress bar: ');
$spinner = Cli::newProgressBar(10, 50);

for ($i = 0; $i < 80; $i++) {
    usleep(20000);
    $spinner->advance(($i / 2) + 11);
}

$spinner->complete();

use DecodeLabs\Terminus as Cli;

Cli::debug('This is a debug');
Cli::info('This is an info message');
Cli::notice('This is a notice');
Cli::success('You\'ve done a success, well done!');
Cli::warning('This is a warning');
Cli::error('Hold tight, we have an error');
Cli::critical('This is CRITICAL');
Cli::alert('alert alert alert');
Cli::emergency('Oh no this is an emergency!');

use DecodeLabs\Terminus as Cli;

Cli::$command
    ->setHelp('Test out Terminus functionality')
    ->addArgument('action', 'Unnamed action argument')
    ->addArgument('?-test|t=Test arg', 'Named test argument with default value');

$action = Cli::$command['action'];
$test = Cli::$command['test'];

use DecodeLabs\Deliverance;
use DecodeLabs\Terminus as Cli;

$session = Cli::newSession(
    Cli::newRequest(['list', 'of', 'argv', 'params']),

    // The Io Broker is optional, defaults to best fit
    Deliverance::newIoBroker()
        ->addInputProvider($inputStream)
        ->addOutputReceiver($outputStream)
        ->addErrorReceiver($errorStream)
);

Cli::setSession($session);