Skip to content

Console

StefansArya edited this page Aug 11, 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', '[email protected]'],
    [2, 'Brian', '[email protected]']
]);

Text styling

This feature is almost similar like html tag, but with custom tags.

$styled = Console::style($text);

# Example
echo Console::style("<b>Bold</b> and <i>Italic</>");

The above example will return "Bold and Italic" and you can echo it to the console.

Below are the available list of the custom tag

Text Color

Tag Attribute Description
black lighter Black color
red lighter Red color
green lighter Green color
yellow lighter Yellow color
blue lighter Blue color
magenta lighter Magenta color
cyan lighter Cyan color
gray lighter Gray color
* bg-* Add background with * is any of the tag color above

An example for using all attribute:

echo Console::style('<black lighter bg-green>Hello world</black>');

Text Format

Tag Attribute Description
b ... Bold
d ... Dim color
i ... Italic
u ... Underline
uu ... Double Underline
cu ... Curly Underline
blink ... Blink
r ... Reverse background and text color
invisible ... Transparent color
st ... Strike Thru
ol ... Overline (Line above text)
link src="link" Hyperlink

Some of above text format may only work on several terminal environment.
An example for using link:

echo Console::style('<link src="https://google.com">Visit google</link>');

// It will return clickable "Visit google" text on the console