Skip to content

Commit

Permalink
Add a pager to the list command
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Jul 9, 2018
1 parent 85b04ff commit 6d71387
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
namespace Platformsh\Cli\Command;

use Platformsh\Cli\Console\CustomTextDescriptor;
use Psy\Output\ProcOutputPager;
use Psy\Output\ShellOutput;
use Symfony\Component\Console\Command\ListCommand as ParentListCommand;
use Symfony\Component\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;

class ListCommand extends ParentListCommand
{
Expand All @@ -20,12 +24,29 @@ protected function configure()
{
parent::configure();
$this->addOption('all', null, InputOption::VALUE_NONE, 'Show all commands, including hidden ones');
$this->addOption('pager', null, InputOption::VALUE_REQUIRED, 'Set the pager command', 'less -R -F');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$helper = new DescriptorHelper();
$helper->register('txt', new CustomTextDescriptor());

if ($output->isDecorated()
&& $output instanceof StreamOutput
&& getenv('PAGER') !== ''
&& (!function_exists('posix_isatty') || posix_isatty($output->getStream()))) {

// Create a pager.
$pager = new ProcOutputPager($output, $input->getOption('pager'));

// Create an output object for the pager.
$pagerOutput = new ShellOutput($output->getVerbosity(), $output->isDecorated(), $output->getFormatter(), $pager);

// Replace the main output object with a buffer.
$output = new BufferedOutput($output->getVerbosity(), $output->isDecorated(), $output->getFormatter());
}

$helper->describe(
$output,
$this->getApplication(),
Expand All @@ -36,5 +57,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
'all' => $input->getOption('all'),
]
);

// If paging is enabled, send buffered output to the pager.
if (isset($pagerOutput) && $output instanceof BufferedOutput) {
$pagerOutput->page($output->fetch());
}
}
}

0 comments on commit 6d71387

Please sign in to comment.