Skip to content

Commit

Permalink
Add cf()
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Oct 14, 2021
1 parent 889bbed commit 34f649c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 92 deletions.
96 changes: 4 additions & 92 deletions src/Host/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Deployer\Deployer;
use Deployer\Exception\ConfigurationException;
use Deployer\Task\Context;
use function Deployer\Support\colorize_host;

class Host
{
Expand All @@ -32,11 +33,10 @@ public function __construct(string $hostname)

public function __toString(): string
{
$asterisks = '';
if (Context::has() && Context::get()->isLocal()) {
$asterisks = '*';
return 'local//' . $this->getTag() . '';
}
return "$asterisks{$this->getTag()}";
return $this->getTag();
}

public function config(): Configuration
Expand Down Expand Up @@ -97,7 +97,7 @@ public function setTag(string $tag): self

public function getTag(): ?string
{
return $this->config->get('tag', $this->generateTag());
return $this->config->get('tag', colorize_host($this->getAlias()));
}

public function setHostname(string $hostname): self
Expand Down Expand Up @@ -228,92 +228,4 @@ public function getSshArguments(): ?array
{
return $this->config->get('ssh_arguments');
}

private function generateTag(): ?string
{
if (defined('NO_ANSI')) {
return $this->getAlias();
}

if (in_array($this->getAlias(), ['localhost', 'local'])) {
return $this->getAlias();
}

if (getenv('COLORTERM') === 'truecolor') {
$hsv = function ($h, $s, $v) {
$r = $g = $b = $i = $f = $p = $q = $t = 0;
$i = floor($h * 6);
$f = $h * 6 - $i;
$p = $v * (1 - $s);
$q = $v * (1 - $f * $s);
$t = $v * (1 - (1 - $f) * $s);
switch ($i % 6) {
case 0:
$r = $v;
$g = $t;
$b = $p;
break;
case 1:
$r = $q;
$g = $v;
$b = $p;
break;
case 2:
$r = $p;
$g = $v;
$b = $t;
break;
case 3:
$r = $p;
$g = $q;
$b = $v;
break;
case 4:
$r = $t;
$g = $p;
$b = $v;
break;
case 5:
$r = $v;
$g = $p;
$b = $q;
break;
}
$r = round($r * 255);
$g = round($g * 255);
$b = round($b * 255);
return "\x1b[38;2;{$r};{$g};{$b}m";
};

$total = 100;
$colors = [];
for ($i = 0; $i < $total; $i++) {
$colors[] = $hsv($i / $total, 1, .9);
}

$alias = $this->getAlias();
$tag = $colors[abs(crc32($alias)) % count($colors)];

return "{$tag}{$alias}\x1b[0m";
}


$colors = [
'fg=cyan;options=bold',
'fg=green;options=bold',
'fg=yellow;options=bold',
'fg=cyan',
'fg=blue',
'fg=yellow',
'fg=magenta',
'fg=blue;options=bold',
'fg=green',
'fg=magenta;options=bold',
'fg=red;options=bold',
];
$alias = $this->getAlias();
$tag = $colors[abs(crc32($alias)) % count($colors)];

return "<{$tag}>{$alias}</>";
}
}
87 changes: 87 additions & 0 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,90 @@ function find_config_line(string $source, string $name): \Generator
}
}
}

function colorize_host(string $alias): string
{
if (defined('NO_ANSI')) {
return $alias;
}

if (in_array($alias, ['localhost', 'local'], true)) {
return $alias;
}

if (getenv('COLORTERM') === 'truecolor') {
$hsv = function ($h, $s, $v) {
$r = $g = $b = $i = $f = $p = $q = $t = 0;
$i = floor($h * 6);
$f = $h * 6 - $i;
$p = $v * (1 - $s);
$q = $v * (1 - $f * $s);
$t = $v * (1 - (1 - $f) * $s);
switch ($i % 6) {
case 0:
$r = $v;
$g = $t;
$b = $p;
break;
case 1:
$r = $q;
$g = $v;
$b = $p;
break;
case 2:
$r = $p;
$g = $v;
$b = $t;
break;
case 3:
$r = $p;
$g = $q;
$b = $v;
break;
case 4:
$r = $t;
$g = $p;
$b = $v;
break;
case 5:
$r = $v;
$g = $p;
$b = $q;
break;
}
$r = round($r * 255);
$g = round($g * 255);
$b = round($b * 255);
return "\x1b[38;2;{$r};{$g};{$b}m";
};
$total = 100;
$colors = [];
for ($i = 0; $i < $total; $i++) {
$colors[] = $hsv($i / $total, .5, .9);
}
if ($alias === 'prod' || $alias === 'production') {
return "$colors[99]$alias\x1b[0m";
}
if ($alias === 'beta') {
return "$colors[14]$alias\x1b[0m";
}
$tag = $colors[abs(crc32($alias)) % count($colors)];
return "$tag$alias\x1b[0m";
}

$colors = [
'fg=cyan;options=bold',
'fg=green;options=bold',
'fg=yellow;options=bold',
'fg=cyan',
'fg=blue',
'fg=yellow',
'fg=magenta',
'fg=blue;options=bold',
'fg=green',
'fg=magenta;options=bold',
'fg=red;options=bold',
];
$tag = $colors[abs(crc32($alias)) % count($colors)];
return "<$tag>$alias</>";
}

0 comments on commit 34f649c

Please sign in to comment.