Helper: render torrents list

This commit is contained in:
Simon Vieille 2015-11-18 19:14:21 +01:00
parent 6711f1ae0c
commit 985e3ebbea
3 changed files with 58 additions and 27 deletions

View file

@ -12,6 +12,7 @@ use Api\ConfigLoader;
use Api\ClientResponse;
use Api\ClientException;
use Helper\Formater;
use Helper\Render;
class TorrentsSearchCommand extends Command
{
@ -84,8 +85,18 @@ Usage: <comment>torrents:search</comment> <info>QUERY</info> [OPTIONS]
}
$response = $this->searchTorrents($client, $input, $termsTree);
if ($response->hasError()) {
$output->writeln(sprintf(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
$this->showResults($response, $output);
return;
}
Render::torrents($response->getData()['torrents'], $output);
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
}

View file

@ -10,6 +10,7 @@ use Api\Client;
use Api\ConfigLoader;
use Api\ClientResponse;
use Api\ClientException;
use Helper\Render;
class TorrentsTopCommand extends Command
{
@ -49,35 +50,20 @@ Usage: <comment>torrents:search:top</comment> [OPTIONS]
}
$response = $client->getTopTorrents($period);
if ($response->hasError()) {
$output->writeln(sprintf(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
return $this->showResults($response, $output);
return;
}
Render::torrents($response->getData(), $output);
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
}
}
protected function showResults(ClientResponse $response, OutputInterface $output)
{
if ($response->hasError()) {
$output->writeln(sprintf(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
$output->writeln(' SEED LEECH ID NAME');
foreach ($response->getData() as $torrent) {
$output->writeln(sprintf(
'[<info>%4d</info><comment>%6d</comment>] %9d %s',
$torrent['seeders'],
$torrent['leechers'],
$torrent['id'],
$torrent['name']
));
}
}
}

34
src/Helper/Render.php Normal file
View file

@ -0,0 +1,34 @@
<?php
namespace Helper;
use Symfony\Component\Console\Output\OutputInterface;
use Api\ClientResponse;
/**
* Class Render
* @author Simon Vieille <simon@deblan.fr>
*/
class Render
{
public static function torrents(array $torrents, OutputInterface $output)
{
if (empty($torrents)) {
return;
}
$output->writeln(' SEED LEECH SIZE ID NAME');
foreach ($torrents as $torrent) {
$output->writeln(sprintf(
'[<info>%4d</info><comment>%6d</comment>] [%8s] %7d %s',
$torrent['seeders'],
$torrent['leechers'],
Formater::humanSize((int) $torrent['size']),
$torrent['id'],
$torrent['name']
));
}
}
}