Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Commit

Permalink
Clean README and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jubianchi committed May 20, 2014
1 parent 64ba2aa commit 1634e1d
Show file tree
Hide file tree
Showing 16 changed files with 579 additions and 209 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
bin/
composer.lock
!bin/console
composer.lock
43 changes: 26 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Add these lines to your `require-dev` section:
```json
{
"require": {
"hoa/core": "*@dev",
"hoa/console": "*@dev",
"hoa/string": "*@dev",
"hoa/stream": "*@dev",
"hoa/core": "*@dev",
"hoa/string": "*@dev",
"hoa/stream": "*@dev",
"hoathis/symfony-console-bridge": "dev-master"
}
}
Expand Down Expand Up @@ -61,7 +61,7 @@ use Symfony\Component\Console\Output\OutputInterface;
$app = new Application();

$app
->register('example:output')
->register('output:verbosity')
->setCode(function(InputInterface $input, OutputInterface $output) {
$output->writeln('<info>I\'m a decorated text only in the console</info>');

Expand Down Expand Up @@ -89,15 +89,15 @@ $app->run(new ArgvInput(), new ConsoleOutput());
Running:

```sh
$ php app.php
$ bin/console output:verbosity
# I'm a decorated text only in the console
# I'll be displayed with the verbose verbosity level
```

As you will see in your terminal, output will be decorated and verbose by default. But if you run:

```sh
$ php app.php > output
$ bin/console output:verbosity > output
$ cat -vet output
# I'm a decorated text only in the console$
# I'll be displayed with the very verbose verbosity level$
Expand All @@ -118,7 +118,7 @@ Those rules will only be used if you do not provide any verbosity level using co
redirect outputs to a file using the debug verbosity level, simply run:

```sh
$ php app.php -vvv > output
$ bin/console output:verbosity -vvv > output
$ cat -vet output
# I'm a decorated text only in the console$
# I'll be displayed with the debug verbosity level$
Expand All @@ -127,7 +127,7 @@ $ cat -vet output
You can still force ansi output using the `--ansi` option:

```sh
$ php app.php -vvv --ansi | xargs -0 echo -n | cat -vet
$ bin/console output:verbosity -vvv --ansi | xargs -0 echo -n | cat -vet
# ^[[38;5;189mI'm a decorated text only in the console^[[39;49;0m$
# I'll be displayed with the ^[[38;5;96mdebug^[[39;49;0m verbosity level$
```
Expand Down Expand Up @@ -175,7 +175,7 @@ As you can see in the previous example, you can replace built-in styles by simpl

### Helpers

The real power of the library are its helpers: they let you manager every terminal components. You will first have to
The real power of the library comes from its helpers: they let you manager every terminal components. You will first have to
manually load them:

```php
Expand Down Expand Up @@ -222,7 +222,7 @@ use Symfony\Component\Console\Output\OutputInterface;
$app = new Application();

