Skip to content

Commit b53ce6c

Browse files
committed
Hyperlink tweaks
1 parent 1025b29 commit b53ce6c

File tree

6 files changed

+113
-11
lines changed

6 files changed

+113
-11
lines changed

src/mako/application/cli/commands/server/Server.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use mako\application\Application;
1111
use mako\cli\input\arguments\Argument;
1212
use mako\cli\input\arguments\NamedArgument;
13+
use mako\cli\output\components\Hyperlink;
14+
use mako\cli\output\components\hyperlink\Theme;
1315
use mako\reactor\attributes\CommandArguments;
1416
use mako\reactor\attributes\CommandDescription;
1517
use mako\reactor\Command;
@@ -95,8 +97,8 @@ public function execute(
9597
// Tell the user where the server will be running
9698

9799
$message = 'Starting <green>Mako</green> development server at ';
98-
$message .= "<underlined>http://{$host}:{$availablePort}</underlined> ";
99-
$message .= '<yellow>(ctrl+c to stop)</yellow> ...';
100+
$message .= (new Hyperlink($this->output, new Theme('<underlined>%s</underlined>')))->render("http://{$host}:{$availablePort}");
101+
$message .= ' <yellow>(ctrl+c to stop)</yellow> ...';
100102

101103
$this->nl();
102104
$this->write($message);

src/mako/cli/output/components/Hyperlink.php

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

88
namespace mako\cli\output\components;
99

10+
use mako\cli\output\components\hyperlink\Theme;
1011
use mako\cli\output\Output;
1112

1213
use function hash;
@@ -30,7 +31,8 @@ class Hyperlink
3031
* Constructor.
3132
*/
3233
public function __construct(
33-
protected Output $output
34+
protected Output $output,
35+
protected Theme $theme = new Theme
3436
) {
3537
}
3638

@@ -48,7 +50,12 @@ protected function hasHyperlinkSupport(): bool
4850
public function render(string $url, ?string $text = null): string
4951
{
5052
if ($this->hasHyperlinkSupport()) {
51-
return sprintf("\x1b]8;id=%s;%s\x1b\\%s\x1b]8;;\x1b\\", hash('xxh128', $url), $url, $text ?? $url);
53+
return sprintf(
54+
"\x1b]8;id=%s;%s\x1b\\%s\x1b]8;;\x1b\\",
55+
hash('xxh128', $url),
56+
$url,
57+
sprintf($this->theme->getFormat(), $text ?? $url)
58+
);
5259
}
5360

5461
return $text ? "{$text} ({$url})" : $url;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* @copyright Frederic G. Østby
5+
* @license http://www.makoframework.com/license
6+
*/
7+
8+
namespace mako\cli\output\components\hyperlink;
9+
10+
/**
11+
* Ascii theme.
12+
*/
13+
class AsciiTheme extends Theme
14+
{
15+
/**
16+
* Arrow.
17+
*/
18+
protected const string ARROW = '^';
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* @copyright Frederic G. Østby
5+
* @license http://www.makoframework.com/license
6+
*/
7+
8+
namespace mako\cli\output\components\hyperlink;
9+
10+
use function sprintf;
11+
12+
/**
13+
* Base theme.
14+
*/
15+
class Theme
16+
{
17+
/**
18+
* Arrow.
19+
*/
20+
protected const string ARROW = '';
21+
22+
/**
23+
* Constructor.
24+
*/
25+
public function __construct(
26+
protected string $template = '%s'
27+
) {
28+
}
29+
30+
/**
31+
* Returns the format.
32+
*/
33+
public function getFormat(): string
34+
{
35+
return sprintf($this->template, '%s [' . static::ARROW . ']');
36+
}
37+
}

tests/unit/cli/output/components/HyperlinkTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace mako\tests\unit\cli\output\components;
99

1010
use mako\cli\output\components\Hyperlink;
11+
use mako\cli\output\components\hyperlink\Theme;
1112
use mako\cli\output\Output;
1213
use mako\tests\TestCase;
1314
use Mockery;
@@ -35,7 +36,7 @@ public function testRenderWithHyperlinkSupport(): void
3536

3637
$output = Mockery::mock(Output::class);
3738

38-
$hyperlink = Mockery::mock(Hyperlink::class, [$output]);
39+
$hyperlink = Mockery::mock(Hyperlink::class, [$output, new Theme]);
3940

4041
$hyperlink->makePartial();
4142

@@ -47,9 +48,9 @@ public function testRenderWithHyperlinkSupport(): void
4748

4849
$link2 = $hyperlink->render($url, 'Example');
4950

50-
$this->assertSame("\x1b]8;id={$hash};{$url}\x1b\\{$url}\x1b]8;;\x1b\\", $link1);
51+
$this->assertSame("\x1b]8;id={$hash};{$url}\x1b\\{$url} [↗]\x1b]8;;\x1b\\", $link1);
5152

52-
$this->assertSame("\x1b]8;id={$hash};{$url}\x1b\\Example\x1b]8;;\x1b\\", $link2);
53+
$this->assertSame("\x1b]8;id={$hash};{$url}\x1b\\Example [↗]\x1b]8;;\x1b\\", $link2);
5354
}
5455

5556
/**
@@ -87,9 +88,9 @@ public function testDrawWithHyperlinkSupport(): void
8788

8889
$output = Mockery::mock(Output::class);
8990

90-
$output->shouldReceive('write')->once()->with("\x1b]8;id={$hash};{$url}\x1b\\{$url}\x1b]8;;\x1b\\", 1);
91+
$output->shouldReceive('write')->once()->with("\x1b]8;id={$hash};{$url}\x1b\\{$url} [↗]\x1b]8;;\x1b\\", Output::STANDARD);
9192

92-
$output->shouldReceive('write')->once()->with("\x1b]8;id={$hash};{$url}\x1b\\Example\x1b]8;;\x1b\\", 1);
93+
$output->shouldReceive('write')->once()->with("\x1b]8;id={$hash};{$url}\x1b\\Example [↗]\x1b]8;;\x1b\\", Output::STANDARD);
9394

9495
$hyperlink = Mockery::mock(Hyperlink::class, [$output]);
9596

@@ -113,9 +114,9 @@ public function testDrawWithoutHyperlinkSupport(): void
113114

114115
$output = Mockery::mock(Output::class);
115116

116-
$output->shouldReceive('write')->once()->with($url, 1);
117+
$output->shouldReceive('write')->once()->with($url, Output::STANDARD);
117118

118-
$output->shouldReceive('write')->once()->with("Example ({$url})", 1);
119+
$output->shouldReceive('write')->once()->with("Example ({$url})", Output::STANDARD);
119120

120121
$hyperlink = Mockery::mock(Hyperlink::class, [$output]);
121122

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* @copyright Frederic G. Østby
5+
* @license http://www.makoframework.com/license
6+
*/
7+
8+
namespace mako\tests\unit\cli\output\components\hyperlink;
9+
10+
use mako\cli\output\components\hyperlink\Theme;
11+
use mako\tests\TestCase;
12+
use PHPUnit\Framework\Attributes\Group;
13+
14+
#[Group('unit')]
15+
class ThemeTest extends TestCase
16+
{
17+
/**
18+
*
19+
*/
20+
public function testWithDefaultTemplate(): void
21+
{
22+
$theme = new Theme;
23+
24+
$this->assertSame('%s [↗]', $theme->getFormat());
25+
}
26+
27+
/**
28+
*
29+
*/
30+
public function testWithCustomTemplate(): void
31+
{
32+
$theme = new Theme('<underlined>%s</underlined>');
33+
34+
$this->assertSame('<underlined>%s [↗]</underlined>', $theme->getFormat());
35+
}
36+
}

0 commit comments

Comments
 (0)