Skip to content

Commit

Permalink
added option to set maximum column width
Browse files Browse the repository at this point in the history
removed empty lines for bottom of table
  • Loading branch information
Fabricio872 committed Apr 16, 2021
1 parent 5a80f46 commit 32e6f29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
18 changes: 15 additions & 3 deletions src/Command/UserListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class UserListCommand extends Command
* @var EntityManagerInterface
*/
private $em;
/**
* @var int
*/
private $colWidth;

public function __construct(
string $userClassName,
Expand All @@ -41,12 +45,15 @@ protected function configure()
$this
->setDescription(self::$defaultDescription)
->addArgument('page', InputArgument::OPTIONAL, 'Page', 1)
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit rows on single page', 10);
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit rows on single page', 10)
->addOption('col-width', 'w', InputOption::VALUE_REQUIRED, 'Set maximum width for one column', 64);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
/** @var int $colWidth */
$this->colWidth = $input->getOption('col-width');

$userClass = new $this->userClassName();
if (!$userClass instanceof UserInterface) {
Expand All @@ -71,14 +78,19 @@ private function draw(SymfonyStyle $io, int $page, int $limit)
->getRepository($this->userClassName)
->findBy([], [], $limit, $limit * ($page - 1));

$table = new ObjectToTable(
$objectToTable = new ObjectToTable(
$userList,
$io,
$limit
);

$table = $table->makeTable();
$table = $objectToTable->makeTable();
$table->setFooterTitle("Page $page / " . ceil($counetr / $limit));

for ($i = 0; $i < count($objectToTable->getUserGetters(new $this->userClassName)); $i++) {
$table->setColumnMaxWidth($i, $this->colWidth);
}

$table->render();
$io->writeln('To exit type "q" and pres <return>');

Expand Down
31 changes: 4 additions & 27 deletions src/Services/ObjectToTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@

namespace Fabricio872\RegisterCommand\Services;

use Doctrine\Common\Annotations\Reader;
use Fabricio872\RegisterCommand\Annotations\RegisterCommand;
use Fabricio872\RegisterCommand\Serializer\UserEntityNormalizer;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class ObjectToTable
Expand Down Expand Up @@ -51,12 +43,9 @@ public function makeTable(): Table
}, $this->getUserGetters($this->users[0]))
);
$table->setRows(
array_merge(
array_map(function ($user) {
return $this->makeCols($user);
}, $this->users),
$this->emptyRows()
)
array_map(function ($user) {
return $this->makeCols($user);
}, $this->users)
);
$table->setStyle('box');
// $table->;
Expand All @@ -72,19 +61,7 @@ private function makeCols($user)
/**
* @return array
*/
private function emptyRows(): array
{
$rows = [];
for ($i = count($this->users); $i < $this->maxRows; $i++) {
$rows[] = [''];
}
return $rows;
}

/**
* @return array
*/
private function getUserGetters(UserInterface $user): array
public function getUserGetters(UserInterface $user): array
{
return array_filter(get_class_methods($user), function ($var) {
return substr($var, 0, 3) == 'get';
Expand Down

0 comments on commit 32e6f29

Please sign in to comment.