2026-03-30 14:54:57 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This file is part of Psy Shell.
|
|
|
|
|
*
|
2026-04-18 20:32:18 +07:00
|
|
|
* (c) 2012-2026 Justin Hileman
|
2026-03-30 14:54:57 +07:00
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Psy\Command;
|
|
|
|
|
|
2026-04-18 20:32:18 +07:00
|
|
|
use Psy\Formatter\ManualWrapper;
|
|
|
|
|
use Psy\Readline\Interactive\Layout\DisplayString;
|
|
|
|
|
use Psy\Util\Tty;
|
2026-03-30 14:54:57 +07:00
|
|
|
use Symfony\Component\Console\Exception\CommandNotFoundException;
|
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Help command.
|
|
|
|
|
*
|
|
|
|
|
* Lists available commands, and gives command-specific help when asked nicely.
|
|
|
|
|
*/
|
|
|
|
|
class HelpCommand extends Command
|
|
|
|
|
{
|
2026-04-18 20:32:18 +07:00
|
|
|
private const TABLE_OVERHEAD_TWO_COLUMNS = 7;
|
|
|
|
|
private const TABLE_OVERHEAD_THREE_COLUMNS = 10;
|
|
|
|
|
private const MIN_DESCRIPTION_WIDTH_FOR_ALIAS_COLUMN = 40;
|
|
|
|
|
|
2026-03-30 14:54:57 +07:00
|
|
|
private ?Command $command = null;
|
2026-04-18 20:32:18 +07:00
|
|
|
private ?InputInterface $commandInput = null;
|
2026-03-30 14:54:57 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*/
|
|
|
|
|
protected function configure(): void
|
|
|
|
|
{
|
|
|
|
|
$this
|
|
|
|
|
->setName('help')
|
|
|
|
|
->setAliases(['?'])
|
|
|
|
|
->setDefinition([
|
|
|
|
|
new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name.', null),
|
|
|
|
|
])
|
|
|
|
|
->setDescription('Show a list of commands. Type `help [foo]` for information about [foo].')
|
|
|
|
|
->setHelp('My. How meta.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper for setting a subcommand to retrieve help for.
|
|
|
|
|
*
|
|
|
|
|
* @param Command $command
|
|
|
|
|
*/
|
|
|
|
|
public function setCommand(Command $command)
|
|
|
|
|
{
|
|
|
|
|
$this->command = $command;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 20:32:18 +07:00
|
|
|
/**
|
|
|
|
|
* Helper for preserving the original input when rendering contextual help.
|
|
|
|
|
*/
|
|
|
|
|
public function setCommandInput(InputInterface $input): void
|
|
|
|
|
{
|
|
|
|
|
$this->commandInput = $input;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 14:54:57 +07:00
|
|
|
/**
|
|
|
|
|
* {@inheritdoc}
|
|
|
|
|
*
|
|
|
|
|
* @return int 0 if everything went fine, or an exit code
|
|
|
|
|
*/
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
|
|
{
|
2026-04-18 20:32:18 +07:00
|
|
|
$shellOutput = $this->shellOutput($output);
|
|
|
|
|
|
2026-03-30 14:54:57 +07:00
|
|
|
if ($this->command !== null) {
|
|
|
|
|
// help for an individual command
|
2026-04-18 20:32:18 +07:00
|
|
|
$shellOutput->page($this->command->asTextForInput($this->commandInput ?? $input));
|
2026-03-30 14:54:57 +07:00
|
|
|
$this->command = null;
|
2026-04-18 20:32:18 +07:00
|
|
|
$this->commandInput = null;
|
2026-03-30 14:54:57 +07:00
|
|
|
} elseif ($name = $input->getArgument('command_name')) {
|
|
|
|
|
// help for an individual command
|
|
|
|
|
try {
|
|
|
|
|
$cmd = $this->getApplication()->get($name);
|
|
|
|
|
} catch (CommandNotFoundException $e) {
|
|
|
|
|
$this->getShell()->writeException($e);
|
|
|
|
|
$output->writeln('');
|
|
|
|
|
$output->writeln(\sprintf(
|
|
|
|
|
'<aside>To read PHP documentation, use <return>doc %s</return></aside>',
|
|
|
|
|
$name
|
|
|
|
|
));
|
|
|
|
|
$output->writeln('');
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-18 20:32:18 +07:00
|
|
|
if (!$cmd instanceof Command) {
|
|
|
|
|
throw new \RuntimeException(\sprintf('Expected Psy\Command\Command instance, got %s', \get_class($cmd)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$shellOutput->page($cmd->asTextForInput($input));
|
2026-03-30 14:54:57 +07:00
|
|
|
} else {
|
2026-04-18 20:32:18 +07:00
|
|
|
$this->commandInput = null;
|
|
|
|
|
$shellOutput->page(function (OutputInterface $pagedOutput): void {
|
|
|
|
|
$this->renderCommandList($pagedOutput);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render the top-level command list with fixed command widths and a
|
|
|
|
|
* conditional alias column when the terminal is wide enough.
|
|
|
|
|
*/
|
|
|
|
|
private function renderCommandList(OutputInterface $output): void
|
|
|
|
|
{
|
|
|
|
|
$commands = [];
|
|
|
|
|
|
|
|
|
|
foreach ($this->getApplication()->all() as $name => $command) {
|
|
|
|
|
if ($name !== $command->getName()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$commands[] = [
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'description' => $command->getDescription(),
|
|
|
|
|
'aliasText' => $command->getAliases()
|
|
|
|
|
? \sprintf('<comment>Aliases:</comment> %s', \implode(', ', $command->getAliases()))
|
|
|
|
|
: '',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$nameWidth = 0;
|
|
|
|
|
$aliasWidth = 0;
|
|
|
|
|
$descriptionWidth = 0;
|
|
|
|
|
$formatter = $output->getFormatter();
|
|
|
|
|
foreach ($commands as $command) {
|
|
|
|
|
$nameWidth = \max($nameWidth, DisplayString::width($command['name']));
|
|
|
|
|
$aliasWidth = \max($aliasWidth, DisplayString::widthWithoutFormatting($command['aliasText'], $formatter));
|
|
|
|
|
$descriptionWidth = \max($descriptionWidth, DisplayString::width($command['description']));
|
|
|
|
|
}
|
2026-03-30 14:54:57 +07:00
|
|
|
|
2026-04-18 20:32:18 +07:00
|
|
|
$terminalWidth = Tty::getWidth();
|
|
|
|
|
$wrapper = new ManualWrapper();
|
|
|
|
|
$table = $this->getTable($output)->setColumnWidth(0, $nameWidth);
|
|
|
|
|
$descriptionWidthWithAliasColumn = $terminalWidth - $nameWidth - $aliasWidth - self::TABLE_OVERHEAD_THREE_COLUMNS;
|
2026-03-30 14:54:57 +07:00
|
|
|
|
2026-04-18 20:32:18 +07:00
|
|
|
if ($aliasWidth > 0 && $descriptionWidthWithAliasColumn >= self::MIN_DESCRIPTION_WIDTH_FOR_ALIAS_COLUMN) {
|
|
|
|
|
$descriptionColumnWidth = \min($descriptionWidth, $descriptionWidthWithAliasColumn);
|
2026-03-30 14:54:57 +07:00
|
|
|
|
2026-04-18 20:32:18 +07:00
|
|
|
$table
|
|
|
|
|
->setColumnWidth(1, $descriptionColumnWidth)
|
|
|
|
|
->setColumnWidth(2, $aliasWidth);
|
2026-03-30 14:54:57 +07:00
|
|
|
|
2026-04-18 20:32:18 +07:00
|
|
|
foreach ($commands as $command) {
|
2026-03-30 14:54:57 +07:00
|
|
|
$table->addRow([
|
2026-04-18 20:32:18 +07:00
|
|
|
\sprintf('<info>%s</info>', $command['name']),
|
|
|
|
|
$wrapper->wrap($command['description'], $descriptionColumnWidth),
|
|
|
|
|
$command['aliasText'],
|
2026-03-30 14:54:57 +07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$table->render();
|
|
|
|
|
|
2026-04-18 20:32:18 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$detailsWidth = \max(10, $terminalWidth - $nameWidth - self::TABLE_OVERHEAD_TWO_COLUMNS);
|
|
|
|
|
$table->setColumnWidth(1, $detailsWidth);
|
|
|
|
|
|
|
|
|
|
foreach ($commands as $command) {
|
|
|
|
|
$details = $wrapper->wrap($command['description'], $detailsWidth);
|
|
|
|
|
if ($command['aliasText'] !== '') {
|
|
|
|
|
$details .= "\n".$wrapper->wrap($command['aliasText'], $detailsWidth);
|
2026-03-30 14:54:57 +07:00
|
|
|
}
|
2026-04-18 20:32:18 +07:00
|
|
|
|
|
|
|
|
$table->addRow([
|
|
|
|
|
\sprintf('<info>%s</info>', $command['name']),
|
|
|
|
|
$details,
|
|
|
|
|
]);
|
2026-03-30 14:54:57 +07:00
|
|
|
}
|
|
|
|
|
|
2026-04-18 20:32:18 +07:00
|
|
|
$table->render();
|
2026-03-30 14:54:57 +07:00
|
|
|
}
|
|
|
|
|
}
|