From 15b7129ffb1d2041c9b92fb17fb84ceec507d352 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 11 Jun 2025 18:01:45 -0300 Subject: [PATCH 1/5] Add Link component --- src/Concerns/Href.php | 13 ++++++++ src/Concerns/Themes.php | 3 ++ src/Link.php | 40 +++++++++++++++++++++++ src/Themes/Default/LinkRenderer.php | 50 +++++++++++++++++++++++++++++ src/helpers.php | 14 ++++++++ 5 files changed, 120 insertions(+) create mode 100644 src/Concerns/Href.php create mode 100644 src/Link.php create mode 100644 src/Themes/Default/LinkRenderer.php diff --git a/src/Concerns/Href.php b/src/Concerns/Href.php new file mode 100644 index 0000000..cf77747 --- /dev/null +++ b/src/Concerns/Href.php @@ -0,0 +1,13 @@ + TableRenderer::class, Progress::class => ProgressRenderer::class, Clear::class => ClearRenderer::class, + Link::class => LinkRenderer::class, ], ]; diff --git a/src/Link.php b/src/Link.php new file mode 100644 index 0000000..6fa7e46 --- /dev/null +++ b/src/Link.php @@ -0,0 +1,40 @@ +prompt(); + } + + /** + * Display the note. + */ + public function prompt(): bool + { + static::output()->write($this->renderTheme()); + + return true; + } + + /** + * Get the value of the prompt. + */ + public function value(): bool + { + return true; + } +} diff --git a/src/Themes/Default/LinkRenderer.php b/src/Themes/Default/LinkRenderer.php new file mode 100644 index 0000000..1368b8a --- /dev/null +++ b/src/Themes/Default/LinkRenderer.php @@ -0,0 +1,50 @@ +href( + $this->convertPathToUri($link->path), + $link->tooltip + ); + + if ($link->message) { + $this->line("{$link->message} {$value}"); + } else { + $this->line("{$value}"); + } + + return $this; + } + + protected function convertPathToUri(string $path): string + { + if (str_starts_with(strtolower($path), 'file://')) { + return $path; + } + + if (preg_match('/^[a-z]+:\/\//i', $path)) { + return $path; + } + + $path = '/' . ltrim(strtr($path, '\\', '/'), '/'); + + return $this->isVSCode() ? "vscode://file{$path}" : "file://{$path}"; + } + + protected function isVSCode(): bool + { + return ($_SERVER['TERM_PROGRAM'] ?? null) === "vscode"; + } +} diff --git a/src/helpers.php b/src/helpers.php index 0da5543..18e8e90 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -331,3 +331,17 @@ function form(): FormBuilder return new FormBuilder; } } + +if (! function_exists('\Laravel\Prompts\link')) { + /** + * Display a link. + * @param string $message The message to display for the link. + * @param string $path The file path or URL the link points to. + * @param ?string $tooltip The tooltip text that appears on the link. + */ + function link(string $message, string $path, ?string $tooltip = null): void + { + (new Link($message, $path, $tooltip))->display(); + } +} + From 4fa77b82ca9aa32a10dd3b0034a5fe9126c63d55 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 11 Jun 2025 18:01:54 -0300 Subject: [PATCH 2/5] Add playground --- playground/link.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 playground/link.php diff --git a/playground/link.php b/playground/link.php new file mode 100644 index 0000000..7e2798c --- /dev/null +++ b/playground/link.php @@ -0,0 +1,33 @@ +Visit Laravel Documentation:', + path: 'https://laravel.com/docs', + tooltip: 'Click here' +); + +link( + message: '', + path: 'https://laravel.com/docs' +); + +link( + message: 'Visit Laravel Documentation:', + path: 'https://laravel.com/docs' +); From 7fe7f25288950efa34bb77bfdccd9581a2168542 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 11 Jun 2025 18:02:08 -0300 Subject: [PATCH 3/5] Add test --- tests/Feature/LinkTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/Feature/LinkTest.php diff --git a/tests/Feature/LinkTest.php b/tests/Feature/LinkTest.php new file mode 100644 index 0000000..555894a --- /dev/null +++ b/tests/Feature/LinkTest.php @@ -0,0 +1,15 @@ + Date: Wed, 11 Jun 2025 18:06:43 -0300 Subject: [PATCH 4/5] Fix link message for Laravel Documentation --- playground/link.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/playground/link.php b/playground/link.php index 7e2798c..3212cb8 100644 --- a/playground/link.php +++ b/playground/link.php @@ -23,11 +23,11 @@ ); link( - message: '', + message: 'Visit Laravel Documentation:', path: 'https://laravel.com/docs' ); link( - message: 'Visit Laravel Documentation:', + message: '', path: 'https://laravel.com/docs' ); From 1d5d214e81be4e194455c981c98eff91fddd98a9 Mon Sep 17 00:00:00 2001 From: luanfreitasdev <33601626+luanfreitasdev@users.noreply.github.com> Date: Wed, 11 Jun 2025 21:07:14 +0000 Subject: [PATCH 5/5] Fix code styling --- playground/link.php | 2 +- src/Themes/Default/LinkRenderer.php | 4 ++-- src/helpers.php | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/playground/link.php b/playground/link.php index 3212cb8..f849050 100644 --- a/playground/link.php +++ b/playground/link.php @@ -18,7 +18,7 @@ link( message: 'Visit Laravel Documentation:', - path: 'https://laravel.com/docs', + path: 'https://laravel.com/docs', tooltip: 'Click here' ); diff --git a/src/Themes/Default/LinkRenderer.php b/src/Themes/Default/LinkRenderer.php index 1368b8a..db0f666 100644 --- a/src/Themes/Default/LinkRenderer.php +++ b/src/Themes/Default/LinkRenderer.php @@ -38,13 +38,13 @@ protected function convertPathToUri(string $path): string return $path; } - $path = '/' . ltrim(strtr($path, '\\', '/'), '/'); + $path = '/'.ltrim(strtr($path, '\\', '/'), '/'); return $this->isVSCode() ? "vscode://file{$path}" : "file://{$path}"; } protected function isVSCode(): bool { - return ($_SERVER['TERM_PROGRAM'] ?? null) === "vscode"; + return ($_SERVER['TERM_PROGRAM'] ?? null) === 'vscode'; } } diff --git a/src/helpers.php b/src/helpers.php index 18e8e90..b6c08ea 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -335,13 +335,13 @@ function form(): FormBuilder if (! function_exists('\Laravel\Prompts\link')) { /** * Display a link. + * * @param string $message The message to display for the link. - * @param string $path The file path or URL the link points to. - * @param ?string $tooltip The tooltip text that appears on the link. + * @param string $path The file path or URL the link points to. + * @param ?string $tooltip The tooltip text that appears on the link. */ function link(string $message, string $path, ?string $tooltip = null): void { (new Link($message, $path, $tooltip))->display(); } } -