diff --git a/playground/href.php b/playground/href.php new file mode 100644 index 0000000..254dc5d --- /dev/null +++ b/playground/href.php @@ -0,0 +1,33 @@ +Visit Laravel Documentation:', + path: 'https://laravel.com/docs', + tooltip: 'Click here' +); + +href( + message: 'Visit Laravel Documentation:', + path: 'https://laravel.com/docs' +); + +href( + message: '', + path: 'https://laravel.com/docs' +); diff --git a/playground/index.php b/playground/index.php index d51c33a..7fe2e05 100644 --- a/playground/index.php +++ b/playground/index.php @@ -3,6 +3,7 @@ use function Laravel\Prompts\alert; use function Laravel\Prompts\confirm; use function Laravel\Prompts\error; +use function Laravel\Prompts\href; use function Laravel\Prompts\intro; use function Laravel\Prompts\multiselect; use function Laravel\Prompts\note; @@ -92,6 +93,10 @@ error('Error'); warning('Warning'); alert('Alert'); +href( + message: 'Visit Laravel Documentation:', + path: 'https://laravel.com/docs', +); note(<< TableRenderer::class, Progress::class => ProgressRenderer::class, Clear::class => ClearRenderer::class, + Href::class => HrefRenderer::class, ], ]; diff --git a/src/Href.php b/src/Href.php new file mode 100644 index 0000000..e4cd301 --- /dev/null +++ b/src/Href.php @@ -0,0 +1,44 @@ +prompt(); + } + + /** + * Display the note. + */ + public function prompt(): bool + { + $this->capturePreviousNewLines(); + + $this->state = 'submit'; + + 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/HrefRenderer.php b/src/Themes/Default/HrefRenderer.php new file mode 100644 index 0000000..4ec385e --- /dev/null +++ b/src/Themes/Default/HrefRenderer.php @@ -0,0 +1,50 @@ +href( + $this->convertPathToUri($href->path), + $href->tooltip + ); + + if ($href->message) { + $this->line(" {$this->blue(" {$href->message} {$value}")}"); + } else { + $this->line(" {$this->blue(" {$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..af50d87 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -331,3 +331,17 @@ function form(): FormBuilder return new FormBuilder; } } + +if (! function_exists('\Laravel\Prompts\href')) { + /** + * 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 href(string $message, string $path, ?string $tooltip = null): void + { + (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 @@ +