Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 6027c7a

Browse files
committed
Fixed colouring problems
1 parent c217cab commit 6027c7a

File tree

3 files changed

+66
-24
lines changed

3 files changed

+66
-24
lines changed

src/Handlers/ShellMessagesHandler.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Litipk\JupyterPHP\KernelOutput;
1414
use Monolog\Logger;
15+
use Psy\Configuration;
1516
use Psy\Shell;
1617
use React\ZMQ\SocketWrapper;
1718

@@ -41,26 +42,26 @@ final class ShellMessagesHandler
4142
* @param JupyterBroker $broker
4243
* @param SocketWrapper $iopubSocket
4344
* @param SocketWrapper $shellSocket
44-
* @param Shell $shellSoul
4545
* @param Logger $logger
4646
*/
4747
public function __construct(
48-
JupyterBroker $broker, SocketWrapper $iopubSocket, SocketWrapper $shellSocket, Shell $shellSoul, Logger $logger
48+
JupyterBroker $broker, SocketWrapper $iopubSocket, SocketWrapper $shellSocket, Logger $logger
4949
)
5050
{
51-
$this->executeAction = new ExecuteAction($broker, $iopubSocket, $shellSocket, $shellSoul);
51+
$this->shellSoul = new Shell();
52+
53+
$this->executeAction = new ExecuteAction($broker, $iopubSocket, $shellSocket, $this->shellSoul);
5254
$this->historyAction = new HistoryAction($broker, $shellSocket);
5355
$this->kernelInfoAction = new KernelInfoAction($broker, $shellSocket, $iopubSocket);
5456
$this->shutdownAction = new ShutdownAction($broker, $shellSocket);
55-
56-
$this->shellSoul = $shellSoul;
57+
5758
$this->logger = $logger;
5859

5960
$broker->send(
6061
$iopubSocket, 'status', ['execution_state' => 'starting'], []
6162
);
6263

63-
$this->shellSoul->setOutput(new KernelOutput($this->executeAction, $this->logger->withName('KernelOutput')));
64+
$this->shellSoul->setOutput( new KernelOutput($this->executeAction, $this->logger->withName('KernelOutput')));
6465
}
6566

6667
/**

src/KernelCore.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Litipk\JupyterPHP\Handlers\ShellMessagesHandler;
1111

1212
use Monolog\Logger;
13-
use Psy\Shell;
13+
1414
use React\EventLoop\Factory as ReactFactory;
1515
use React\ZMQ\Context as ReactZmqContext;
1616
use React\ZMQ\SocketWrapper;
@@ -42,9 +42,6 @@ final class KernelCore
4242
/** @var SocketWrapper|\ZMQSocket */
4343
private $shellSocket;
4444

45-
/** @var Shell */
46-
private $shellSoul;
47-
4845
/**
4946
* KernelCore constructor.
5047
* @param JupyterBroker $jupyterBroker
@@ -55,9 +52,7 @@ public function __construct(JupyterBroker $jupyterBroker, array $connUris, Logge
5552
{
5653
$this->broker = $jupyterBroker;
5754
$this->logger = $logger;
58-
59-
$this->shellSoul = new Shell();
60-
55+
6156
$this->initSockets($connUris);
6257
$this->registerHandlers();
6358
}
@@ -120,7 +115,6 @@ private function registerHandlers()
120115
$this->broker,
121116
$this->iopubSocket,
122117
$this->shellSocket,
123-
$this->shellSoul,
124118
$this->logger->withName('ShellMessagesHandler')
125119
)
126120
);

src/KernelOutput.php

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
use Litipk\JupyterPHP\Actions\ExecuteAction;
88
use Psr\Log\LoggerInterface;
9+
use Symfony\Component\Console\Formatter\OutputFormatter;
910
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
11+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
1012
use Symfony\Component\Console\Output\OutputInterface;
1113

1214

@@ -17,6 +19,9 @@ final class KernelOutput implements OutputInterface
1719

1820
/** @var LoggerInterface */
1921
private $logger;
22+
23+
/** @var OutputFormatterInterface */
24+
private $formatter;
2025

