Skip to content

Commit 6325162

Browse files
authored
Merge pull request #23 from blitz-php/devs
patch: amelioration de la methode "justify" du CLI
2 parents a44f8ed + b69287f commit 6325162

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/Cli/Console/Command.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -562,31 +562,37 @@ final public function progress(?int $total = null): ProgressBar
562562
*/
563563
final public function justify(string $first, ?string $second = '', array $options = []): self
564564
{
565+
$second = trim($second);
566+
$first = trim($first);
567+
568+
$start = $first;
569+
$end = $second;
570+
565571
$options = [
566572
'first' => $options['first'] ?? [],
567573
'second' => ($options['second'] ?? []) + ['bold' => 1],
568574
'sep' => (string) ($options['sep'] ?? '.'),
569575
];
570576

571-
$second = trim((string) $second);
572-
$first = trim($first);
577+
if (preg_match('/(\\x1b(?:.+)m)/U', $first, $matches)) {
578+
$first = str_replace($matches[1], '', $first);
579+
$first = preg_replace('/\\x1b\[0m/', '', $first);
580+
}
581+
if (preg_match('/(\\x1b(?:.+)m)/U', $second, $matches)) {
582+
$second = str_replace($matches[1], '', $second);
583+
$second = preg_replace('/\\x1b\[0m/', '', $second);
584+
}
573585

574586
$firstLength = strlen($first);
575587
$secondLength = strlen($second);
576588

577-
if (preg_match('/^\\x1b(?:.+)m(.+)\\x1b(?:.+)m$/', $first, $matches)) {
578-
$firstLength = strlen($matches[1]);
579-
}
580-
if (preg_match('/^\\x1b(?:.+)m(.+)\\x1b(?:.+)m$/', $second, $matches)) {
581-
$secondLength = strlen($matches[1]);
582-
}
583-
584589
$dashWidth = ($this->terminal->width() ?: 100) - ($firstLength + $secondLength);
585590
$dashWidth -= $second === '' ? 1 : 2;
591+
$dashWidth = $dashWidth < 0 ? 0 : $dashWidth;
586592

587-
$first = $this->color->line($first, $options['first']);
593+
$first = $this->color->line($start, $options['first']);
588594
if ($second !== '') {
589-
$second = $this->color->line($second, $options['second']);
595+
$second = $this->color->line($end, $options['second']);
590596
}
591597

592598
return $this->write($first . ' ' . str_repeat($options['sep'], $dashWidth) . ' ' . $second)->eol();
@@ -640,6 +646,6 @@ private function initProps()
640646
$this->reader = $this->io->reader();
641647
$this->color = $this->writer->colorizer();
642648
$this->cursor = $this->writer->cursor();
643-
$this->terminal = new Terminal();
649+
$this->terminal = $this->writer->terminal();
644650
}
645651
}

src/Cli/Console/Console.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,15 @@ private function addCommand(string $className, ?Logger $logger = null)
288288

289289
$console = $this;
290290
$action = function (?array $arguments = [], ?array $options = [], ?bool $suppress = null) use ($instance, $command, $console) {
291+
$this->name(); // ne pas retirer. car en cas, d'absence, cs-fixer mettra cette fonction en static. Et php-cli generera une erreur
292+
291293
foreach ($instance->required as $package) {
292294
$package = explode(':', $package);
293295
$version = $package[1] ?? null;
294296
$package = $package[0];
295297

296298
/** @var Interactor $io */
297-
$io = $this->io();
299+
$io = $console->io();
298300

299301
if (! InstalledVersions::isInstalled($package)) {
300302
$io->info('Cette commande nécessite le package "' . $package . '" mais vous ne l\'avez pas', true);

0 commit comments

Comments
 (0)