From 2a744599c0282c0438666683f8af155ef0568b81 Mon Sep 17 00:00:00 2001 From: jubianchi Date: Sat, 20 Jan 2018 09:32:34 +0100 Subject: [PATCH] feat: Add the option to set the graph's background color Closes #25 --- CHANGELOG.md | 1 + src/application.php | 18 ++++++++++-------- src/functions.php | 8 +++++--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1447d3..1cc6a31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # `1.2.0` (unreleased) +* Add the `--background` option to set the graph's background color * Versions correctly merged and checked # `1.1.0` diff --git a/src/application.php b/src/application.php index 239b98b..31ab97b 100644 --- a/src/application.php +++ b/src/application.php @@ -4,12 +4,6 @@ namespace PMSIpilot\DockerComposeViz; use Graphp\GraphViz\GraphViz; use Symfony\Component\Console; -use function PMSIpilot\DockerComposeViz\applyGraphvizStyle; -use function PMSIpilot\DockerComposeViz\createGraph; -use function PMSIpilot\DockerComposeViz\fetchNetworks; -use function PMSIpilot\DockerComposeViz\fetchServices; -use function PMSIpilot\DockerComposeViz\fetchVolumes; -use function PMSIpilot\DockerComposeViz\readConfiguration; $application = new Console\Application(); @@ -25,8 +19,15 @@ $application->register('render') ->addOption('no-volumes', null, Console\Input\InputOption::VALUE_NONE, 'Do not display volumes') ->addOption('horizontal', 'r', Console\Input\InputOption::VALUE_NONE, 'Display a horizontal graph') ->addOption('ignore-override', null, Console\Input\InputOption::VALUE_NONE, 'Ignore override file') + ->addOption('background', null, Console\Input\InputOption::VALUE_REQUIRED, 'Set the graph background color', '#ffffff') ->setCode(function (Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { + $backgroundColor = $input->getOption('background'); + + if (preg_match('/^#[a-fA-F0-9]{6}|transparent$/', $backgroundColor) === 0) { + throw new Console\Exception\InvalidArgumentException(sprintf('Invalid background color "%s". It must be a valid hex color or "transparent".', $backgroundColor)); + } + $inputFile = $input->getArgument('input-file'); $inputFileExtension = pathinfo($inputFile, PATHINFO_EXTENSION); $overrideFile = dirname($inputFile).DIRECTORY_SEPARATOR.basename($inputFile, '.'.$inputFileExtension).'.'.$input->getOption('override').'.'.$inputFileExtension; @@ -57,7 +58,7 @@ $application->register('render') $overrideVersion = (string) ($override['version'] ?? 1); if ($configurationVersion !== $overrideVersion) { - throw new Console\Exception\LogicException('Version mismatch: file ' . $inputFile . ' specifies version ' . $configurationVersion . ' but file ' . $overrideFile . ' uses version ' . $overrideVersion); + throw new Console\Exception\LogicException(sprintf('Version mismatch: file "%s" specifies version "%s" but file "%s" uses version "%s"', $inputFile, $configurationVersion, $overrideFile, $overrideVersion)); } $configuration = array_merge_recursive($configuration, $override); @@ -86,7 +87,8 @@ $application->register('render') $graph = applyGraphvizStyle( createGraph($services, $volumes, $networks, $input->getOption('no-volumes') === false, $inputFile), - $input->getOption('horizontal') + $input->getOption('horizontal'), + $input->getOption('background') ); switch ($outputFormat) { diff --git a/src/functions.php b/src/functions.php index 8dbf7b2..87bab07 100644 --- a/src/functions.php +++ b/src/functions.php @@ -95,14 +95,16 @@ function createGraph(array $services, array $volumes, array $networks, bool $wit /** * @public * - * @param Graph $graph Input graph - * @param bool $horizontal Display a horizontal graph + * @param Graph $graph Input graph + * @param bool $horizontal Display a horizontal graph + * @param string $horizontal Background color (any hex color or 'transparent') * * @return Graph A copy of the input graph with style attributes */ -function applyGraphvizStyle(Graph $graph, bool $horizontal) : Graph +function applyGraphvizStyle(Graph $graph, bool $horizontal, string $background) : Graph { $graph = $graph->createGraphClone(); + $graph->setAttribute('graphviz.graph.bgcolor', $background); $graph->setAttribute('graphviz.graph.pad', '0.5'); $graph->setAttribute('graphviz.graph.ratio', 'fill');