Skip to content

Commit 1b06e0a

Browse files
committed
Output class
1 parent a7d3605 commit 1b06e0a

2 files changed

Lines changed: 64 additions & 59 deletions

File tree

src/Output.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pest\Profanity;
6+
7+
use function Termwind\render;
8+
use function Termwind\terminal;
9+
10+
class Output
11+
{
12+
public static function successMessage(string $message): void
13+
{
14+
render(<<<HTML
15+
<div class="my-1">
16+
<span class="ml-2 px-1 bg-green font-bold">PASS</span>
17+
<span class="ml-1">$message</span>
18+
</div>
19+
HTML);
20+
}
21+
22+
public static function errorMessage(string $message): void
23+
{
24+
render(<<<HTML
25+
<div class="my-1">
26+
<span class="ml-2 px-1 bg-red font-bold">ERROR</span>
27+
<span class="ml-1">$message</span>
28+
</div>
29+
HTML);
30+
}
31+
32+
public static function pass(string $path): void
33+
{
34+
$truncateAt = max(1, terminal()->width() - 24);
35+
36+
render(<<<HTML
37+
<div class="flex mx-2">
38+
<span class="truncate-{$truncateAt}">$path</span>
39+
<span class="flex-1 content-repeat-[.] text-gray mx-1"></span>
40+
<span class="text-green">OK</span>
41+
</div>
42+
HTML);
43+
}
44+
45+
public static function fail(string $path, string $feedback): void
46+
{
47+
$truncateAt = max(1, terminal()->width() - 24);
48+
49+
render(<<<HTML
50+
<div class="flex mx-2">
51+
<span class="truncate-{$truncateAt}">{$path}</span>
52+
<span class="flex-1 content-repeat-[.] text-gray mx-1"></span>
53+
<span class="text-red">$feedback</span>
54+
</div>
55+
HTML);
56+
}
57+
}

src/Plugin.php

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
use Symfony\Component\Console\Output\OutputInterface;
1515
use Symfony\Component\Finder\Finder;
1616

17-
use function Termwind\render;
1817
use function Termwind\renderUsing;
19-
use function Termwind\terminal;
2018

2119
/**
2220
* @internal
@@ -118,15 +116,7 @@ public function handleOriginalArguments(array $arguments): void
118116
$outputPath = explode('=', $argument)[1] ?? null;
119117

120118
if (empty($outputPath)) {
121-
render(<<<'HTML'
122-
<div class="my-1">
123-
<span class="ml-2 px-1 bg-red font-bold">ERROR</span>
124-
<span class="ml-1">
125-
No output path provided for [--profanity-json].
126-
</span>
127-
</div>
128-
HTML);
129-
119+
Output::errorMessage("No output path provided for [--profanity-json].");
130120
$this->exit(1);
131121
}
132122

@@ -137,14 +127,7 @@ public function handleOriginalArguments(array $arguments): void
137127
$invalidLanguages = $this->validateLanguages($this->language);
138128
if (! empty($invalidLanguages)) {
139129
$invalidLangsStr = implode(', ', $invalidLanguages);
140-
render(<<<HTML
141-
<div class="my-1">
142-
<span class="ml-2 px-1 bg-red font-bold">ERROR</span>
143-
<span class="ml-1">
144-
The specified language does not exist: {$invalidLangsStr}
145-
</span>
146-
</div>
147-
HTML);
130+
Output::errorMessage("The specified language does not exist: $invalidLangsStr");
148131

149132
$this->output->writeln(['']);
150133
$this->output->writeln('<info>Available languages:</info>');
@@ -163,14 +146,7 @@ public function handleOriginalArguments(array $arguments): void
163146
$source = ConfigurationSourceDetector::detect();
164147

165148
if ($source === []) {
166-
render(<<<'HTML'
167-
<div class="my-1">
168-
<span class="ml-2 px-1 bg-red font-bold">ERROR</span>
169-
<span class="ml-1">
170-
No source section found. Did you forget to add a `source` section to your `phpunit.xml` file?
171-
</span>
172-
</div>
173-
HTML);
149+
Output::errorMessage("No source section found. Did you forget to add a `source` section to your `phpunit.xml` file?");
174150

175151
$this->exit(1);
176152
}
@@ -192,18 +168,10 @@ function (Result $result) use (&$filesWithProfanity, &$totalProfanities): void {
192168
$path = str_replace(TestSuite::getInstance()->rootPath.'/', '', $result->file);
193169
$errors = $result->errors;
194170

195-
$truncateAt = max(1, terminal()->width() - 24);
196-
197171
if (empty($errors)) {
198172
if (! $this->compact) {
199173
renderUsing($this->output);
200-
render(<<<HTML
201-
<div class="flex mx-2">
202-
<span class="truncate-{$truncateAt}">{$path}</span>
203-
<span class="flex-1 content-repeat-[.] text-gray mx-1"></span>
204-
<span class="text-green">OK</span>
205-
</div>
206-
HTML);
174+
Output::pass($path);
207175

208176
$this->profanityLogger->append($path, []);
209177
}
@@ -223,13 +191,7 @@ function (Result $result) use (&$filesWithProfanity, &$totalProfanities): void {
223191
$profanityLines = implode(', ', $profanityLines);
224192

225193
renderUsing($this->output);
226-
render(<<<HTML
227-
<div class="flex mx-2">
228-
<span class="truncate-{$truncateAt}">{$path}</span>
229-
<span class="flex-1 content-repeat-[.] text-gray mx-1"></span>
230-
<span class="text-red">{$profanityLines}</span>
231-
</div>
232-
HTML);
194+
Output::fail($path, $profanityLines);
233195
}
234196
},
235197
$this->excludeWords,
@@ -243,23 +205,9 @@ function (Result $result) use (&$filesWithProfanity, &$totalProfanities): void {
243205
$this->profanityLogger->output();
244206

245207
if ($exitCode === 1) {
246-
render(<<<HTML
247-
<div class="my-1">
248-
<span class="ml-2 px-1 bg-red font-bold">ERROR</span>
249-
<span class="ml-1">
250-
Found {$totalProfanities} instances of profanity in {$filesWithProfanityCount} files
251-
</span>
252-
</div>
253-
HTML);
208+
Output::errorMessage("Found $totalProfanities instances of profanity in $filesWithProfanityCount files");
254209
} else {
255-
render(<<<'HTML'
256-
<div class="my-1">
257-
<span class="ml-2 px-1 bg-green font-bold">PASS</span>
258-
<span class="ml-1">
259-
No profanity found in your application!
260-
</span>
261-
</div>
262-
HTML);
210+
Output::successMessage("No profanity found in your application!");
263211
}
264212

265213
$this->output->writeln(['']);

0 commit comments

Comments
 (0)