Skip to content

Commit

Permalink
Add more details on verbose output #52
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Jun 5, 2018
1 parent 29a8a8d commit 41c355e
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 20 deletions.
5 changes: 3 additions & 2 deletions libs/Console/DauxCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Daux;

class DauxCommand extends SymfonyCommand
Expand Down Expand Up @@ -50,9 +51,9 @@ function($value) {
}
}

protected function prepareDaux(InputInterface $input)
protected function prepareDaux(InputInterface $input, OutputInterface $output)
{
$daux = new Daux(Daux::STATIC_MODE);
$daux = new Daux(Daux::STATIC_MODE, $output);

// Set the format if requested
if ($input->hasOption('format') && $input->getOption('format')) {
Expand Down
2 changes: 1 addition & 1 deletion libs/Console/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$input = new ArgvInput($argv, $this->getDefinition());
}

$daux = $this->prepareDaux($input);
$daux = $this->prepareDaux($input, $output);

$width = (new Terminal)->getWidth();

Expand Down
23 changes: 17 additions & 6 deletions libs/Console/RunAction.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
<?php namespace Todaymade\Daux\Console;

use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Daux;

trait RunAction
{
protected function getLength($content) {
return function_exists('mb_strlen') ? mb_strlen($content) : strlen($content);
}

protected function runAction($title, OutputInterface $output, $width, \Closure $closure)
protected function runAction($title, $width, \Closure $closure)
{
$output->write($title);
$verbose = Daux::getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;

Daux::write($title, $verbose);

// 8 is the length of the label + 2 let it breathe
$padding = $width - $this->getLength($title) - 10;

try {
$response = $closure(function ($content) use ($output, &$padding) {
$response = $closure(function ($content) use (&$padding) {
$padding -= $this->getLength($content);
$output->write($content);
Daux::write($content, $verbose);
});
} catch (\Exception $e) {
$output->writeln(str_pad(' ', $padding) . '[ <fg=red>FAIL</fg=red> ]');
$this->status($padding, '[ <fg=red>FAIL</fg=red> ]');
throw $e;
}
$output->writeln(str_pad(' ', $padding) . '[ <fg=green>OK</fg=green> ]');
$this->status($padding, '[ <fg=green>OK</fg=green> ]');

return $response;
}

protected function status($padding, $content)
{
$verbose = Daux::getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
$padding = $verbose ? '' : str_pad(' ', $padding);
Daux::writeln($padding . $content);
}
}
2 changes: 1 addition & 1 deletion libs/Console/Serve.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$host = $input->getOption('host');
$port = $input->getOption('port');

$daux = $this->prepareDaux($input);
$daux = $this->prepareDaux($input, $output);

// Daux can only serve HTML
$daux->getParams()->setFormat('html');
Expand Down
41 changes: 39 additions & 2 deletions libs/Daux.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Todaymade\Daux;

use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\ContentTypes\ContentTypeHandler;
use Todaymade\Daux\Tree\Builder;
use Todaymade\Daux\Tree\Content;
Expand All @@ -12,6 +13,8 @@ class Daux
const STATIC_MODE = 'DAUX_STATIC';
const LIVE_MODE = 'DAUX_LIVE';

public static $output;

/** @var string */
public $local_base;

Expand Down Expand Up @@ -44,8 +47,9 @@ class Daux
/**
* @param string $mode
*/
public function __construct($mode)
public function __construct($mode, OutputInterface $output)
{
Daux::$output = $output;
$this->mode = $mode;

$this->local_base = dirname(__DIR__);
Expand Down Expand Up @@ -236,7 +240,7 @@ public function getParams()
public function getProcessor()
{
if (!$this->processor) {
$this->processor = new Processor($this, new NullOutput(), 0);
$this->processor = new Processor($this, Daux::getOutput(), 0);
}

return $this->processor;
Expand Down Expand Up @@ -347,4 +351,37 @@ public function getContentExtensions()

return $this->validExtensions = $this->getContentTypeHandler()->getContentExtensions();
}

public static function getOutput() {
if (!Daux::$output) {
Daux:$output = new NullOutput();
}

return Daux::$output;
}

/**
* Writes a message to the output.
*
* @param string|array $messages The message as an array of lines or a single string
* @param bool $newline Whether to add a newline
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
*/
public static function write($messages, $newline = false, $options = 0) {
Daux::$output->write($messages, $newline, $options);
}

/**
* Writes a message to the output and adds a newline at the end.
*
* @param string|array $messages The message as an array of lines of a single string
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
*/
public static function writeln($messages, $options = 0) {
Daux::write($messages, true, $options);
}

public static function getVerbosity() {
return Daux::getOutput()->getVerbosity();
}
}
1 change: 0 additions & 1 deletion libs/Format/Confluence/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public function generateAll(InputInterface $input, OutputInterface $output, $wid

$tree = $this->runAction(
'Generating Tree ...',
$output,
$width,
function() use ($params) {
$tree = $this->generateRecursive($this->daux->tree, $params);
Expand Down
2 changes: 1 addition & 1 deletion libs/Format/Confluence/Publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct($confluence)
public function run($title, $closure)
{
try {
return $this->runAction($title, $this->output, $this->width, $closure);
return $this->runAction($title, $this->width, $closure);
} catch (BadResponseException $e) {
$this->output->writeLn('<fg=red>' . $e->getMessage() . '</>');
}
Expand Down
2 changes: 0 additions & 2 deletions libs/Format/HTML/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public function generateAll(InputInterface $input, OutputInterface $output, $wid

$this->runAction(
'Copying Static assets ...',
$output,
$width,
function() use ($destination, $params) {
$this->ensureEmptyDestination($destination);
Expand Down Expand Up @@ -194,7 +193,6 @@ private function generateRecursive(Directory $tree, $output_dir, $params, $outpu
} else {
$this->runAction(
'- ' . $node->getUrl(),
$output,
$width,
function() use ($node, $output_dir, $key, $params, $index_pages) {
if ($node instanceof Raw) {
Expand Down
5 changes: 5 additions & 0 deletions libs/Format/HTML/Template.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Todaymade\Daux\Format\HTML;

use League\Plates\Engine;
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Config;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Tree\Content;
Expand Down Expand Up @@ -43,6 +44,8 @@ public function getEngine(Config $params)
}
$this->engine->addFolder('theme', $theme, true);

Daux::writeln("Starting Template engine with basedir '$base' and theme folder '$theme'.", OutputInterface::VERBOSITY_VERBOSE);

$this->registerFunctions($this->engine);

return $this->engine;
Expand All @@ -65,6 +68,8 @@ public function render($name, array $data = [])
'tree' => $data['params']['tree'],
]);

Daux::writeln("Rendering template '$name'", OutputInterface::VERBOSITY_VERBOSE);

return $engine->render($name, $data);
}

Expand Down
2 changes: 0 additions & 2 deletions libs/Format/HTMLFile/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ protected function initPDF()
return $pdf;
}


/**
* {@inheritdoc}
*/
Expand All @@ -90,7 +89,6 @@ public function generateAll(InputInterface $input, OutputInterface $output, $wid
while ($current) {
$this->runAction(
'Generating ' . $current->getTitle(),
$output,
$width,
function () use ($book, $current, $params) {
$contentType = $this->daux->getContentTypeHandler()->getType($current);
Expand Down
6 changes: 4 additions & 2 deletions libs/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ class Server
*/
public static function serve()
{
$daux = new Daux(Daux::LIVE_MODE);
$output = new NullOutput();

$daux = new Daux(Daux::LIVE_MODE, $output);
$daux->initializeConfiguration();

$class = $daux->getProcessorClass();
if (!empty($class)) {
$daux->setProcessor(new $class($daux, new NullOutput(), 0));
$daux->setProcessor(new $class($daux, $output, 0));
}

// Set this critical configuration
Expand Down

0 comments on commit 41c355e

Please sign in to comment.