PHP class for outputting color text to the console

 3r33338. 3r3-31.
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 outputting colored text to the console with indentation support in this form
 3r33338.
Console :: ident (2) -> color ('brown') -> bgcolor ('magenta') -> writeln ('Hello Habr!'); 3r33333. 3r33333.
 3r33338.
PHP class for outputting color text to the console var_export (
The value of
,
True
)
 3r33338.
Function
reset
() - reset colors to defaults to
 3r33338.
Function
setDefault
() - set the default colors. Default values: background color = black, text color = white
 3r33338.
Function
enter
(
$ ResetColor
= True). By default, a function call resets colors to default values. Note that the string is not displayed until the enter () function is called. This is due to the fact that the library supports indentation.
 3r33338.
Function
writeln
(
) - write (
) + Enter ()
 3r33338.
 3r33338.
use ShasoftConsoleConsole; 3r33338. //Display color text on line 3r33333. Console :: color ('red') -> bgcolor ('green') -> write ('Red text on green background') -> enter (); 3r33338. //Display color text on line 3r33333. Console :: color ('green') -> bgcolor ('red') -> writeln ('Green text on red background'); 3r33338. //Display color text on line 3r33333. Console :: color ('red') -> bgcolor ('white') -> write ('Red text on white background') -> reset () -> writeln ('Display text in default color'); 3r33333. 3r33333.
 3r33338.
Indents 3r3306.
 3r33338.
To work with indents is the function
ident
(
Indentation value 3r33283. W2w2w20.) - 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, you need to call the ident () function without parameters.
 3r33338.
Example: 3r3326.
 3r33338.
use ShasoftConsoleConsole; 3r33338. 3r33338. Console :: ident (? true); //Indent 0
Console :: ident (1) -> color ('red') -> writeln ('Indent 1'); 3r33338. Console :: ident (? true) -> color ('green') -> writeln ('Indent 3'); 3r33338. Console :: ident (-1) -> color ('blue') -> writeln ('Indent 2'); 3r33333. 3r33333.
 3r33338.
conclusion:
 3r33338.
 3r33338.
Function
setTabSize
(3r33282. Tab size 3r38383.) - sets the size of the tabulator. Default = 3 r3r3284.  3r33338.
Function
setSpace
(
Symbol
) - sets the tab character. Default = '' (space)
 3r33338.
 3r33338.
The ident function is applied to the output STRING and the value will change until the enter () function is called. Those. This code will print a line with an indent of 3
 3r33338.
Console :: ident (? true) -> color ('red') -> ident (1) -> bgcolor ('blue') -> ident (1) -> write ('Indent 3') -> ident ( 1) -> enter (); 3r33333. 3r33333.
 3r33338.
Styles 3r3306.
 3r33338.
You can specify styles. The default error style is "error"
 3r33338.
 3r33338.
Function
setStyle
(
Style name
,
Text color
= Null,
Background color
= Null) —set the parameters of the
style.  3r33338.
Function
style
(
The name of the style
) - use the specified style
 3r33338.
 3r33338.
Usage example: 3r3326.
 3r33338.
Console :: ident (? true) -> style ("error") -> writeln ('Some kind of error'); 3r33333. 3r33333.
 3r33338.
conclusion:
 3r33338.
Logging 3r3306.
 3r33338.
There are special functions for controlling logging
 3r33338.
 3r33338.
Function
setLogLevel
(
$ Value
= Null) - Set /get the global logging level. Default = 0
 3r33338.
Function
logLevel
(
$ Value
= Null) - Set /get the logging level. Default = 0
 3r33338.
 3r33338.
Values ​​are displayed using the write () function only if the current logging level is less than the global logging level.
 3r33338.
Example: 3r3326.
 3r33338.
Console :: setLogLevel (0) -> logLevel (1) -> writeln ('It will not be displayed, because the logging level = 1 which is greater than 0'); 3r33338. Console :: setLogLevel (2) -> logLevel (1) -> writeln ('Displays, because the logging level = 1 which is less than equal 2'); 3r33338. Console :: setLogLevel (2) -> logLevel (3) -> write ('This text is not displayed') -> logLevel (1) -> write ('Text on screen') -> enter (); 3r33333. 3r33333.
 3r33338. 3r3305. Syntactic sugar
 3r33338.
Not to write color ('red') -> bgcolor ('green') you can write in short form
 3r33338.
Console :: red () -> bg_green () -> writeln ('String of red color on green background.'); 3r33333. 3r33333.
 3r33338.
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.
 3r33338.
3r3323. Library page 3r3325.
3r33338. 3r33338. 3r33338.
3r33338.
3r33338. 3r33338. 3r33338. 3r33338.
+ 0 -

Add comment