From 985e3ebbea646584b14d33ca52e9d14ee19f55b1 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 18 Nov 2015 19:14:21 +0100 Subject: [PATCH] Helper: render torrents list --- src/Console/Command/TorrentsSearchCommand.php | 13 ++++++- src/Console/Command/TorrentsTopCommand.php | 38 ++++++------------- src/Helper/Render.php | 34 +++++++++++++++++ 3 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 src/Helper/Render.php diff --git a/src/Console/Command/TorrentsSearchCommand.php b/src/Console/Command/TorrentsSearchCommand.php index 9baafdb..07b0110 100644 --- a/src/Console/Command/TorrentsSearchCommand.php +++ b/src/Console/Command/TorrentsSearchCommand.php @@ -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: torrents:search QUERY [OPTIONS] } $response = $this->searchTorrents($client, $input, $termsTree); + + if ($response->hasError()) { + $output->writeln(sprintf( + '%s (%d)', + $response->getErrorMessage(), + $response->getErrorCode() + )); - $this->showResults($response, $output); + return; + } + + Render::torrents($response->getData()['torrents'], $output); } catch (ClientException $e) { $output->writeln(sprintf('An error occured. %s', $e->getMessage())); } diff --git a/src/Console/Command/TorrentsTopCommand.php b/src/Console/Command/TorrentsTopCommand.php index 7123d82..0d8770b 100644 --- a/src/Console/Command/TorrentsTopCommand.php +++ b/src/Console/Command/TorrentsTopCommand.php @@ -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: torrents:search:top [OPTIONS] } $response = $client->getTopTorrents($period); + + if ($response->hasError()) { + $output->writeln(sprintf( + '%s (%d)', + $response->getErrorMessage(), + $response->getErrorCode() + )); - return $this->showResults($response, $output); + return; + } + + Render::torrents($response->getData(), $output); } catch (ClientException $e) { $output->writeln(sprintf('An error occured. %s', $e->getMessage())); } } - - protected function showResults(ClientResponse $response, OutputInterface $output) - { - if ($response->hasError()) { - $output->writeln(sprintf( - '%s (%d)', - $response->getErrorMessage(), - $response->getErrorCode() - )); - - return; - } - - $output->writeln(' SEED LEECH ID NAME'); - - foreach ($response->getData() as $torrent) { - $output->writeln(sprintf( - '[%4d%6d] %9d %s', - $torrent['seeders'], - $torrent['leechers'], - $torrent['id'], - $torrent['name'] - )); - } - } } diff --git a/src/Helper/Render.php b/src/Helper/Render.php new file mode 100644 index 0000000..3387c7e --- /dev/null +++ b/src/Helper/Render.php @@ -0,0 +1,34 @@ + + */ +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( + '[%4d%6d] [%8s] %7d %s', + $torrent['seeders'], + $torrent['leechers'], + Formater::humanSize((int) $torrent['size']), + $torrent['id'], + $torrent['name'] + )); + } + } +} +