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


    • Function color ( value ) - set the text color
    • Bgcolor function ( value ) - set the background color
    • The write function ( value1 , value2 , ... ) - display the value on the screen. If the value is not a string, then it is converted to a string using the php function var_export ( value , true )
    • Function reset () - reset colors to default values
    • The setDefault () function sets default colors. Default values: background color = black, text color = white
    • The enter function ( $ 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.
    • Function writeln ( ... ) - write ( ... ) + enter ()

    useShasoft\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:


    useShasoft\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 setTabSize function ( tab size ) sets the tab size. Default = 3
    • Function setSpace ( symbol ) - sets the tab character. Default = '' (space)

    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"


    • SetStyle function ( style name , text color = null, background color = null) - set the style parameters
    • Function style ( style name ) - use the specified style

    Usage example:


    Console::indent(1,true)->style("error")->writeln('Какая-то ошибка');

    conclusion: Style Example


    Logging


    There are special functions to control logging.


    • SetLogLevel function ( $ value = null) - Set / get the global logging level. Default = 0
    • Function logLevel ( $ value = null) - Set / get logging level. Default = 0

    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 :)


    Also popular now: