diff --git a/lib/tty/link.rb b/lib/tty/link.rb index 83feba1..becaa1b 100644 --- a/lib/tty/link.rb +++ b/lib/tty/link.rb @@ -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"}) # @@ -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 @@ -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 @@ -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 diff --git a/spec/unit/link_to_spec.rb b/spec/unit/link_to_spec.rb index 2f788e4..4df8a93 100644 --- a/spec/unit/link_to_spec.rb +++ b/spec/unit/link_to_spec.rb @@ -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 @@ -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 @@ -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