$app
->register('example:window')
->register('helper:window:animate')
->setCode(function(InputInterface $input, OutputInterface $output) use ($app) {
$window = $app->getHelperSet()->get('window');

Expand Down Expand Up @@ -265,30 +265,39 @@ use Symfony\Component\Console\Output\OutputInterface;
$app = new Application();

$app
->register('example:window')
->register('helper:cursor:draw')
->setCode(function(InputInterface $input, OutputInterface $output) use ($app) {
$window = $app->getHelperSet()->get('cursor');

$colors = ['red', '#FFCC33', 'yellow', 'green', 'blue', '#003DF5', '#6633FF'];

$helper = new Helper\CursorHelper();
$helper->hide($output)->move($output, 'up', 1);

foreach ($colors as $index => $color) {
$helper->hide($output)->move($output, '', 20 - ($index * 4));
$helper->move($output, 'left', 20 - ($index * 4));
$output->write(sprintf('<bg=%1$s>%2$s</bg=%1$s>', $color, str_repeat(' ', 20)));
$helper->move($output, '↓')->move($output, '←', 20);

$helper->move($output, 'down')->move($output, 'left', 20);
$output->write(sprintf('<bg=%1$s>%2$s</bg=%1$s>', $color, str_repeat(' ', 20)));
$helper->move($output, '↑')->bip($output);

usleep(500000);
$helper->move($output, 'up')->bip($output);

usleep(250000);
}

$helper->move($output, '↓', 2)->reset($output);
$helper
->move($output, 'down', 2)
->move($output, 'left', 100)
->reset($output)
->show($output);
})
;
```

Many other utility method are available

* `move`, `moveTo` to change cursor position, `getPosition` to retrieve current position
* `move`, `moveTo` to change cursor position, `getPosition` to retrieve the current position
* `save` and `restore` to save and restore cursor position
* `clear` to clear whole or part of the screen
* `hide`, `show` and `style` to change cursor display options
Expand Down
92 changes: 27 additions & 65 deletions Tests/Functionals/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,47 @@

namespace Hoathis\SymfonyConsoleBridge\Tests\Functionals;

use Hoathis\SymfonyConsoleBridge\Formatter\OutputFormatter;
use Hoathis\SymfonyConsoleBridge\Formatter\OutputFormatterStyle;
use Hoathis\SymfonyConsoleBridge\Output\ConsoleOutput;
use Hoathis\SymfonyConsoleBridge;
use Hoathis\SymfonyConsoleBridge\Formatter;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input;
use Symfony\Component\Console\Output;

require_once __DIR__ . '/../../vendor/autoload.php';

$application = new Application('Hoathis\SymfonyConsoleBridge');
$input = new ArgvInput();
$output = new ConsoleOutput();


$application
->register('highlight')
->setDescription('Highlights PHP code')
->addArgument('file')
->addArgument('lines', InputArgument::OPTIONAL | InputArgument::IS_ARRAY)
->setCode(function(InputInterface $input, OutputInterface $output) {
$output->getFormatter()->setStyle('num', new OutputFormatterStyle('#e4cbf4', '#795290', ['b']));
$output->getFormatter()->setStyle('numhl', new OutputFormatterStyle('#795290', '#e4cbf4', ['b']));
$output->getFormatter()->setStyle('code', new OutputFormatterStyle('#795290', '#e4cbf4'));
$output->getFormatter()->setStyle('codehl', new OutputFormatterStyle('#795290', '#FFFFFF', ['b']));
$output->getFormatter()->setStyle('file', new OutputFormatterStyle('#e4cbf4', '#795290'));

$file = $input->getArgument('file');
$hl = $input->getArgument('lines');
$lines = file($file);
$max = strlen($file);

foreach ($lines as $line) {
$max = strlen($line) > $max ? strlen($line) : $max;
}

$output->write([
PHP_EOL,
'<num> </num>',
sprintf('<file> %s </file>%s', str_repeat(' ', $max), PHP_EOL),
'<num> </num>',
sprintf('<file> %s </file>%s', str_pad($file, $max, ' ', STR_PAD_RIGHT), PHP_EOL),
'<num> </num>',
sprintf('<file> %s </file>%s', str_repeat(' ', $max), PHP_EOL)
]);
->addArgument('lines', Input\InputArgument::OPTIONAL | Input\InputArgument::IS_ARRAY)
->setCode(function(Input\InputInterface $input, Output\OutputInterface $output) {
$formatter = new Highlighter(file_get_contents($input->getArgument('file')));
$formatter->highlight($input->getArgument('lines'));

foreach ($lines as $num => $line) {
$trimmed = rtrim($line);
$highlight = in_array($num + 1, $hl);

$output->write([
sprintf('<%1$s> %2$3d </%1$s>', $highlight === true ? 'numhl' : 'num', $num + 1),
]);

$pattern = $output->getFormatter()->format(sprintf('<%1$s> %%s </%1$s>%%s', $highlight === true ? 'codehl' : 'code'));

$decorated = $output->getFormatter()->isDecorated();
$output->getFormatter()->setDecorated(false);
$output->write([
sprintf(
$pattern,
OutputFormatter::escape(str_pad(
$trimmed,
$max,
' ',
STR_PAD_RIGHT
)),
$trimmed !== $line ? PHP_EOL : ''
)
]);
$output->getFormatter()->setDecorated($decorated);
}
$output->write([PHP_EOL, Formatter\OutputFormatter::escape($formatter->format())]);
});

foreach (new \RecursiveDirectoryIterator(__DIR__ . '/Commands/', \FilesystemIterator::SKIP_DOTS) as $file) {
$register = include_once $file;

$register($application);
$highlight = function($file, array $highlights, Input\InputInterface $input, Output\OutputInterface $output) use($application) {
if ($input->getOption('no-code') === false && $output->isDecorated()) {
$application->find('highlight')->run(
new Input\ArrayInput([
'file' => $file,
'lines' => $highlights
]),
$output
);
}
};

$command = $register($application, $highlight);
$command->addOption('no-code', null, Input\InputOption::VALUE_NONE, 'Do not display code snippets');
}

$application->run($input, $output);
$output = new SymfonyConsoleBridge\Output\ConsoleOutput();
$output->setFormatter(new Formatter\OutputFormatter());

$application->run(new Input\ArgvInput(), $output);
68 changes: 32 additions & 36 deletions Tests/Functionals/Commands/CursorDraw.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,37 @@

namespace Hoathis\SymfonyConsoleBridge\Tests\Functionals\Commands;

use Hoathis\SymfonyConsoleBridge\Formatter\OutputFormatter;
use Hoathis\SymfonyConsoleBridge\Helper\CursorHelper;
use Hoathis\SymfonyConsoleBridge\Helper;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

return function(Application $application) {
$application
->register('helper:cursor:draw')
->setDescription('Tests readline select')
->setCode(function(InputInterface $input, OutputInterface $output) use($application) {
$output->setFormatter(new OutputFormatter());
$helper = new CursorHelper();

$colors = ['red', '#FFCC33', 'yellow', 'green', 'blue', '#003DF5', '#6633FF'];

foreach ($colors as $index => $color) {
$helper->hide($output)->move($output, '←', 20 - ($index * 4));
$output->write(sprintf('<bg=%1$s>%2$s</bg=%1$s>', $color, str_repeat(' ', 20)));
$helper->move($output, '↓')->move($output, '←', 20);
$output->write(sprintf('<bg=%1$s>%2$s</bg=%1$s>', $color, str_repeat(' ', 20)));
$helper->move($output, '↑')->bip($output);

usleep(500000);
}

$helper->move($output, '↓', 2)->reset($output);

$application->find('highlight')->run(
new ArrayInput([
'file' => __FILE__,
'lines' => [6, 23, 25, 27, 32]
]),
$output
);
});
use Symfony\Component\Console\Input;
use Symfony\Component\Console\Output;

return function(Application $application, callable $highlight) {
return $application->register('helper:cursor:draw')
->setDescription('Tests readline select')
->setCode(function(Input\InputInterface $input, Output\OutputInterface $output) use($application, $highlight) {
(new Helper\WindowHelper())->scroll($output, 'up', 2);
$colors = ['red', '#FFCC33', 'yellow', 'green', 'blue', '#003DF5', '#6633FF'];

$helper = new Helper\CursorHelper();
$helper->hide($output)->move($output, 'up', 1);

foreach ($colors as $index => $color) {
$helper->move($output, 'left', 20 - ($index * 4));
$output->write(sprintf('<bg=%1$s>%2$s</bg=%1$s>', $color, str_repeat(' ', 20)));
$helper->move($output, 'down')->move($output, 'left', 20);
$output->write(sprintf('<bg=%1$s>%2$s</bg=%1$s>', $color, str_repeat(' ', 20)));
$helper->move($output, 'up')->bip($output);

usleep(250000);
}

$helper
->move($output, 'down', 2)
->move($output, 'left', 100)
->reset($output)
->show($output);

$highlight(__FILE__, range(17, 34), $input, $output);
});
};
32 changes: 0 additions & 32 deletions Tests/Functionals/Commands/FormatterCustom.php

This file was deleted.

24 changes: 24 additions & 0 deletions Tests/Functionals/Commands/OutputFormatterCustom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Hoathis\SymfonyConsoleBridge\Tests\Functionals\Commands;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

return function(Application $application, callable $highlight) {
return $application
->register('output:formatter:custom')
->setDescription('Tests custom formatter styles')
->setCode(function(InputInterface $input, OutputInterface $output) use($application, $highlight) {
$output->writeln('<fg=#FF00FF> fg=#FF00FF </fg=#FF00FF>');
$output->writeln('<fg=#FF00FF;bg=white> fg=#FF00FF;bg=white </fg=#FF00FF;bg=white>');
$output->writeln([
'<fg=#FF00FF;bg=white;options=inverse;options=underlined>',
'fg=#FF00FF;bg=white;options=inverse;options=underlined',
'</fg=#FF00FF;bg=white;options=inverse;options=underlined>'
]);

$highlight(__FILE__, range(16, 22), $input, $output);
});
};
Loading

0 comments on commit 1634e1d

Please sign in to comment.