]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use symfony console table to render the job list properly
authorCôme Chilliet <come.chilliet@nextcloud.com>
Mon, 27 Jun 2022 13:20:16 +0000 (15:20 +0200)
committerCôme Chilliet (Rebase PR Action) <come-nc@users.noreply.github.com>
Mon, 11 Jul 2022 09:46:23 +0000 (09:46 +0000)
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
core/Command/Background/ListCommand.php
core/Command/Base.php

index 0bbf01d76db06507374e2bb4478c417232706e0a..d29ac38a7bf0dc830fd578ec31e149173c236db1 100644 (file)
@@ -68,7 +68,7 @@ class ListCommand extends Base {
 
        protected function execute(InputInterface $input, OutputInterface $output): int {
                $jobs = $this->jobList->getJobs($input->getOption('class'), (int)$input->getOption('limit'), (int)$input->getOption('offset'));
-               $this->writeArrayInOutputFormat($input, $output, $this->formatJobs($jobs));
+               $this->writeTableInOutputFormat($input, $output, $this->formatJobs($jobs));
                return 0;
        }
 
index d228e362447235050b5e35d2011472c7147950f4..abf9f95773a59d5555570f5faf22fd1b6bec50bc 100644 (file)
 namespace OC\Core\Command;
 
 use OC\Core\Command\User\ListCommand;
-use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
 use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
+use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
 use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\Table;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -54,7 +55,7 @@ class Base extends Command implements CompletionAwareInterface {
                ;
        }
 
-       protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, array $items, string $prefix = '  - ') {
+       protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, array $items, string $prefix = '  - '): void {
                switch ($input->getOption('output')) {
                        case self::OUTPUT_FORMAT_JSON:
                                $output->writeln(json_encode($items));
@@ -84,6 +85,26 @@ class Base extends Command implements CompletionAwareInterface {
                }
        }
 
+       protected function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void {
+               switch ($input->getOption('output')) {
+                       case self::OUTPUT_FORMAT_JSON:
+                               $output->writeln(json_encode($items));
+                               break;
+                       case self::OUTPUT_FORMAT_JSON_PRETTY:
+                               $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
+                               break;
+                       default:
+                               $table = new Table($output);
+                               $table->setRows($items);
+                               if (!empty($items) && is_string(array_key_first(reset($items)))) {
+                                       $table->setHeaders(array_keys(reset($items)));
+                               }
+                               $table->render();
+                               break;
+               }
+       }
+
+
        /**
         * @param mixed $item
         */