Skip to content

Commit

Permalink
Colorize console output
Browse files Browse the repository at this point in the history
- and tabularize
  • Loading branch information
weierophinney committed Sep 26, 2012
1 parent 41e2dab commit 5a45de4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function getControllerConfig()

$controller = new CompileController();
$controller->setConfig($config);
$controller->setConsole($services->get('Console'));
$controller->setView($view);
return $controller;
},
Expand Down
41 changes: 35 additions & 6 deletions src/PhlyBlog/CompileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
namespace PhlyBlog;

use RuntimeException;
use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\Console\ColorInterface as Color;
use Zend\Console\Request as ConsoleRequest;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\View;
Expand All @@ -13,6 +16,7 @@ class CompileController extends AbstractActionController

protected $compiler;
protected $compilerOptions;
protected $console;
protected $responseFile;
protected $writer;

Expand Down Expand Up @@ -41,6 +45,11 @@ public function setConfig($config)
$this->config = $config;
}

public function setConsole(Console $console)
{
$this->console = $console;
}

public function setEventManager(EventManagerInterface $events)
{
parent::setEventManager($events);
Expand Down Expand Up @@ -205,32 +214,52 @@ public function attachListeners(array $flags, $tags)

public function compileAction()
{
$request = $this->getRequest();
if (!$request instanceof ConsoleRequest) {
throw new RuntimeException(sprintf(
'%s may only be called from the console',
__METHOD__
));
}

$flags = $this->getFlags();
$compiler = $this->getCompiler();
$tags = $this->attachTags();
$listeners = $this->attachListeners($flags, $tags);

// Compile
echo "Compiling and sorting entries...";
$width = $this->console->getWidth();
$this->console->write("Compiling and sorting entries", Color::BLUE);
$compiler->compile();
echo "DONE!\n";
$this->reportDone($width, 29);

// Create tag cloud
if ($this->config['cloud_callback']
&& is_callable($this->config['cloud_callback'])
) {
$callable = $this->config['cloud_callback'];
echo "Creating and rendering tag cloud...";
$this->console->write("Creating and rendering tag cloud", Color::BLUE);
$cloud = $tags->getTagCloud();
call_user_func($callable, $cloud, $this->view, $this->config, $this->getServiceLocator());
echo "DONE!\n";
$this->reportDone($width, 32);
}

// compile various artifacts
foreach ($listeners as $type => $listener) {
echo "Compiling " . $type . "...";
$message = "Compiling " . $type;
$this->console->write($message, Color::BLUE);
$listener->compile();
echo "DONE!\n";
$this->reportDone($width, strlen($message));
}
}

protected function reportDone($width, $used)
{
if (($used + 8) > $width) {
$this->console->writeLine('');
$used = 0;
}
$spaces = $width - $used - 8;
$this->console->writeLine(str_repeat('.', $spaces) . '[ DONE ]', Color::GREEN);
}
}

0 comments on commit 5a45de4

Please sign in to comment.