From 15b7129ffb1d2041c9b92fb17fb84ceec507d352 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 11 Jun 2025 18:01:45 -0300 Subject: [PATCH 1/9] 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/9] 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/9] 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/9] 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/9] 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(); } } - From c46b9528a1f82a195cd682017cd4264db1c5077b Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Tue, 25 Nov 2025 21:30:51 -0300 Subject: [PATCH 6/9] Add a link function and improve Link component output --- playground/index.php | 5 +++++ src/Link.php | 4 ++++ src/Themes/Default/LinkRenderer.php | 4 ++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/playground/index.php b/playground/index.php index d51c33a..883c6c7 100644 --- a/playground/index.php +++ b/playground/index.php @@ -13,6 +13,7 @@ use function Laravel\Prompts\suggest; use function Laravel\Prompts\text; use function Laravel\Prompts\warning; +use function Laravel\Prompts\link; require __DIR__.'/../vendor/autoload.php'; @@ -92,6 +93,10 @@ error('Error'); warning('Warning'); alert('Alert'); +link( + message: 'Visit Laravel Documentation:', + path: 'https://laravel.com/docs', +); note(<<capturePreviousNewLines(); + + $this->state = 'submit'; + static::output()->write($this->renderTheme()); return true; diff --git a/src/Themes/Default/LinkRenderer.php b/src/Themes/Default/LinkRenderer.php index db0f666..ca569b7 100644 --- a/src/Themes/Default/LinkRenderer.php +++ b/src/Themes/Default/LinkRenderer.php @@ -20,9 +20,9 @@ public function __invoke(Link $link): string ); if ($link->message) { - $this->line("{$link->message} {$value}"); + $this->line(" {$this->blue(" {$link->message} {$value}")}"); } else { - $this->line("{$value}"); + $this->line(" {$this->blue(" {$value}")}"); } return $this; From 767567f8f9f80da56bdb30521c72fda1db386192 Mon Sep 17 00:00:00 2001 From: luanfreitasdev <33601626+luanfreitasdev@users.noreply.github.com> Date: Wed, 26 Nov 2025 00:31:16 +0000 Subject: [PATCH 7/9] Fix code styling --- playground/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playground/index.php b/playground/index.php index 883c6c7..ecbca75 100644 --- a/playground/index.php +++ b/playground/index.php @@ -4,6 +4,7 @@ use function Laravel\Prompts\confirm; use function Laravel\Prompts\error; use function Laravel\Prompts\intro; +use function Laravel\Prompts\link; use function Laravel\Prompts\multiselect; use function Laravel\Prompts\note; use function Laravel\Prompts\outro; @@ -13,7 +14,6 @@ use function Laravel\Prompts\suggest; use function Laravel\Prompts\text; use function Laravel\Prompts\warning; -use function Laravel\Prompts\link; require __DIR__.'/../vendor/autoload.php'; From 70ad6e0556ab9a1ba393ee8616d1428286d1f9b1 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Thu, 27 Nov 2025 06:34:39 -0300 Subject: [PATCH 8/9] Rename Link to Href and update related functions and classes --- playground/{link.php => href.php} | 12 +++++----- playground/index.php | 4 ++-- src/Concerns/Themes.php | 6 ++--- src/{Link.php => Href.php} | 4 ++-- .../{LinkRenderer.php => HrefRenderer.php} | 20 ++++++++-------- src/helpers.php | 8 +++---- tests/Feature/HrefTest.php | 24 +++++++++++++++++++ tests/Feature/LinkTest.php | 15 ------------ 8 files changed, 51 insertions(+), 42 deletions(-) rename playground/{link.php => href.php} (89%) rename src/{Link.php => Href.php} (90%) rename src/Themes/Default/{LinkRenderer.php => HrefRenderer.php} (66%) create mode 100644 tests/Feature/HrefTest.php delete mode 100644 tests/Feature/LinkTest.php diff --git a/playground/link.php b/playground/href.php similarity index 89% rename from playground/link.php rename to playground/href.php index f849050..254dc5d 100644 --- a/playground/link.php +++ b/playground/href.php @@ -1,33 +1,33 @@ Visit Laravel Documentation:', path: 'https://laravel.com/docs', tooltip: 'Click here' ); -link( +href( message: 'Visit Laravel Documentation:', path: 'https://laravel.com/docs' ); -link( +href( message: '', path: 'https://laravel.com/docs' ); diff --git a/playground/index.php b/playground/index.php index ecbca75..d2c48ef 100644 --- a/playground/index.php +++ b/playground/index.php @@ -4,7 +4,7 @@ use function Laravel\Prompts\confirm; use function Laravel\Prompts\error; use function Laravel\Prompts\intro; -use function Laravel\Prompts\link; +use function Laravel\Prompts\href; use function Laravel\Prompts\multiselect; use function Laravel\Prompts\note; use function Laravel\Prompts\outro; @@ -93,7 +93,7 @@ error('Error'); warning('Warning'); alert('Alert'); -link( +href( message: 'Visit Laravel Documentation:', path: 'https://laravel.com/docs', ); diff --git a/src/Concerns/Themes.php b/src/Concerns/Themes.php index 4b21153..0459874 100644 --- a/src/Concerns/Themes.php +++ b/src/Concerns/Themes.php @@ -5,7 +5,7 @@ use InvalidArgumentException; use Laravel\Prompts\Clear; use Laravel\Prompts\ConfirmPrompt; -use Laravel\Prompts\Link; +use Laravel\Prompts\Href; use Laravel\Prompts\MultiSearchPrompt; use Laravel\Prompts\MultiSelectPrompt; use Laravel\Prompts\Note; @@ -21,7 +21,7 @@ use Laravel\Prompts\TextPrompt; use Laravel\Prompts\Themes\Default\ClearRenderer; use Laravel\Prompts\Themes\Default\ConfirmPromptRenderer; -use Laravel\Prompts\Themes\Default\LinkRenderer; +use Laravel\Prompts\Themes\Default\HrefRenderer; use Laravel\Prompts\Themes\Default\MultiSearchPromptRenderer; use Laravel\Prompts\Themes\Default\MultiSelectPromptRenderer; use Laravel\Prompts\Themes\Default\NoteRenderer; @@ -65,7 +65,7 @@ trait Themes Table::class => TableRenderer::class, Progress::class => ProgressRenderer::class, Clear::class => ClearRenderer::class, - Link::class => LinkRenderer::class, + Href::class => HrefRenderer::class, ], ]; diff --git a/src/Link.php b/src/Href.php similarity index 90% rename from src/Link.php rename to src/Href.php index e89055f..e4cd301 100644 --- a/src/Link.php +++ b/src/Href.php @@ -2,10 +2,10 @@ namespace Laravel\Prompts; -class Link extends Prompt +class Href extends Prompt { /** - * Create a new link prompt instance. + * Create a new href prompt instance. */ public function __construct(public string $message, public string $path, public ?string $tooltip = '') { diff --git a/src/Themes/Default/LinkRenderer.php b/src/Themes/Default/HrefRenderer.php similarity index 66% rename from src/Themes/Default/LinkRenderer.php rename to src/Themes/Default/HrefRenderer.php index ca569b7..4ec385e 100644 --- a/src/Themes/Default/LinkRenderer.php +++ b/src/Themes/Default/HrefRenderer.php @@ -2,25 +2,25 @@ namespace Laravel\Prompts\Themes\Default; -use Laravel\Prompts\Concerns\Href; -use Laravel\Prompts\Link; +use Laravel\Prompts\Concerns\Href as HrefConcern; +use Laravel\Prompts\Href; -class LinkRenderer extends Renderer +class HrefRenderer extends Renderer { - use Href; + use HrefConcern; /** - * Render the link. + * Render the href. */ - public function __invoke(Link $link): string + public function __invoke(Href $href): string { $value = $this->href( - $this->convertPathToUri($link->path), - $link->tooltip + $this->convertPathToUri($href->path), + $href->tooltip ); - if ($link->message) { - $this->line(" {$this->blue(" {$link->message} {$value}")}"); + if ($href->message) { + $this->line(" {$this->blue(" {$href->message} {$value}")}"); } else { $this->line(" {$this->blue(" {$value}")}"); } diff --git a/src/helpers.php b/src/helpers.php index b6c08ea..af50d87 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -332,16 +332,16 @@ function form(): FormBuilder } } -if (! function_exists('\Laravel\Prompts\link')) { +if (! function_exists('\Laravel\Prompts\href')) { /** - * Display a link. + * Display a link to the given file path or URL. * * @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 + function href(string $message, string $path, ?string $tooltip = null): void { - (new Link($message, $path, $tooltip))->display(); + (new Href($message, $path, $tooltip))->display(); } } diff --git a/tests/Feature/HrefTest.php b/tests/Feature/HrefTest.php new file mode 100644 index 0000000..e172521 --- /dev/null +++ b/tests/Feature/HrefTest.php @@ -0,0 +1,24 @@ + Date: Thu, 27 Nov 2025 09:35:25 +0000 Subject: [PATCH 9/9] Fix code styling --- playground/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playground/index.php b/playground/index.php index d2c48ef..7fe2e05 100644 --- a/playground/index.php +++ b/playground/index.php @@ -3,8 +3,8 @@ use function Laravel\Prompts\alert; use function Laravel\Prompts\confirm; use function Laravel\Prompts\error; -use function Laravel\Prompts\intro; use function Laravel\Prompts\href; +use function Laravel\Prompts\intro; use function Laravel\Prompts\multiselect; use function Laravel\Prompts\note; use function Laravel\Prompts\outro;