Skip to content

Add clickable Link prompt #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions playground/link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use function Laravel\Prompts\link;

require __DIR__.'/../vendor/autoload.php';

link(
message: 'Visit Laravel documentation:',
path: 'https://laravel.com/docs',
tooltip: 'Click here'
);

link(
message: 'Open current file:',
path: __FILE__,
tooltip: 'Click here'
);

link(
message: '<fg=green;options=bold>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'
);
13 changes: 13 additions & 0 deletions src/Concerns/Href.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Laravel\Prompts\Concerns;

trait Href
{
public function href(string $path, ?string $tooltip = null): string
{
$tooltip = $tooltip ?: $path;

return "\033]8;;{$path}\033\\{$tooltip}\033]8;;\033\\";
}
}
3 changes: 3 additions & 0 deletions src/Concerns/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InvalidArgumentException;
use Laravel\Prompts\Clear;
use Laravel\Prompts\ConfirmPrompt;
use Laravel\Prompts\Link;
use Laravel\Prompts\MultiSearchPrompt;
use Laravel\Prompts\MultiSelectPrompt;
use Laravel\Prompts\Note;
Expand All @@ -20,6 +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\MultiSearchPromptRenderer;
use Laravel\Prompts\Themes\Default\MultiSelectPromptRenderer;
use Laravel\Prompts\Themes\Default\NoteRenderer;
Expand Down Expand Up @@ -63,6 +65,7 @@ trait Themes
Table::class => TableRenderer::class,
Progress::class => ProgressRenderer::class,
Clear::class => ClearRenderer::class,
Link::class => LinkRenderer::class,
],
];

Expand Down
40 changes: 40 additions & 0 deletions src/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Laravel\Prompts;

class Link extends Prompt
{
/**
* Create a new link prompt instance.
*/
public function __construct(public string $message, public string $path, public ?string $tooltip = '')
{
//
}

/**
* Display the note.
*/
public function display(): void
{
$this->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;
}
}
50 changes: 50 additions & 0 deletions src/Themes/Default/LinkRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Laravel\Prompts\Themes\Default;

use Laravel\Prompts\Concerns\Href;
use Laravel\Prompts\Link;

class LinkRenderer extends Renderer
{
use Href;

/**
* Render the link.
*/
public function __invoke(Link $link): string
{
$value = $this->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';
}
}
14 changes: 14 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
15 changes: 15 additions & 0 deletions tests/Feature/LinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Laravel\Prompts\Prompt;

use function Laravel\Prompts\link;

it('renders a link', function () {
Prompt::fake();

link('Hello, World!', 'https://example.com', 'title');

Prompt::assertStrippedOutputContains('Hello, World!');
Prompt::assertStrippedOutputContains('https://example.com');
Prompt::assertStrippedOutputContains('title');
});
Loading