Skip to content

Commit 005768e

Browse files
Merge pull request #41 from TheDragonCode/1.x
Added extended information output
2 parents b546f55 + 1e1fd5c commit 005768e

File tree

5 files changed

+94
-10
lines changed

5 files changed

+94
-10
lines changed

app/Commands/ReadCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DragonCode\GithubNotifications\Factories\ClientFactory;
66
use DragonCode\GithubNotifications\Services\GitHub;
7+
use DragonCode\GithubNotifications\Services\Output;
78
use Github\ResultPager;
89
use LaravelZero\Framework\Commands\Command;
910
use Symfony\Component\Console\Exception\InvalidOptionException;
@@ -35,13 +36,14 @@ public function handle(): void
3536
protected function welcome(array $repositories): void
3637
{
3738
if ($repositories) {
38-
$this->components->info('You specified the following repository name masks:');
39+
Output::info('You specified the following repository name masks:');
40+
3941
$this->components->bulletList($repositories);
4042

4143
return;
4244
}
4345

44-
$this->components->info('Mark as read all notifications except open ones');
46+
Output::info('Mark as read all notifications except open ones');
4547
}
4648

4749
protected function hasContinue(): bool

app/Services/GitHub.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class GitHub
2525

2626
protected int $index = 0;
2727

28+
protected int $marked = 0;
29+
2830
public function __construct(
2931
protected Factory $output,
3032
protected Client $github,
@@ -66,17 +68,36 @@ public function when(bool $value, Closure $callback, Closure $default): void
6668

6769
public function markAll(): void
6870
{
69-
$this->notifications()->markRead();
71+
$this->output->task('Mark all notifications', fn () => $this->notifications()->markRead());
72+
73+
Output::success('All notifications have been successfully marked.');
7074
}
7175

7276
public function mark(): void
7377
{
7478
if (! $items = $this->paginated()) {
75-
$this->output->info('No unread notifications');
79+
Output::success('No unread notifications');
7680

7781
return;
7882
}
7983

84+
$count = count($items);
85+
86+
Output::info('unread notifications detected', $count);
87+
88+
$this->process($items);
89+
$this->result($count);
90+
}
91+
92+
protected function markAsRead(NotificationData $data): void
93+
{
94+
$this->github->notification()->markThreadRead($data->id);
95+
96+
++$this->marked;
97+
}
98+
99+
protected function process(array $items): void
100+
{
80101
foreach ($items as $data) {
81102
$notification = new NotificationData($data, ++$this->index);
82103

@@ -88,11 +109,6 @@ public function mark(): void
88109
}
89110
}
90111

91-
protected function markAsRead(NotificationData $data): void
92-
{
93-
$this->github->notification()->markThreadRead($data->id);
94-
}
95-
96112
protected function paginated(): array
97113
{
98114
return $this->paginator->fetchAll($this->notifications(), 'all');
@@ -154,4 +170,15 @@ protected function shouldSkip(NotificationData $notification, ItemData $item): b
154170

155171
return $item->isOpen;
156172
}
173+
174+
protected function result(int $count): void
175+
{
176+
$info = sprintf(
177+
'%d notifications were marked as read and %d were skipped.',
178+
$this->marked,
179+
$count - $this->marked
180+
);
181+
182+
$this->marked ? Output::success($info) : Output::info($info);
183+
}
157184
}

app/Services/Output.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\GithubNotifications\Services;
6+
7+
use Illuminate\Support\Str;
8+
9+
use function Termwind\{render};
10+
11+
class Output
12+
{
13+
public static function info(string $value, int|string $title = 'INFO'): void
14+
{
15+
static::line($value, $title, 'blue-300');
16+
}
17+
18+
public static function success(string $value, int|string $title = 'SUCCESS'): void
19+
{
20+
static::line($value, $title, 'green-300');
21+
}
22+
23+
public static function comment(string $value, int|string $title = 'COMMENT'): void
24+
{
25+
static::line($value, $title, 'gray-300');
26+
}
27+
28+
protected static function line(string $value, int|string $title, string $color): void
29+
{
30+
$padding = static::padding($title);
31+
32+
render(
33+
<<<HTML
34+
<div class="py-1 ml-1">
35+
<div class="px-$padding bg-$color text-black">
36+
$title
37+
</div>
38+
<em class="ml-1">
39+
$value
40+
</em>
41+
</div>
42+
HTML
43+
);
44+
}
45+
46+
protected static function padding(int|string $value): int
47+
{
48+
return match (Str::length((string) $value)) {
49+
1, 2 => 3,
50+
3, 4 => 2,
51+
default => 1
52+
};
53+
}
54+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"knplabs/github-api": "^3.14.1",
3939
"laravel-zero/framework": "^10.3",
4040
"mockery/mockery": "^1.6.11",
41+
"nunomaduro/termwind": "^1.15.1",
4142
"pestphp/pest": "^2.34.6"
4243
},
4344
"minimum-stability": "stable",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)