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\ConsoleInterface as Console;
10: use Salient\Contract\Console\ConsoleTag as Tag;
11: use Salient\Contract\Console\ConsoleTargetStreamInterface;
12: use Salient\Contract\HasEscapeSequence;
13:
14: /**
15: * Base class for console output targets with an underlying PHP stream
16: */
17: abstract class ConsoleStreamTarget extends ConsolePrefixTarget implements
18: ConsoleTargetStreamInterface,
19: HasEscapeSequence
20: {
21: /**
22: * @inheritDoc
23: */
24: public function isStdout(): bool
25: {
26: return false;
27: }
28:
29: /**
30: * @inheritDoc
31: */
32: public function isStderr(): bool
33: {
34: return false;
35: }
36:
37: /**
38: * @inheritDoc
39: */
40: public function isTty(): bool
41: {
42: return false;
43: }
44:
45: /**
46: * @inheritDoc
47: */
48: public function getEol(): string
49: {
50: return "\n";
51: }
52:
53: protected function createTagFormats(): TagFormats
54: {
55: if (!$this->isTty()) {
56: return new TagFormats();
57: }
58:
59: $bold = Format::ttyBold();
60: $dim = Format::ttyDim();
61: $boldCyan = Format::ttyBold(self::CYAN_FG);
62: $red = Format::ttyColour(self::RED_FG);
63: $green = Format::ttyColour(self::GREEN_FG);
64: $yellow = Format::ttyColour(self::YELLOW_FG);
65: $cyan = Format::ttyColour(self::CYAN_FG);
66: $yellowUnderline = Format::ttyUnderline(self::YELLOW_FG);
67:
68: return (new TagFormats())
69: ->withFormat(Tag::HEADING, $boldCyan)
70: ->withFormat(Tag::BOLD, $bold)
71: ->withFormat(Tag::ITALIC, $yellow)
72: ->withFormat(Tag::UNDERLINE, $yellowUnderline)
73: ->withFormat(Tag::LOW_PRIORITY, $dim)
74: ->withFormat(Tag::CODE_SPAN, $bold)
75: ->withFormat(Tag::DIFF_HEADER, $bold)
76: ->withFormat(Tag::DIFF_RANGE, $cyan)
77: ->withFormat(Tag::DIFF_ADDITION, $green)
78: ->withFormat(Tag::DIFF_REMOVAL, $red);
79: }
80:
81: protected function createMessageFormats(): MessageFormats
82: {
83: if (!$this->isTty()) {
84: return new MessageFormats();
85: }
86:
87: $default = Format::getDefaultFormat();
88: $bold = Format::ttyBold();
89: $dim = Format::ttyDim();
90: $boldRed = Format::ttyBold(self::RED_FG);
91: $boldGreen = Format::ttyBold(self::GREEN_FG);
92: $boldYellow = Format::ttyBold(self::YELLOW_FG);
93: $boldMagenta = Format::ttyBold(self::MAGENTA_FG);
94: $boldCyan = Format::ttyBold(self::CYAN_FG);
95: $green = Format::ttyColour(self::GREEN_FG);
96: $yellow = Format::ttyColour(self::YELLOW_FG);
97: $cyan = Format::ttyColour(self::CYAN_FG);
98:
99: return (new MessageFormats())
100: ->set(Console::LEVELS_ERRORS, Console::TYPES_ALL, new MessageFormat($boldRed, $default, $boldRed))
101: ->set(Console::LEVEL_WARNING, Console::TYPES_ALL, new MessageFormat($yellow, $default, $boldYellow))
102: ->set(Console::LEVEL_NOTICE, Console::TYPES_ALL, new MessageFormat($bold, $cyan, $boldCyan))
103: ->set(Console::LEVEL_INFO, Console::TYPES_ALL, new MessageFormat($default, $yellow, $yellow))
104: ->set(Console::LEVEL_DEBUG, Console::TYPES_ALL, new MessageFormat($dim, $dim, $dim))
105: ->set(Console::LEVELS_INFO, Console::TYPE_PROGRESS, new MessageFormat($default, $yellow, $yellow))
106: ->set(Console::LEVELS_INFO, Console::TYPES_GROUP, new MessageFormat($boldMagenta, $default, $boldMagenta))
107: ->set(Console::LEVELS_INFO, Console::TYPE_SUMMARY, new MessageFormat($default, $default, $bold))
108: ->set(Console::LEVELS_INFO, Console::TYPE_SUCCESS, new MessageFormat($green, $default, $boldGreen))
109: ->set(Console::LEVELS_ERRORS_AND_WARNINGS, Console::TYPE_FAILURE, new MessageFormat($yellow, $default, $boldYellow));
110: }
111: }
112: