PHP Classes

File: doc/3_tools.bloc_tool.md

Recommend this page to a friend!
  Classes of Alexandre   PHP Commands   doc/3_tools.bloc_tool.md   Download  
File: doc/3_tools.bloc_tool.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: PHP Commands
Run commands for CLI programs using class packages
Author: By
Last change:
Date: 5 years ago
Size: 3,330 bytes
 

Contents

Class file image Download

Bloc tool

Basics

Creating a simple bloc

The bloc tool can display content in a box. A bloc is created with the bloc method and displayed with the display method. The width of the bloc will automatically adjust to the width of the content.


// In the "handle" method of your command class ...
$this->bloc('I am a simple bloc ...')
    ->display();

basic bloc

Setting bloc content


You can also add, prepend, set or clear the bloc content with the addContent, prependContent, setContent or clear methods.

// In the "handle" method of your command class ...
$bloc = $this->bloc('content ...');
$bloc->clear();
$bloc->setContent('middle');
$bloc->addContent(' after');
$bloc->prependContent('before ');
$bloc->display();

The content of the bloc will be "before middle after".

Styling

Customizing a bloc

You can modify the display of a bloc as you wish. Here is an example of what you could do with all the styling methods available :

      
// In the "handle" method of your command class ...
$this->bloc('I am a customized bloc ...')
    // Set width to 50 characters
    ->setWidth(50)
    // Set top and bottom padding width to 1 character, 
    // left and right padding to 5 characters
    ->setPadding(1, 5) 
    // Set top and bottom border width to 1 character, 
    // left and right border to 3 characters,
    // border color to red
    ->setBorder(1, 3, 'red') 
    ->setBackgroundColor('green')
    ->display();

custom command bloc

  

Creating a style

  

If you are using your own command application, you can also build a custom style using the bloc manager :

// In your command application file ...
$command_manager->getblocManager()
    ->buildStyle('your-style')
    ->setBackgroundColor('yellow')
    

And then simply apply your style to your bloc like this :

// In the "handle" method of your command class ...
$color_index = $this->bloc('content ...')
    ->setStyle('your-style')
    ->display();

Verbosity

You can decide to display a bloc or not depending on the verbosity. Also content can easily be added, prepended, cleared or not depending on the verbosity.

In the following example, the bloc will only be displayed with the "--verbose", or the "--debug" options.

// In the "handle" method of your command class ...
$bloc = $this->bloc();
$bloc->addContent('content ...');
$bloc->display(self::VERBOSITY_VERBOSE);

In the following example, the message "more details ..." will only be added with the "--debug" option.


// In the "handle" method of your command class ...
$bloc = $this->bloc();
$bloc->addContent('content ...');
$bloc->addContent('more details ...', self::VERBOSITY_DEBUG);
$bloc->display();    


Sticky bloc

You can "stick" a bloc to the same position with the placeHere method. I allows you to dynamically update your bloc. For example :

// In the "handle" method of your command class ...
$bloc = $this->bloc('I am bloc ...')->placeHere()->display();

sleep(1);
$bloc->setContent('I am a sticky bloc ...')->display();
sleep(1);
$bloc->setBackgroundColor('green')->display();

sticky command bloc