serafim/terminal

This package is abandoned and no longer maintained. No replacement package was suggested.

Library to work with the console enabled ascii control characters

dev-master / 1.0.x-dev 2014-07-14 18:49 UTC

README

Library to work with the console enabled ascii control characters

Installation:

{
    "require": {
        "serafim/terminal": "dev-master"
    }
}

Usage example:

Example preview

<?php
use Terminal\Progress;
use Terminal\Char;
use Terminal\Stdout;
use Terminal\Stdin;

require __DIR__ . '/../vendor/autoload.php';

// Progress style
$progress = (new Progress())

    // Before drawing
    ->before(function($render){
        $render->setAlignment(20);      // Set progress alignment
        $render->setProgressColors([    // Set colors for 0%-90% and 90%-100%
            0 => Char::FONT_L_RED,
            90 => Char::FONT_L_GREEN
        ]);
    })

    // On value change event
    ->onProgress(function($render){
        return $render
            ->drawLine()                // Draw progress line
            ->prefix(chr(219))          // With prefix chr(219)
            ->aggregate(chr(219))       // With chr(219) as highlighter
            ->postfix(
                chr(221) .
                $render->drawValue() .  // With percentage as postfix
                chr(222)
            )
            ->length(30);               // With length 30 chars
    });


// Write title
Stdout::writeln('%s%sExample progress', [Char::STYLE_BOLD, Char::STYLE_UNDERSCORE]);
Stdout::newLine();

// Get progress speed (as example)
$speed = (int)Stdin::read('Progress speed [1]: ', 1);

// Set new progress title
$progress->setTitle('Some progress: ');
for ($i=0; $i<=100; $i++) {
    $progress->setValue($i);            // Update progress value
    usleep($speed * 1000);
}
Stdout::newLine();


// Update progress title
$progress->setTitle('Another progress: ');
for ($i=0; $i<=100; $i++) {
    $progress->setValue($i);            // Update value
    usleep($speed * 1000);
}
Stdout::newLine();

// Write line
Stdout::writeln('Success!', Char::FONT_L_GREEN);