📜 ⬆️ ⬇️

PHP class for displaying color text to the console

I made for myself a script for the site deployment for production. For what it was necessary to display what is happening on the screen. And to make it clearer decided to display in color. However, I could not find something suitable. Maximum library output color text, but did not support indents. Therefore, I made my own library for this. Perhaps someone will be useful. A small (but functional) library for displaying colored text on the console with indentation support in this form


Console::indent(2)->color('brown')->bgcolor('magenta')->writeln(' Habr!'); 


  1. Installation
  2. Using
  3. Indent
  4. Styles
  5. Logging
  6. Syntactic sugar

Installation


To install, you can use composer


 composer require shasoft/console 

or download from github


Using


List of all supported colors. The column names are the background colors, the row names are the text colors.
Example of color text output


Color text output



 use Shasoft\Console\Console; //      Console::color('red')->bgcolor('green')->write('    ')->enter(); //      Console::color('green')->bgcolor('red')->writeln('    '); //      Console::color('red')->bgcolor('white')->write('     ')->reset()->writeln('    '); 

Indentation


To work with indents, use the indent function ( indent value [, absolute value ]) - if the second parameter is specified and it is = true, then the indent is absolute. Otherwise, the indent is relative. To get the current indent, call the indent () function with no parameters.


Example:


 use Shasoft\Console\Console; Console::indent(0,true); //  0 Console::indent(1)->color('red')->writeln(' 1'); Console::indent(3,true)->color('green')->writeln(' 3'); Console::indent(-1)->color('blue')->writeln(' 2'); 

     = 2 indent(1)  = 3     = 2 indent(-1)  = 2     = 2 indent(10)  = 10     = 2 indent(1)  = 1 

conclusion: Indented example output



the indent function is applied to the output STRING and the value will change until the enter () function is called. Those. this code will indent 3


 Console::indent(0,true)->color('red')->indent(1)->bgcolor('blue')->indent(1)->write(' 3')->indent(1)->enter(); 

Styles


You can specify styles. The default error style is "error"



Usage example:


 Console::indent(1,true)->style("error")->writeln('- '); 

conclusion: Style Example


Logging


There are special functions to control logging.



Values ​​are displayed using the write () function only if the current logging level is less than the global logging level.


Example:


 Console::setLogLevel(0)->logLevel(1)->writeln(' ,     = 1   0'); Console::setLogLevel(2)->logLevel(1)->writeln(',     = 1  - 2'); Console::setLogLevel(2)->logLevel(3)->write('   ')->logLevel(1)->write('  ')->enter(); 

Syntactic sugar


Not to write color ('red') -> bgcolor ('green') you can write in short


 Console::red()->bg_green()->writeln('     .'); 

Background color can be set by function without underlining. However, it visually separates the color from the prefix and, in my opinion, is very convenient.


Library page


upd : since I was pointed out an error in the name of the ident function instead of indent , I fixed it so as not to embarrass those who know English well :)


')

Source: https://habr.com/ru/post/432242/


All Articles