1. Go to this page and download the library: Download fi1a/console library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fi1a / console example snippets
di()->get(Fi1a\Console\IO\ConsoleOutputInterface::class)->writeln('Вывод в консоль');
declare(strict_types=1);
namespace Foo\Bar;
/**
* Простая команда
*/
class BazCommand extends AbstractCommand
{
/**
* @inheritDoc
*/
public function __construct(DefinitionInterface $definition)
{
$definition->addOption('time', 't')
->default(false)
->description('Вывод времени.')
->validation()
->allOf()
->boolean();
$definition->addArgument('format')
->default('H:i:s')
->description('Формат вывода времени.');
}
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$output->writeln('<option=bold>Пример команды</>');
if ($definition->getOption('time')->getValue()) {
$output->writeln(
'<success>Серверное время: <option=bold>{{time}}</></success>',
['time' => date($definition->getArgument('format')->getValue())]
);
}
return 0;
}
/**
* @inheritDoc
*/
public function description(): ?string
{
return 'Тестовая команда baz.';
}
}
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$output->writeln('<info>foo</info>');
$output->writeln('<error>bar</error>');
$output->writeln('<success>baz<info>qux</info></success>');
}
...
use Fi1a\Console\IO\ConsoleOutputInterface;
$output = di()->get(ConsoleOutputInterface::class);
$output->writeln('<info>foo</info>');
$output->writeln('<error>bar</error>');
$output->writeln('<success>baz<info>qux</info></success>');
use Fi1a\Console\IO\Formatter;
use Fi1a\Console\IO\Style\TrueColor;
use Fi1a\Console\IO\Style\TrueColorStyle;
Formatter::addStyle('error', new TrueColorStyle(TrueColor::WHITE, TrueColor::RED));
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$output->writeln('<color=black>foo</>');
$output->writeln('<bg=white;color=black>bar</>');
$output->writeln('<color=red>baz<option=bold,underscore>qux</></>');
}
...
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$value = $stream->read('y');
}
...
use Fi1a\Console\IO\InputInterface;
$stream = di()->get(InputInterface::class);
$value = $stream->read('y');
use Fi1a\Console\IO\InteractiveInput;
...
/**
* @inheritDoc
* @psalm-suppress PossiblyFalseReference
* @psalm-suppress MixedMethodCall
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$output->writeln(['', '<option=bold>Интерактивный ввод</>', '']);
$interactive = new InteractiveInput($output, $stream);
$interactive->addValue('foo')
->description('Введите количество от 1 до 10')
->validation()
->allOf()
->min(1)
->max(10);
$bar = $interactive->addValue('bar')
->description('Введите строки длиной от 2-х символов')
->multiple();
$bar->multipleValidation()
->allOf()
->minCount(1)
->
use Fi1a\Console\IO\ConsoleOutputInterface;
use Fi1a\Console\IO\InteractiveInputInterface;
$output = di()->get(ConsoleOutputInterface::class);
$interactive = di()->get(InteractiveInputInterface::class);
$output->writeln(['', '<option=bold>Интерактивный ввод</>', '']);
$interactive->addValue('foo')
->description('Введите количество от 1 до 10')
->validation()
->allOf()
->min(1)
->max(10);
$bar = $interactive->addValue('bar')
->description('Введите строки длиной от 2-х символов')
->multiple();
$bar->multipleValidation()
->allOf()
->minCount(1)
->
use Fi1a\Console\Component\PanelComponent\PanelComponent;
use Fi1a\Console\Component\PanelComponent\PanelStyle;
use Fi1a\Console\Component\PanelComponent\PanelStyleInterface;
use Fi1a\Console\IO\Style\ColorInterface;
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$panelStyle = new PanelStyle();
$panelStyle->setWidth(40)
->setPadding(1)
->setBorder('heavy')
->setBackgroundColor(ColorInterface::YELLOW)
->setBorderColor(ColorInterface::RED)
->setColor(ColorInterface::BLACK)
->setAlign(PanelStyleInterface::ALIGN_CENTER);
$panel = new PanelComponent(
$output,
'Lorem ipsum dolor sit amet, <error>consectetur adipiscing elit</error>, '
. 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
$panelStyle
);
$panel->display();
return 0;
}
...
use Fi1a\Console\Component\PanelComponent\PanelComponentInterface;
use Fi1a\Console\Component\PanelComponent\PanelStyleInterface;
use Fi1a\Console\IO\Style\ColorInterface;
$panel = di()->get(PanelComponentInterface::class);
$panelStyle = $panel->getStyle();
$panelStyle->setWidth(40)
->setPadding(1)
->setBorder('heavy')
->setBackgroundColor(ColorInterface::YELLOW)
->setBorderColor(ColorInterface::RED)
->setColor(ColorInterface::BLACK)
->setAlign(PanelStyleInterface::ALIGN_CENTER);
$panel->setText('Lorem ipsum dolor sit amet, <error>consectetur adipiscing elit</error>, '
. 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.');
$panel->display();
use Fi1a\Console\Component\PanelComponent\AsciiBorder;
use Fi1a\Console\Component\PanelComponent\BorderRegistry;
BorderRegistry::add(
'ascii',
new AsciiBorder()
);
use Fi1a\Console\Component\GroupComponent\GroupComponent;
use Fi1a\Console\Component\GroupComponent\GroupStyle;
use Fi1a\Console\Component\PanelComponent\PanelComponent;
use Fi1a\Console\Component\PanelComponent\PanelStyle;
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$groupStyle = new GroupStyle(40);
$groupStyle->setPanelSpacing(2);
$group = new GroupComponent($output, $groupStyle);
$panelStyle = new PanelStyle();
$panelStyle->setBorder('heavy')
->setPadding(1);
$panel1 = new PanelComponent(
$output,
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
. 'sed do eiusmod tempor incididunt ut '
. 'labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris '
. 'nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit '
. 'in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat '
. 'non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
$panelStyle
);
$panel2 = new PanelComponent(
$output,
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
. 'sed do eiusmod tempor incididunt ut '
. 'labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris '
. 'nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit '
. 'in voluptate velit esse cillum dolore eu fugiat nulla pariatur.',
$panelStyle
);
$panel3 = new PanelComponent(
$output,
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
. 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
$panelStyle
);
$group->addPanel($panel1);
$group->addPanel($panel2);
$group->addPanel($panel3);
$group->display();
return 0;
}
...
use Fi1a\Console\Component\GroupComponent\GroupComponentInterface;
use Fi1a\Console\Component\PanelComponent\PanelComponentInterface;
use Fi1a\Console\Component\PanelComponent\PanelStyleInterface;
$group = di()->get(GroupComponentInterface::class);
$group->getStyle()->setPanelSpacing(2);
$panelStyle = di()->get(PanelStyleInterface::class);
$panelStyle->setBorder('heavy')
->setPadding(1);
$panel1 = di()->get(PanelComponentInterface::class);
$panel1->setStyle($panelStyle);
$panel1->setText('Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
. 'sed do eiusmod tempor incididunt ut '
. 'labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris '
. 'nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit '
. 'in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat '
. 'non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');
$panel2 = di()->get(PanelComponentInterface::class);
$panel2->setStyle($panelStyle);
$panel2->setText('Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
. 'sed do eiusmod tempor incididunt ut '
. 'labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris '
. 'nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit '
. 'in voluptate velit esse cillum dolore eu fugiat nulla pariatur.');
$panel3 = di()->get(PanelComponentInterface::class);
$panel3->setStyle($panelStyle);
$panel3->setText('Lorem ipsum dolor sit amet, consectetur adipiscing elit, '
. 'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.');
$group->addPanel($panel1);
$group->addPanel($panel2);
$group->addPanel($panel3);
$group->display();
use Fi1a\Console\Component\ListComponent\ListComponent;
use Fi1a\Console\Component\ListComponent\ListStyle;
use Fi1a\Console\Component\ListComponent\ListStyleInterface;
use Fi1a\Console\IO\Style\ColorInterface;
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$listStyle = new ListStyle();
$listStyle->setType('upper-alpha')
->setMarkerColor(ColorInterface::GREEN);
$subListStyle = new ListStyle();
$subListStyle->setType('lower-alpha')
->setMarkerColor(ColorInterface::RED)
->setPosition(ListStyleInterface::POSITION_OUTSIDE);
$subList = new ListComponent($output, $subListStyle);
$subList->addItem('Lorem ipsum dolor sit amet');
$subList->addItem('Consectetur adipiscing elit');
$list = new ListComponent($output, $listStyle);
$list->addItem('Lorem ipsum dolor sit amet');
$list->addItem('Consectetur adipiscing elit');
$list->addItem($subList);
$list->addItem('Sed do eiusmod tempor incididunt');
$list->addItem('Duis aute irure dolor in reprehenderit');
$list->addItem('Reprehenderit in voluptate velit');
$list->display();
return 0;
}
...
use Fi1a\Console\Component\ListComponent\ListComponentInterface;
use Fi1a\Console\Component\ListComponent\ListStyleInterface;
use Fi1a\Console\IO\Style\ColorInterface;
$subList = di()->get(ListComponentInterface::class);
$subList->getStyle()
->setType('lower-alpha')
->setMarkerColor(ColorInterface::RED)
->setPosition(ListStyleInterface::POSITION_OUTSIDE);
$subList->addItem('Lorem ipsum dolor sit amet');
$subList->addItem('Consectetur adipiscing elit');
$list = di()->get(ListComponentInterface::class);
$list->getStyle()
->setType('upper-alpha')
->setMarkerColor(ColorInterface::GREEN);
$list->addItem('Lorem ipsum dolor sit amet');
$list->addItem('Consectetur adipiscing elit');
$list->addItem($subList);
$list->addItem('Sed do eiusmod tempor incididunt');
$list->addItem('Duis aute irure dolor in reprehenderit');
$list->addItem('Reprehenderit in voluptate velit');
$list->display();
use Fi1a\Console\Component\ListComponent\ListTypeRegistry;
use Fi1a\Console\Component\ListComponent\UpperAlphaListType;
ListTypeRegistry::add('upper-alpha', new UpperAlphaListType());
use Fi1a\Console\Component\PaginationComponent\PaginationComponent;
use Fi1a\Console\Component\PaginationComponent\PaginationStyle;
use Fi1a\Console\Component\TableComponent\TableComponent;
use Fi1a\Console\Component\TableComponent\TableStyle;
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$data = [
['Смартфон', '1000', '2', '2000'],
['Шкаф', '500', '1', '500'],
['Электробритва', '300', '5', '1500'],
['Станок', '200', '1', '200'],
['Диван', '1200', '1', '1200'],
['Кровать', '100', '2', '200'],
['Кресло', '300', '3', '900'],
['Шифанер', '150', '1', '150'],
['Стул', '50', '4', '200'],
['Стол', '100', '1', '100'],
];
$tableStyle = new TableStyle();
$table = new TableComponent($output, $tableStyle);
$table->setHeaders(['Товар', 'Стоимость', 'Количество', 'Итоговая сумма']);
$paginationStyle = new PaginationStyle();
$pagination = new PaginationComponent($output, $stream, $paginationStyle);
$pagination->setCount((int) ceil(count($data) / 3));
$page = 1;
do {
$rows = array_slice($data, ($page - 1) * 3, 3);
$table->setRows($rows);
$table->display();
$pagination->display();
$page = $pagination->getCurrent();
} while ($pagination->isValid());
$output->writeln('');
return 0;
}
...
use Fi1a\Console\Component\ProgressbarComponent\ProgressbarComponent;
use Fi1a\Console\Component\ProgressbarComponent\ProgressbarStyle;
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$progressbarStyle = new ProgressbarStyle();
$progressbarStyle->setTemplateByName('full');
$progressbar = new ProgressbarComponent($output, $progressbarStyle);
$progressbar->start(10);
do {
$progressbar->increment();
$progressbar->display();
sleep(1);
} while($progressbar->getProgress() < $progressbar->getMaxSteps());
$progressbar->finish();
$output->writeln(['', '']);
return 0;
}
...
use Fi1a\Console\Component\ProgressbarComponent\ProgressbarComponentInterface;
use Fi1a\Console\IO\ConsoleOutputInterface;
$output = di()->get(ConsoleOutputInterface::class);
$progressbar = di()->get(ProgressbarComponentInterface::class);
$progressbar->getStyle()
->setTemplateByName('full');
$progressbar->start(10);
do {
$progressbar->increment();
$progressbar->display();
sleep(1);
} while($progressbar->getProgress() < $progressbar->getMaxSteps());
$progressbar->finish();
$output->writeln(['', '']);
use Fi1a\Console\Component\ProgressbarComponent\ProgressbarTemplateRegistry;
ProgressbarTemplateRegistry::add(
'normal',
'{{current}}/{{max}} [{{bar}}] {{percent|sprintf("3s")}}%{{if(title)}} {{title}}{{endif}}'
);
use Fi1a\Console\Component\SpinnerComponent\DotsSpinner;
use Fi1a\Console\Component\SpinnerComponent\SpinnerRegistry;
SpinnerRegistry::add('dots', new DotsSpinner());
use Fi1a\Console\Component\SpinnerComponent\SpinnerComponent;
use Fi1a\Console\Component\SpinnerComponent\SpinnerStyle;
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$spinnerStyle = new SpinnerStyle();
$spinnerStyle->setTemplate('{{if(title)}}{{title}} {{endif}}<color=green>{{spinner}}</> ');
$spinner = new SpinnerComponent($output, $spinnerStyle);
$index = 0;
do {
if ($index % 1000000 === 0) {
$title = $spinner->getTitle();
if ($title) {
$spinner->clear();
$output->writeln($title);
}
$spinner->setTitle('In progress (' . $index . ')');
}
$spinner->display();
$index++;
} while ($index < 10000000);
$output->writeln('');
return 0;
}
...
use Fi1a\Console\Component\SpinnerComponent\SpinnerComponentInterface;
use Fi1a\Console\IO\ConsoleOutputInterface;
$output = di()->get(ConsoleOutputInterface::class);
$spinner = di()->get(SpinnerComponentInterface::class);
$spinner->getStyle()
->setTemplate('{{if(title)}}{{title}} {{endif}}<color=green>{{spinner}}</> ');
$index = 0;
do {
if ($index % 1000000 === 0) {
$title = $spinner->getTitle();
if ($title) {
$spinner->clear();
$output->writeln($title);
}
$spinner->setTitle('In progress (' . $index . ')');
}
$spinner->display();
$index++;
} while ($index < 10000000);
$output->writeln('');
use Fi1a\Console\Component\TableComponent\TableComponent;
use Fi1a\Console\Component\TableComponent\TableStyle;
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$headers = ['Товар', 'Стоимость', 'Количество', 'Итоговая сумма'];
$rows = [
['Смартфон', '1000', '2', '2000'],
['Шкаф', '500', '1', '500'],
];
$tableStyle = new TableStyle();
$tableStyle->setBorder('ascii')
->setWidth(50);
$table = new TableComponent($output, $tableStyle);
$table->setHeaders($headers);
$table->setRows($rows);
$table->display();
return 0;
}
...
use Fi1a\Console\Component\TableComponent\TableCell;
use Fi1a\Console\Component\TableComponent\TableCellStyle;
use Fi1a\Console\Component\TableComponent\TableCellStyleInterface;
$style = new TableCellStyle();
$style->setAlign(TableCellStyleInterface::ALIGN_CENTER)
$cell = new TableCell([
'value' => 'foo',
'colspan' => 2,
'style' => $style,
]);
use Fi1a\Console\Component\TableComponent\BorderRegistry;
BorderRegistry::add('none', new NoneBorder());
use Fi1a\Console\Component\TreeComponent\TreeComponent;
use Fi1a\Console\Component\TreeComponent\TreeStyle;
...
/**
* @inheritDoc
*/
public function run(
InputArgumentsInterface $input,
ConsoleOutputInterface $output,
InputInterface $stream,
DefinitionInterface $definition,
AppInterface $app
): int {
$style = new TreeStyle();
$style->setWidth(20)
->setLine('heavy');
$tree = new TreeComponent($output);
$node1 = $tree->addNode('Lorem ipsum dolor', $style);
$node1->addNode('Ex ea commodo consequat', $style);
$node2 = $tree->addNode('Consectetur adipiscing elit', $style);
$node3 = $node2->addNode('Ex ea commodo consequat', $style);
$node2->addNode('Sunt in culpa qui officia', $style);
$node3->addNode('Ut aliquip ex ea commodo');
$node3->addNode('Sunt in culpa qui officia');
$tree->addNode('Ut enim ad minim veniam', $style);
$tree->display();
return 0;
}
...
use Fi1a\Console\Component\TreeComponent\TreeComponentInterface;
use Fi1a\Console\Component\TreeComponent\TreeStyleInterface;
$style = di()->get(TreeStyleInterface::class);
$style->setWidth(20)
->setLine('heavy');
$tree = di()->get(TreeComponentInterface::class);
$tree->setStyle($style);
$node1 = $tree->addNode('Lorem ipsum dolor', $style);
$node1->addNode('Ex ea commodo consequat', $style);
$node2 = $tree->addNode('Consectetur adipiscing elit', $style);
$node3 = $node2->addNode('Ex ea commodo consequat', $style);
$node2->addNode('Sunt in culpa qui officia', $style);
$node3->addNode('Ut aliquip ex ea commodo');
$node3->addNode('Sunt in culpa qui officia');
$tree->addNode('Ut enim ad minim veniam', $style);
$tree->display();
use Fi1a\Console\Component\TreeComponent\LineRegistry;
use Fi1a\Console\Component\TreeComponent\NormalLine;
LineRegistry::add('normal', new NormalLine());
InputArgumentsInterface $input
ConsoleOutputInterface $output
shell
php examples/examples.php colors
shell
php examples/examples.php output
shell
php examples/examples.php interactive
shell
php examples/examples.php panel
shell
php examples/examples.php panel-borders
shell
php examples/examples.php group
shell
php examples/examples.php list
shell
php examples/examples.php pagination
shell
php examples/examples.php progressbar
shell
php examples/examples.php spinner
shell
php examples/examples.php table
shell
php examples/examples.php tree
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.