Skip to content

Commit 6d71387

Browse files
committed
Add a pager to the list command
1 parent 85b04ff commit 6d71387

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/Command/ListCommand.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
namespace Platformsh\Cli\Command;
88

99
use Platformsh\Cli\Console\CustomTextDescriptor;
10+
use Psy\Output\ProcOutputPager;
11+
use Psy\Output\ShellOutput;
1012
use Symfony\Component\Console\Command\ListCommand as ParentListCommand;
1113
use Symfony\Component\Console\Helper\DescriptorHelper;
1214
use Symfony\Component\Console\Input\InputInterface;
1315
use Symfony\Component\Console\Input\InputOption;
16+
use Symfony\Component\Console\Output\BufferedOutput;
1417
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\Console\Output\StreamOutput;
1519

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

2530
protected function execute(InputInterface $input, OutputInterface $output)
2631
{
2732
$helper = new DescriptorHelper();
2833
$helper->register('txt', new CustomTextDescriptor());
34+
35+
if ($output->isDecorated()
36+
&& $output instanceof StreamOutput
37+
&& getenv('PAGER') !== ''
38+
&& (!function_exists('posix_isatty') || posix_isatty($output->getStream()))) {
39+
40+
// Create a pager.
41+
$pager = new ProcOutputPager($output, $input->getOption('pager'));
42+
43+
// Create an output object for the pager.
44+
$pagerOutput = new ShellOutput($output->getVerbosity(), $output->isDecorated(), $output->getFormatter(), $pager);
45+
46+
// Replace the main output object with a buffer.
47+
$output = new BufferedOutput($output->getVerbosity(), $output->isDecorated(), $output->getFormatter());
48+
}
49+
2950
$helper->describe(
3051
$output,
3152
$this->getApplication(),
@@ -36,5 +57,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
3657
'all' => $input->getOption('all'),
3758
]
3859
);
60+
61+
// If paging is enabled, send buffered output to the pager.
62+
if (isset($pagerOutput) && $output instanceof BufferedOutput) {
63+
$pagerOutput->page($output->fetch());
64+
}
3965
}
4066
}

0 commit comments

Comments
 (0)