2126
/**
2227
* KernelOutput constructor.
@@ -27,6 +32,11 @@ public function __construct(ExecuteAction $executeAction, LoggerInterface $logge
2732
{
2833
$this->executeAction = $executeAction;
2934
$this->logger = $logger;
35+
36+
$this->formatter = new OutputFormatter();
37+
$this->formatter->setDecorated(true);
38+
39+
$this->initFormatters();
3040
}
3141

3242
/**
@@ -36,22 +46,36 @@ public function __construct(ExecuteAction $executeAction, LoggerInterface $logge
3646
* @param bool $newline Whether to add a newline
3747
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
3848
*/
39-
public function write($messages, $newline = false, $options = 0)
49+
public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
4050
{
4151
$this->logger->debug('Write operation inside KernelOutput');
42-
52+
53+
$types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
54+
$type = $types & $options ?: self::OUTPUT_NORMAL;
4355

4456
if (is_string($messages)) {
4557
if ("<aside>⏎</aside>" === $messages) {
4658
return;
4759
}
48-
49-
$this->executeAction->notifyMessage($messages . ($newline ? '' : "\n"));
60+
61+
$preparedMessage = $messages . ($newline ? '' : "\n");
62+
switch ($type) {
63+
case OutputInterface::OUTPUT_NORMAL:
64+
$preparedMessage = $this->formatter->format($messages) . ($newline ? '' : "\n");
65+
break;
66+
case OutputInterface::OUTPUT_RAW:
67+
break;
68+
case OutputInterface::OUTPUT_PLAIN:
69+
$preparedMessage = strip_tags($this->formatter->format($messages)) . ($newline ? '' : "\n");
70+
break;
71+
}
5072
} elseif (is_array($messages)) {
51-
$this->executeAction->notifyMessage(implode("\n", $messages) . ($newline ? '' : "\n"));
73+
$preparedMessage = implode("\n", $messages) . ($newline ? '' : "\n");
5274
} else {
53-
75+
return; // TODO: Throw an error?
5476
}
77+
78+
$this->executeAction->notifyMessage($preparedMessage);
5579
}
5680

5781
/**
@@ -141,7 +165,7 @@ public function setDecorated($decorated)
141165
*/
142166
public function isDecorated()
143167
{
144-
return false;
168+
return true;
145169
}
146170

147171
/**
@@ -155,12 +179,35 @@ public function setFormatter(OutputFormatterInterface $formatter)
155179

156180
/**
157181
* Returns current output formatter instance.
158-
*
159182
* @return OutputFormatterInterface
160183
*/
161184
public function getFormatter()
162185
{
163-
$this->logger->debug('Trying to get a formatter :( .');
164-
// TODO: Implement getFormatter() method.
186+
return $this->formatter;
187+
}
188+
189+
/**
190+
* Initialize output formatter styles.
191+
*/
192+
private function initFormatters()
193+
{
194+
$formatter = $this->getFormatter();
195+
196+
$formatter->setStyle('error', new OutputFormatterStyle('red', null, ['bold']));
197+
$formatter->setStyle('warning', new OutputFormatterStyle('yellow', null, ['bold']));
198+
$formatter->setStyle('aside', new OutputFormatterStyle('blue'));
199+
$formatter->setStyle('strong', new OutputFormatterStyle(null, null, array('bold')));
200+
$formatter->setStyle('return', new OutputFormatterStyle('cyan'));
201+
$formatter->setStyle('urgent', new OutputFormatterStyle('red'));
202+
$formatter->setStyle('hidden', new OutputFormatterStyle('white'));
203+
204+
// Types
205+
$formatter->setStyle('number', new OutputFormatterStyle('magenta'));
206+
$formatter->setStyle('string', new OutputFormatterStyle('green'));
207+
$formatter->setStyle('bool', new OutputFormatterStyle('cyan'));
208+
$formatter->setStyle('keyword', new OutputFormatterStyle('yellow'));
209+
$formatter->setStyle('comment', new OutputFormatterStyle('blue'));
210+
$formatter->setStyle('object', new OutputFormatterStyle('blue'));
211+
$formatter->setStyle('resource', new OutputFormatterStyle('yellow'));
165212
}
166213
}

0 commit comments

Comments
 (0)