Skip to content

Commit

Permalink
Add the ability to create hyperlinks from the URL only to the link_to…
Browse files Browse the repository at this point in the history
… method
  • Loading branch information
piotrmurach committed Aug 30, 2024
1 parent a2d1c1f commit 82177a2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
16 changes: 12 additions & 4 deletions lib/tty/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class Link
# TTY::Link.link_to("TTY Toolkit", "https://ttytoolkit.org")
#
# @example
# TTY::Link.link_to("https://ttytoolkit.org")
#
# @example
# TTY::Link.link_to("TTY Toolkit", "https://ttytoolkit.org",
# env: {"VTE_VERSION" => "7603"})
#
Expand All @@ -49,7 +52,7 @@ class Link
#
# @param [String] name
# the name for the URL
# @param [String] url
# @param [String, nil] url
# the URL target
# @param [ENV, Hash{String => String}] env
# the environment variables
Expand All @@ -61,7 +64,7 @@ class Link
# @see #link_to
#
# @api public
def self.link_to(name, url, env: ENV, output: $stdout)
def self.link_to(name, url = nil, env: ENV, output: $stdout)
new(env: env, output: output).link_to(name, url)
end

Expand Down Expand Up @@ -120,15 +123,20 @@ def initialize(env: ENV, output: $stdout)
# @example
# link.link_to("TTY Toolkit", "https://ttytoolkit.org")
#
# @example
# link.link_to("https://ttytoolkit.org")
#
# @param [String] name
# the name for the URL
# @param [String] url
# @param [String, nil] url
# the URL target
#
# @return [String]
#
# @api public
def link_to(name, url)
def link_to(name, url = nil)
url ||= name

if link?
[OSC8, SEP, SEP, url, BEL, name, OSC8, SEP, SEP, BEL].join
else
Expand Down
42 changes: 40 additions & 2 deletions spec/unit/link_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@

describe ".link_to" do
context "when unsupported terminal" do
it "fails to create a terminal link" do
it "createa a terminal link replacement" do
linked = described_class.link_to(
"TTY Toolkit", "https://ttytoolkit.org", env: {}, output: output
)

expect(linked).to eql("TTY Toolkit -> https://ttytoolkit.org")
end

it "creates a terminal link replacement from a single argument" do
linked = described_class.link_to(
"https://ttytoolkit.org", env: {}, output: output
)

expect(linked).to eql(
"https://ttytoolkit.org -> https://ttytoolkit.org"
)
end
end

context "when supported terminal" do
Expand All @@ -29,17 +39,36 @@
expect(linked)
.to eql("\e]8;;https://ttytoolkit.org\aTTY Toolkit\e]8;;\a")
end

it "creates a terminal link from a single argument" do
linked = described_class.link_to(
"https://ttytoolkit.org", env: env, output: output
)

expect(linked).to eql(
"\e]8;;https://ttytoolkit.org\ahttps://ttytoolkit.org\e]8;;\a"
)
end
end
end

describe "#link_to" do
context "when unsupported terminal" do
it "fails to create a terminal link" do
it "creates a terminal link replacement" do
link = described_class.new(env: {}, output: output)
linked = link.link_to("TTY Toolkit", "https://ttytoolkit.org")

expect(linked).to eql("TTY Toolkit -> https://ttytoolkit.org")
end

it "creates a terminal link replacement from a single argument" do
link = described_class.new(env: {}, output: output)
linked = link.link_to("https://ttytoolkit.org")

expect(linked).to eql(
"https://ttytoolkit.org -> https://ttytoolkit.org"
)
end
end

context "when supported terminal" do
Expand All @@ -50,6 +79,15 @@
expect(linked)
.to eql("\e]8;;https://ttytoolkit.org\aTTY Toolkit\e]8;;\a")
end

it "creates a terminal link from a single argument" do
link = described_class.new(env: env, output: output)
linked = link.link_to("https://ttytoolkit.org")

expect(linked).to eql(
"\e]8;;https://ttytoolkit.org\ahttps://ttytoolkit.org\e]8;;\a"
)
end
end
end
end

0 comments on commit 82177a2

Please sign in to comment.