1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Console\Format; |
4: | |
5: | use Salient\Contract\HasEscapeSequence; |
6: | |
7: | |
8: | |
9: | |
10: | class TtyFormat extends AbstractFormat implements HasEscapeSequence |
11: | { |
12: | use EncloseAndReplaceTrait; |
13: | use IndentCodeBlockTrait; |
14: | |
15: | |
16: | |
17: | |
18: | public function apply(string $string, $attributes = null): string |
19: | { |
20: | if ($string === '') { |
21: | return ''; |
22: | } |
23: | |
24: | $string = $this->indentCodeBlock($string, $attributes); |
25: | |
26: | return $this->encloseAndReplace($string); |
27: | } |
28: | |
29: | |
30: | |
31: | |
32: | protected static function getTagFormats(): ?TagFormats |
33: | { |
34: | $bold = self::getBold(); |
35: | $faint = self::getFaint(); |
36: | $boldCyan = self::getBold(self::CYAN_FG); |
37: | $red = self::getColour(self::RED_FG); |
38: | $green = self::getColour(self::GREEN_FG); |
39: | $yellow = self::getColour(self::YELLOW_FG); |
40: | $cyan = self::getColour(self::CYAN_FG); |
41: | $yellowUnderline = self::getUnderline(self::YELLOW_FG); |
42: | |
43: | return (new TagFormats()) |
44: | ->withFormat(self::TAG_HEADING, $boldCyan) |
45: | ->withFormat(self::TAG_BOLD, $bold) |
46: | ->withFormat(self::TAG_ITALIC, $yellow) |
47: | ->withFormat(self::TAG_UNDERLINE, $yellowUnderline) |
48: | ->withFormat(self::TAG_LOW_PRIORITY, $faint) |
49: | ->withFormat(self::TAG_CODE_SPAN, $bold) |
50: | ->withFormat(self::TAG_DIFF_HEADER, $bold) |
51: | ->withFormat(self::TAG_DIFF_RANGE, $cyan) |
52: | ->withFormat(self::TAG_DIFF_ADDITION, $green) |
53: | ->withFormat(self::TAG_DIFF_REMOVAL, $red); |
54: | } |
55: | |
56: | |
57: | |
58: | |
59: | protected static function getMessageFormats(): ?MessageFormats |
60: | { |
61: | $null = new NullFormat(); |
62: | $bold = self::getBold(); |
63: | $faint = self::getFaint(); |
64: | $boldRed = self::getBold(self::RED_FG); |
65: | $boldGreen = self::getBold(self::GREEN_FG); |
66: | $boldYellow = self::getBold(self::YELLOW_FG); |
67: | $boldMagenta = self::getBold(self::MAGENTA_FG); |
68: | $boldCyan = self::getBold(self::CYAN_FG); |
69: | $green = self::getColour(self::GREEN_FG); |
70: | $yellow = self::getColour(self::YELLOW_FG); |
71: | $cyan = self::getColour(self::CYAN_FG); |
72: | |
73: | $boldRedNull = new MessageFormat($boldRed, $null, $boldRed); |
74: | $yellowNullBoldYellow = new MessageFormat($yellow, $null, $boldYellow); |
75: | $boldCyanBoldCyan = new MessageFormat($bold, $cyan, $boldCyan); |
76: | $nullYellowYellow = new MessageFormat($null, $yellow, $yellow); |
77: | $faint = new MessageFormat($faint, $faint, $faint); |
78: | $boldMagentaNull = new MessageFormat($boldMagenta, $null, $boldMagenta); |
79: | $nullNullBold = new MessageFormat($null, $null, $bold); |
80: | $greenNullBoldGreen = new MessageFormat($green, $null, $boldGreen); |
81: | |
82: | return (new MessageFormats()) |
83: | ->withFormat(self::LEVELS_ERRORS, self::TYPES_ALL, $boldRedNull) |
84: | ->withFormat(self::LEVEL_WARNING, self::TYPES_ALL, $yellowNullBoldYellow) |
85: | ->withFormat(self::LEVEL_NOTICE, self::TYPES_ALL, $boldCyanBoldCyan) |
86: | ->withFormat(self::LEVEL_INFO, self::TYPES_ALL, $nullYellowYellow) |
87: | ->withFormat(self::LEVEL_DEBUG, self::TYPES_ALL, $faint) |
88: | ->withFormat(self::LEVELS_INFO, self::TYPE_PROGRESS, $nullYellowYellow) |
89: | ->withFormat(self::LEVELS_INFO, self::TYPES_GROUP, $boldMagentaNull) |
90: | ->withFormat(self::LEVELS_INFO, self::TYPE_SUMMARY, $nullNullBold) |
91: | ->withFormat(self::LEVELS_INFO, self::TYPE_SUCCESS, $greenNullBoldGreen) |
92: | ->withFormat(self::LEVELS_ERRORS_AND_WARNINGS, self::TYPE_FAILURE, $yellowNullBoldYellow); |
93: | } |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | protected static function getColour(string $colour): self |
101: | { |
102: | return new self( |
103: | $colour, |
104: | self::DEFAULT_FG, |
105: | [ |
106: | self::DEFAULT_FG => $colour, |
107: | ], |
108: | ); |
109: | } |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | protected static function getBold(?string $colour = null): self |
118: | { |
119: | return $colour !== null |
120: | ? new self( |
121: | self::BOLD . $colour, |
122: | self::DEFAULT_FG . self::NOT_BOLD_NOT_FAINT, |
123: | [ |
124: | self::NOT_BOLD_NOT_FAINT => self::BOLD_NOT_FAINT, |
125: | self::DEFAULT_FG => $colour, |
126: | ], |
127: | ) |
128: | : new self( |
129: | self::BOLD, |
130: | self::NOT_BOLD_NOT_FAINT, |
131: | [ |
132: | self::NOT_BOLD_NOT_FAINT => self::BOLD_NOT_FAINT, |
133: | ], |
134: | ); |
135: | } |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | protected static function getFaint(?string $colour = null): self |
144: | { |
145: | return $colour !== null |
146: | ? new self( |
147: | self::FAINT . $colour, |
148: | self::DEFAULT_FG . self::NOT_BOLD_NOT_FAINT, |
149: | [ |
150: | self::NOT_BOLD_NOT_FAINT => self::FAINT_NOT_BOLD, |
151: | self::DEFAULT_FG => $colour, |
152: | ], |
153: | ) |
154: | : new self( |
155: | self::FAINT, |
156: | self::NOT_BOLD_NOT_FAINT, |
157: | [ |
158: | self::NOT_BOLD_NOT_FAINT => self::FAINT_NOT_BOLD, |
159: | ], |
160: | ); |
161: | } |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | |
170: | |
171: | |
172: | protected static function getBoldFaint(?string $colour = null): self |
173: | { |
174: | return $colour !== null |
175: | ? new self( |
176: | self::BOLD . self::FAINT . $colour, |
177: | self::DEFAULT_FG . self::NOT_BOLD_NOT_FAINT, |
178: | [ |
179: | self::NOT_BOLD_NOT_FAINT => self::BOLD . self::FAINT, |
180: | self::DEFAULT_FG => $colour, |
181: | ], |
182: | ) |
183: | : new self( |
184: | self::BOLD . self::FAINT, |
185: | self::NOT_BOLD_NOT_FAINT, |
186: | [ |
187: | self::NOT_BOLD_NOT_FAINT => self::BOLD . self::FAINT, |
188: | ], |
189: | ); |
190: | } |
191: | |
192: | |
193: | |
194: | |
195: | |
196: | |
197: | |
198: | protected static function getUnderline(?string $colour = null): self |
199: | { |
200: | return $colour !== null |
201: | ? new self( |
202: | $colour . self::UNDERLINED, |
203: | self::NOT_UNDERLINED . self::DEFAULT_FG, |
204: | [ |
205: | self::DEFAULT_FG => $colour, |
206: | self::NOT_UNDERLINED => '', |
207: | ], |
208: | ) |
209: | : new self( |
210: | self::UNDERLINED, |
211: | self::NOT_UNDERLINED, |
212: | [ |
213: | self::NOT_UNDERLINED => '', |
214: | ], |
215: | ); |
216: | } |
217: | } |
218: | |