1: <?php declare(strict_types=1);
2:
3: namespace Salient\Console\Concept;
4:
5: use Salient\Console\Support\ConsoleFormat as Format;
6: use Salient\Console\Support\ConsoleMessageFormat as MessageFormat;
7: use Salient\Console\Support\ConsoleMessageFormats as MessageFormats;
8: use Salient\Console\Support\ConsoleTagFormats as TagFormats;
9: use Salient\Contract\Console\ConsoleMessageType as MessageType;
10: use Salient\Contract\Console\ConsoleMessageTypeGroup as MessageTypeGroup;
11: use Salient\Contract\Console\ConsoleTag as Tag;
12: use Salient\Contract\Console\ConsoleTargetStreamInterface;
13: use Salient\Contract\Core\EscapeSequence as Colour;
14: use Salient\Contract\Core\MessageLevel as Level;
15: use Salient\Contract\Core\MessageLevelGroup as LevelGroup;
16:
17: /**
18: * Base class for console output targets with an underlying PHP stream
19: */
20: abstract class ConsoleStreamTarget extends ConsolePrefixTarget implements ConsoleTargetStreamInterface
21: {
22: /**
23: * @inheritDoc
24: */
25: public function isStdout(): bool
26: {
27: return false;
28: }
29:
30: /**
31: * @inheritDoc
32: */
33: public function isStderr(): bool
34: {
35: return false;
36: }
37:
38: /**
39: * @inheritDoc
40: */
41: public function isTty(): bool
42: {
43: return false;
44: }
45:
46: /**
47: * @inheritDoc
48: */
49: public function getEol(): string
50: {
51: return "\n";
52: }
53:
54: protected function createTagFormats(): TagFormats
55: {
56: if (!$this->isTty()) {
57: return new TagFormats();
58: }
59:
60: $bold = Format::ttyBold();
61: $dim = Format::ttyDim();
62: $boldCyan = Format::ttyBold(Colour::CYAN);
63: $red = Format::ttyColour(Colour::RED);
64: $green = Format::ttyColour(Colour::GREEN);
65: $yellow = Format::ttyColour(Colour::YELLOW);
66: $cyan = Format::ttyColour(Colour::CYAN);
67: $yellowUnderline = Format::ttyUnderline(Colour::YELLOW);
68:
69: return (new TagFormats())
70: ->withFormat(Tag::HEADING, $boldCyan)
71: ->withFormat(Tag::BOLD, $bold)
72: ->withFormat(Tag::ITALIC, $yellow)
73: ->withFormat(Tag::UNDERLINE, $yellowUnderline)
74: ->withFormat(Tag::LOW_PRIORITY, $dim)
75: ->withFormat(Tag::CODE_SPAN, $bold)
76: ->withFormat(Tag::DIFF_HEADER, $bold)
77: ->withFormat(Tag::DIFF_RANGE, $cyan)
78: ->withFormat(Tag::DIFF_ADDITION, $green)
79: ->withFormat(Tag::DIFF_REMOVAL, $red);
80: }
81:
82: protected function createMessageFormats(): MessageFormats
83: {
84: if (!$this->isTty()) {
85: return new MessageFormats();
86: }
87:
88: $default = Format::getDefaultFormat();
89: $bold = Format::ttyBold();
90: $dim = Format::ttyDim();
91: $boldRed = Format::ttyBold(Colour::RED);
92: $boldGreen = Format::ttyBold(Colour::GREEN);
93: $boldYellow = Format::ttyBold(Colour::YELLOW);
94: $boldMagenta = Format::ttyBold(Colour::MAGENTA);
95: $boldCyan = Format::ttyBold(Colour::CYAN);
96: $green = Format::ttyColour(Colour::GREEN);
97: $yellow = Format::ttyColour(Colour::YELLOW);
98: $cyan = Format::ttyColour(Colour::CYAN);
99:
100: return (new MessageFormats())
101: ->set(LevelGroup::ERRORS, MessageTypeGroup::ALL, new MessageFormat($boldRed, $default, $boldRed))
102: ->set(Level::WARNING, MessageTypeGroup::ALL, new MessageFormat($yellow, $default, $boldYellow))
103: ->set(Level::NOTICE, MessageTypeGroup::ALL, new MessageFormat($bold, $cyan, $boldCyan))
104: ->set(Level::INFO, MessageTypeGroup::ALL, new MessageFormat($default, $yellow, $yellow))
105: ->set(Level::DEBUG, MessageTypeGroup::ALL, new MessageFormat($dim, $dim, $dim))
106: ->set(LevelGroup::INFO, MessageType::PROGRESS, new MessageFormat($default, $yellow, $yellow))
107: ->set(LevelGroup::INFO, MessageTypeGroup::GROUP, new MessageFormat($boldMagenta, $default, $boldMagenta))
108: ->set(LevelGroup::INFO, MessageType::SUMMARY, new MessageFormat($default, $default, $bold))
109: ->set(LevelGroup::INFO, MessageType::SUCCESS, new MessageFormat($green, $default, $boldGreen))
110: ->set(LevelGroup::ERRORS_AND_WARNINGS, MessageType::FAILURE, new MessageFormat($yellow, $default, $boldYellow));
111: }
112: }
113: