diff --git a/playground/link.php b/playground/link.php new file mode 100644 index 0000000..f849050 --- /dev/null +++ b/playground/link.php @@ -0,0 +1,33 @@ +Visit Laravel Documentation:', + path: 'https://laravel.com/docs', + tooltip: 'Click here' +); + +link( + message: 'Visit Laravel Documentation:', + path: 'https://laravel.com/docs' +); + +link( + message: '', + path: 'https://laravel.com/docs' +); 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..db0f666 --- /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..b6c08ea 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(); + } +} 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 @@ +