Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 14, 2019
1 parent 5342418 commit 7582ba3
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/Console/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ class RunCommand extends SymfonyCommand
'--verbose',
];

protected $hosts = [];
/**
* The hosts that have already been assigned a color for output.
*
* @var array
*/
protected $hostsWithColor = [];

/**
* Configure the command options.
Expand Down Expand Up @@ -181,17 +186,18 @@ protected function passToRemoteProcessor(Task $task)
protected function displayOutput($type, $host, $line)
{
$lines = explode("\n", $line);
$coloredHost = $this->getColoredHost($host);

$hostColor = $this->getHostColor($host);

foreach ($lines as $line) {
if (strlen(trim($line)) === 0) {
continue;
}

if ($type == Process::OUT) {
$this->output->write($coloredHost.': '.trim($line).PHP_EOL);
$this->output->write($hostColor.': '.trim($line).PHP_EOL);
} else {
$this->output->write($coloredHost.': '.'<fg=red>'.trim($line).'</>'.PHP_EOL);
$this->output->write($hostColor.': '.'<fg=red>'.trim($line).'</>'.PHP_EOL);
}
}
}
Expand Down Expand Up @@ -224,22 +230,22 @@ protected function loadTaskContainer()
}

/**
* Return the hostname wrapped in a color tag (cycles through 4 colors for each new host).
* Return the hostname wrapped in a color tag.
*
* @param string $host
* @param string $host
* @return string
*/
protected function getColoredHost($host)
protected function getHostColor($host)
{
$colors = ['yellow', 'cyan', 'magenta', 'blue'];

if (! in_array($host, $this->hosts)) {
$this->hosts[] = $host;
if (! in_array($host, $this->hostsWithColor)) {
$this->hostsWithColor[] = $host;
}

$color = $colors[array_search($host, $this->hosts) % count($colors)];
$color = $colors[array_search($host, $this->hostsWithColor) % count($colors)];

return "<fg=$color>[$host]</>";
return "<fg={$color}>[{$host}]</>";
}

/**
Expand Down

0 comments on commit 7582ba3

Please sign in to comment.