Skip to content

Console

StefansArya edited this page Apr 18, 2019 · 5 revisions

If you want to use the console feature, you need to defined your commands on/routes/console.php. You may need to import this feature on top of your code.

use \Scarlets\Console;

Define command

Console::command($pattern, $callback, $info = '');

# Example
Console::command('echo {*}', function($message){
    echo($message);
});

Use invisible writting

Console::hiddenInput();

# Example
Console::command('input', function(){
    echo("Type something invisible: ");
    print_r("Result: ".Console::hiddenInput());
}, 'Type something');

Change text color

Console::chalk($text, $color);

// The color can be either:
//    black, red, green, yellow,
//    blue, magenta, cyan

# Example
Console::command('echo {*}', function($message){
    echo(Console::chalk("Computer> $message", 'yellow'));
});

Match more pattern

Console::args($pattern, $callback);

# Example
Console::command('echo {*}', function($all){
    Console::args('{0} {1}', function($name, $message){
        echo("$name> $message");
    });

    // Check if args above was not being called
    if(!Console::$found)
        echo "Computer> $all";
});

Adding help section

Console::help($name, $callback = 'or text');

# Example
Console::help('echo', "Type 'echo (anything here)' to get echo back.");

Clear console

Console::clear();

Check if running on console

// Check if the framework was turned on interactive CLI
\Scarlets::$interactiveCLI === true;

// Check if the framework was turned on the console feature
\Scarlets::$isConsole === true;

// Check if the script is started from shell/cli
Console::isConsole() === true;

Arrange result as a table on console

This will help you to create table for your data.

Console::table($array);

# Example
Console::table([
    ['No', 'Name', 'Email'],
    [1, 'Alex', 'alex@mail.com'],
    [2, 'Brian', 'brian@yahoo.com']
]);
Clone this wiki locally