Formatting the output from the console

The base class console controller yiiconsoleController supports methods to display colored and formatted output.

There are two standard methods to display the output, which are as follows:

  • stdout: This prints a string to STDOUT
  • strerr: This prints a string to STDERR

Both these methods support more parameters: the first is the text string to be displayed, and the other includes the formatting options that can be passed to make a pretty output.

There are formatting options for colors and typing; these are defined by constants from yiihelpersConsole; for example, BG_CYAN for cyan background color, BG_RED for red background color, and UNDERLINE for underlined text.

Let's see an example using the following code:

$this->stdout("Hello?
", Console::BOLD);

This will display Hello? (with a carriage return) with bold font. Sometimes, it could be possible that no effect will be displayed, since our terminal does not support colors.

In this case, a method of the console controller will help us verify our terminal capabilities: isColorEnabled() returns a Boolean indicating whether the terminal supports ANSI colors.

Both the methods strout and strerr are applied to the whole text string and are passed as the first parameter. If we want to apply some features only to a single part of the text, we must use the ansiFormat method that returns an ANSI-formatted string.

Let's take an example. Create a controller to check whether the console supports ANSI or not, and try to print the colored text if this feature is supported.

Then, create a new controller named ColorController in console/controllers/ColorController.php with this content:

<?php

namespace consolecontrollers;

use yiiconsoleController;
use yiihelpersConsole;

/**
 * Colors dedicated controller
 */
class ColorController extends Controller
{
    /**
     * Simply return a welcome text
     */
    public function actionIsClientEnabled()
    {
        if($this->isColorEnabled())
        {
            $this->stdout('OK, terminal supports colors!');
        }
        else
        {
            $this->stdout('NOT OK, terminal does not support colors!'); 
            
        }

        $this->stdOut("
");        
        
        // equivalent to return 0;
        return Controller::EXIT_CODE_NORMAL;
    }
    
    public function actionPrintColouredText()
    {
        $colouredText = $this->ansiFormat('This text is coloured', Console::FG_RED);
        $normalText ="This text is normal";
        
        $this->stdout(sprintf("%s - %s
", $normalText, $colouredText));
    }
    
    
}

?>

We call launch to check if client supports ANSI colors or not:

$ ./yii color/is-client-enabled

And to display colored text (if the client supports it):

$ ./yii color/print-coloured-text

The Console class under yiihelpers contains many other useful methods to format text and output, such as confirm() or prompt() to get input from the user, or progress to create a progress bar to display the execution state.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.20.56