1
0
Fork 0

Align result descriptions to make search output more readable, fixes #9455

pull/9915/head
Jordi Boggiano 2021-05-24 21:44:56 +02:00
parent 44e6591573
commit 7888d3fb97
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
3 changed files with 43 additions and 21 deletions

View File

@ -21,10 +21,12 @@ use Composer\IO\NullIO;
use Composer\Plugin\PreCommandRunEvent;
use Composer\Package\Version\VersionParser;
use Composer\Plugin\PluginEvents;
use Composer\Util\Platform;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Terminal;
/**
* Base class for Composer commands
@ -242,4 +244,29 @@ abstract class BaseCommand extends Command
$rendererStyle->setCellRowContentFormat('%s ');
$renderer->setRows($table)->render();
}
protected function getTerminalWidth()
{
if (class_exists('Symfony\Component\Console\Terminal')) {
$terminal = new Terminal();
$width = $terminal->getWidth();
} else {
// For versions of Symfony console before 3.2
// TODO remove in composer 2.2
// @phpstan-ignore-next-line
list($width) = $this->getApplication()->getTerminalDimensions();
}
if (null === $width) {
// In case the width is not detected, we're probably running the command
// outside of a real terminal, use space without a limit
$width = PHP_INT_MAX;
}
if (Platform::isWindows()) {
$width--;
} else {
$width = max(80, $width);
}
return $width;
}
}

View File

@ -87,8 +87,21 @@ EOT
$results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags, $type);
if ($results && $format === 'text') {
$width = $this->getTerminalWidth();
$nameLength = 0;
foreach ($results as $result) {
$io->write($result['name'] . (isset($result['description']) ? ' '. $result['description'] : ''));
$nameLength = max(strlen($result['name']), $nameLength);
}
$nameLength += 1;
foreach ($results as $result) {
$description = isset($result['description']) ? $result['description'] : '';
$remaining = $width - $nameLength - 2;
if (strlen($description) > $remaining) {
$description = substr($description, 0, $remaining - 3) . '...';
}
$io->write(str_pad($result['name'], $nameLength, ' ') . $description);
}
} elseif ($format === 'json') {
$io->write(JsonFile::encode($results));

View File

@ -331,26 +331,6 @@ EOT
$packageListFilter = $this->getRootRequires();
}
if (class_exists('Symfony\Component\Console\Terminal')) {
$terminal = new Terminal();
$width = $terminal->getWidth();
} else {
// For versions of Symfony console before 3.2
// TODO remove in composer 2.2
// @phpstan-ignore-next-line
list($width) = $this->getApplication()->getTerminalDimensions();
}
if (null === $width) {
// In case the width is not detected, we're probably running the command
// outside of a real terminal, use space without a limit
$width = PHP_INT_MAX;
}
if (Platform::isWindows()) {
$width--;
} else {
$width = max(80, $width);
}
if ($input->getOption('path') && null === $composer) {
$io->writeError('No composer.json found in the current directory, disabling "path" option');
$input->setOption('path', false);
@ -495,6 +475,8 @@ EOT
if ('json' === $format) {
$io->write(JsonFile::encode($viewData));
} else {
$width = $this->getTerminalWidth();
foreach ($viewData as $type => $packages) {
$nameLength = $viewMetaData[$type]['nameLength'];
$versionLength = $viewMetaData[$type]['versionLength'];