Align result descriptions to make search output more readable, fixes #9455
parent
44e6591573
commit
7888d3fb97
|
@ -21,10 +21,12 @@ use Composer\IO\NullIO;
|
||||||
use Composer\Plugin\PreCommandRunEvent;
|
use Composer\Plugin\PreCommandRunEvent;
|
||||||
use Composer\Package\Version\VersionParser;
|
use Composer\Package\Version\VersionParser;
|
||||||
use Composer\Plugin\PluginEvents;
|
use Composer\Plugin\PluginEvents;
|
||||||
|
use Composer\Util\Platform;
|
||||||
use Symfony\Component\Console\Helper\Table;
|
use Symfony\Component\Console\Helper\Table;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Terminal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for Composer commands
|
* Base class for Composer commands
|
||||||
|
@ -242,4 +244,29 @@ abstract class BaseCommand extends Command
|
||||||
$rendererStyle->setCellRowContentFormat('%s ');
|
$rendererStyle->setCellRowContentFormat('%s ');
|
||||||
$renderer->setRows($table)->render();
|
$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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,8 +87,21 @@ EOT
|
||||||
$results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags, $type);
|
$results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags, $type);
|
||||||
|
|
||||||
if ($results && $format === 'text') {
|
if ($results && $format === 'text') {
|
||||||
|
$width = $this->getTerminalWidth();
|
||||||
|
|
||||||
|
$nameLength = 0;
|
||||||
foreach ($results as $result) {
|
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') {
|
} elseif ($format === 'json') {
|
||||||
$io->write(JsonFile::encode($results));
|
$io->write(JsonFile::encode($results));
|
||||||
|
|
|
@ -331,26 +331,6 @@ EOT
|
||||||
$packageListFilter = $this->getRootRequires();
|
$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) {
|
if ($input->getOption('path') && null === $composer) {
|
||||||
$io->writeError('No composer.json found in the current directory, disabling "path" option');
|
$io->writeError('No composer.json found in the current directory, disabling "path" option');
|
||||||
$input->setOption('path', false);
|
$input->setOption('path', false);
|
||||||
|
@ -495,6 +475,8 @@ EOT
|
||||||
if ('json' === $format) {
|
if ('json' === $format) {
|
||||||
$io->write(JsonFile::encode($viewData));
|
$io->write(JsonFile::encode($viewData));
|
||||||
} else {
|
} else {
|
||||||
|
$width = $this->getTerminalWidth();
|
||||||
|
|
||||||
foreach ($viewData as $type => $packages) {
|
foreach ($viewData as $type => $packages) {
|
||||||
$nameLength = $viewMetaData[$type]['nameLength'];
|
$nameLength = $viewMetaData[$type]['nameLength'];
|
||||||
$versionLength = $viewMetaData[$type]['versionLength'];
|
$versionLength = $viewMetaData[$type]['versionLength'];
|
||||||
|
|
Loading…
Reference in New Issue