Console Output

Introduction

The Ghostly Console/Output Component Allows you to easily output text to the console using objects such as Text and Color

Installation

composer r ghosty/console-output

Using introduction

The output class allows you to print some text to the console. Using the Output::prinstStatic() method you can display various text on the screen, for example:

// app/Console/TestCommand.php
use Ghosty\Component\Console\Output\Output;

Output::printStatic('some text');

Running a command

php ./app/Console/TestCommand.php

Using Colors

Colored text

To display colored text on the screen, use the Ghosty\Component\Console\Output\Text\Text object, as well as Ghosty\Component\Console\Output\Color\TextColor.

The Text object constructor takes the following parameters as arguments:

string $text - Text to output to the console.

TextColor $textColor - Text color.

BackgroundColor $backgroundColor - Background color.

For example:

// app/Console/TestCommand.php
$TextColor = new TextColor('Red');
$BackgroundColor = new BackgroundColor('White');

$Text = new Text('Some text', $TextColor, $BackgroundColor);

echo $Text; 
Output::printStatic($Text);

Output:

Some TextSome text

List of colors

You can use the TextColor and BackgroundColor or AbstractColor class constants, they list all available colors.

// app/Console/TestCommand.php
$TextColor = new TextColor(AbstactColor::Blue);
$BackgroundColor = new BackgroundColor(AbstactColor::Yellow);

$Text = new Text('Some text', $TextColor, $BackgroundColor);

Output::printStatic($Text);

Output:

Some Text

Text as string

You can also use the Text object as a string.

// app/Console/TestCommand.php
$some = new Text(
    'Some',
    new TextColor(AbstactColor::Default),
    new BackgroundColor(AbstactColor::DarkGray)
);

$text = new Text(
    'text',
    new TextColor(AbstactColor::Red),
    new BackgroundColor(AbstactColor::Green)
);

echo $some . $text;

Output:

Sometext

Text Bag

Using TextBag you can output multiple Text objects to the console using the Output object's printStatic method or the default echo method

// app/Console/TestCommand.php
$some = new Text(
    'Some',
    new TextColor(AbstactColor::Default),
    new BackgroundColor(AbstactColor::DarkGray)
);

$text = new Text(
    'text',
    new TextColor(AbstactColor::Red),
    new BackgroundColor(AbstactColor::Green)
);

echo new TextBag([$some, $text]);

Output:

Sometext

You can also use the TextBag object as a string:

// app/Console/TestCommand.php
$some = new Text(
    'Some',
    new TextColor(AbstactColor::Default),
    new BackgroundColor(AbstactColor::DarkGray)
);

$text = new Text(
    'text',
    new TextColor(AbstactColor::Red),
    new BackgroundColor(AbstactColor::Green)
);

echo new TextBag([$some, $text]) . "\n" . new TextBag([$text, $some]);

Output:

Sometext
textSome

Text array

Using the printStatic() or print() method of the Output object, you can print text from an array to the console. The array can contain both Text objects and strings or any other objects that have the __toString() method

// app/Console/TestCommand.php
$some = new Text(
    'Some',
    new TextColor(AbstactColor::Default),
    new BackgroundColor(AbstactColor::DarkGray)
);
$text = new Text(
    'text',
    new TextColor(AbstactColor::Red),
    new BackgroundColor(AbstactColor::Green)
);

Output::printStatic([$some, ' - ', $text]);

Output:

Some - text

Using Decorations

To use text decorations, you need to pass an object of type TextDecoration or DecorationBag or an array of TextDecoration objects to the constructor of the Text class, for example:

// app/Console/TestCommand.php
$text = new Text(
    'Text',
    new TextColor(AbstactColor::Default),
    new BackgroundColor(AbstactColor::Default),
    new TextDecoration("Underlined")
);

echo $text; 

Output:

Text

List of decorations

You can use constants of the Text Decoration class; they list all available text decorations, for example:

// app/Console/TestCommand.php
$text = new Text(
    'Text',
    new TextColor(AbstactColor::Default),
    new BackgroundColor(AbstactColor::Default),
    new TextDecoration(TextDecoration::UNDERLINED)
);

echo $text; 

Output:

Text

Multiple decorations

To use multiple decorations, you can pass an array of TextDecoration objects or a DecorationBag object to the Text class constructor, for example:

Using array

In this example, we pass an array of TextDecoration objects to the Text class constructor

// app/Console/TestCommand.php
$text = new Text(
    'Text',
    new TextColor(AbstactColor::Default),
    new BackgroundColor(AbstactColor::Default),
    [new TextDecoration(TextDecoration::UNDERLINED),
    new TextDecoration(TextDecoration::REVERSE)]
);

echo $text; 

Output:

Text

Using DecorationBag

In this example, we pass an DecorationBag object to the Text class constructor

// app/Console/TestCommand.php
$text = new Text(
    'Text',
    new TextColor(AbstactColor::Default),
    new BackgroundColor(AbstactColor::Default),
    new DecorationBag([
        new TextDecoration(TextDecoration::UNDERLINED),
        new TextDecoration(TextDecoration::REVERSE)
    ])
);

echo $text; 

Output:

Text