7
7
namespace Platformsh \Cli \Command ;
8
8
9
9
use Platformsh \Cli \Console \CustomTextDescriptor ;
10
+ use Psy \Output \ProcOutputPager ;
11
+ use Psy \Output \ShellOutput ;
10
12
use Symfony \Component \Console \Command \ListCommand as ParentListCommand ;
11
13
use Symfony \Component \Console \Helper \DescriptorHelper ;
12
14
use Symfony \Component \Console \Input \InputInterface ;
13
15
use Symfony \Component \Console \Input \InputOption ;
16
+ use Symfony \Component \Console \Output \BufferedOutput ;
14
17
use Symfony \Component \Console \Output \OutputInterface ;
18
+ use Symfony \Component \Console \Output \StreamOutput ;
15
19
16
20
class ListCommand extends ParentListCommand
17
21
{
@@ -20,12 +24,29 @@ protected function configure()
20
24
{
21
25
parent ::configure ();
22
26
$ 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 ' );
23
28
}
24
29
25
30
protected function execute (InputInterface $ input , OutputInterface $ output )
26
31
{
27
32
$ helper = new DescriptorHelper ();
28
33
$ 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
+
29
50
$ helper ->describe (
30
51
$ output ,
31
52
$ this ->getApplication (),
@@ -36,5 +57,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
36
57
'all ' => $ input ->getOption ('all ' ),
37
58
]
38
59
);
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
+ }
39
65
}
40
66
}
0 commit comments