-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxkcd2term.php
executable file
·115 lines (98 loc) · 3.28 KB
/
xkcd2term.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env php
<?php
/*
* This file is part of php-drawille
*
* (c) Jeff Welch <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Command\Command as ConsoleCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Goutte\Client;
class Application extends ConsoleApplication
{
public function __construct() {
parent::__construct(basename(__FILE__), '1.0');
$this->add(new Command);
}
protected function getCommandName(InputInterface $input) {
return basename(__FILE__);
}
protected function getDefaultInputDefinition()
{
return new InputDefinition(array(
new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message.'),
new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version.')
));
}
}
class Command extends ConsoleCommand
{
protected function configure()
{
$this->setName(basename(__FILE__))
->setDescription('convert an xkcd comic to terminal')
->addArgument(
'comic',
InputArgument::REQUIRED,
'Comic ID or "random"'
)
->addOption(
'threshold',
't',
InputOption::VALUE_REQUIRED,
'Color threshold',
382.5
)
->addOption(
'ratio',
'r',
InputOption::VALUE_REQUIRED,
'Image resize ratio'
)
->addOption(
'fab',
'f',
InputOption::VALUE_NONE,
'Make the output fabulous'
)
->addOption(
'invert',
'i',
InputOption::VALUE_NONE,
'Invert colors'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$comic = $input->getArgument('comic');
$url = $comic == 'random' ? 'http://c.xkcd.com/random/comic/' : "http://xkcd.com/$comic/";
$client = new Client();
$crawler = $client->request('GET', $url);
try {
$image = $crawler->filter('#comic > img')->attr('src');
}
catch(InvalidArgumentException $exception) {
throw new RuntimeException('No comic found on: ' . $url);
}
list($terminalWidth, $terminalHeight) = $this->getApplication()->getTerminalDimensions();
$printer = new ImagePrinter(
$image,
$input->getOption('threshold'),
$input->getOption('ratio'),
$input->getOption('invert'),
$input->getOption('fab')
);
$printer->run($terminalWidth, $terminalHeight);
}
}
$console = new Application();
$console->run();