Skip to content

Commit

Permalink
Add TTY::Link::Terminals::Wezterm to detect hyperlinks support in Wez…
Browse files Browse the repository at this point in the history
…Term
  • Loading branch information
piotrmurach committed Aug 27, 2024
1 parent 2631e86 commit b9ae09c
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
71 changes: 71 additions & 0 deletions lib/tty/link/terminals/wezterm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

require_relative "abstract"

module TTY
class Link
module Terminals
# Responsible for detecting hyperlink support in the WezTerm terminal
#
# @api private
class Wezterm < Abstract
# The WezTerm terminal name pattern
#
# @return [Regexp]
#
# @api private
WEZTERM = /WezTerm/i.freeze
private_constant :WEZTERM

# The version release
#
# @return [String]
#
# @api private
VERSION_RELEASE = "20180218"
private_constant :VERSION_RELEASE

# The version separator
#
# @return [String]
#
# @api private
VERSION_SEPARATOR = "-"
private_constant :VERSION_SEPARATOR

private

# Detect WezTerm terminal
#
# @example
# wezterm.name?
# # => true
#
# @return [Boolean]
#
# @api private
def name?
!(term_program =~ WEZTERM).nil?
end

# Detect whether the WezTerm version supports terminal hyperlinks
#
# @example
# wezterm.version?
# # => true
#
# @return [Boolean]
#
# @api private
def version?
return false unless term_program_version

current_semantic_version =
semantic_version(term_program_version, separator: VERSION_SEPARATOR)

current_semantic_version >= semantic_version(VERSION_RELEASE, 0, 0)
end
end # Wezterm
end # Terminals
end # Link
end # TTY
12 changes: 12 additions & 0 deletions spec/unit/link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,17 @@
expect(link.link?).to eq(true)
end
end

context "when WezTerm" do
it "supports links above the 20180218 version" do
env = {
"TERM_PROGRAM" => "WezTerm",
"TERM_PROGRAM_VERSION" => "20180218-123-abc"
}
link = described_class.new(env: env, output: output)

expect(link.link?).to eq(true)
end
end
end
end
70 changes: 70 additions & 0 deletions spec/unit/terminals/wezterm_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# frozen_string_literal: true

RSpec.describe TTY::Link::Terminals::Wezterm, "#link?" do
let(:env_with_name) { {"TERM_PROGRAM" => "WezTerm"} }
let(:semantic_version) { TTY::Link::SemanticVersion }

it "doesn't support links without the term program environment variable" do
wezterm = described_class.new(semantic_version, {})

expect(wezterm.link?).to eq(false)
end

it "doesn't support links without a terminal program name" do
env = {"TERM_PROGRAM" => nil}
wezterm = described_class.new(semantic_version, env)

expect(wezterm.link?).to eq(false)
end

it "doesn't support links with a non-WezTerm program name" do
env = {"TERM_PROGRAM" => "other-terminal"}
wezterm = described_class.new(semantic_version, env)

expect(wezterm.link?).to eq(false)
end

it "supports links above the 20240203 version" do
env = {
"TERM_PROGRAM" => "wezterm",
"TERM_PROGRAM_VERSION" => "20250403"
}
wezterm = described_class.new(semantic_version, env)

expect(wezterm.link?).to eq(true)
end

it "supports links above the 20180218 version" do
env = env_with_name.merge({"TERM_PROGRAM_VERSION" => "20180219-123-abc"})
wezterm = described_class.new(semantic_version, env)

expect(wezterm.link?).to eq(true)
end

it "supports links on the 20180218 version" do
env = env_with_name.merge({"TERM_PROGRAM_VERSION" => "20180218-123-abc"})
wezterm = described_class.new(semantic_version, env)

expect(wezterm.link?).to eq(true)
end

it "doesn't support links below the 20180218 version" do
env = env_with_name.merge({"TERM_PROGRAM_VERSION" => "20180217-123-abc"})
wezterm = described_class.new(semantic_version, env)

expect(wezterm.link?).to eq(false)
end

it "doesn't support links without a version" do
env = env_with_name.merge({"TERM_PROGRAM_VERSION" => nil})
wezterm = described_class.new(semantic_version, env)

expect(wezterm.link?).to eq(false)
end

it "doesn't support links without the term program version env variable" do
wezterm = described_class.new(semantic_version, env_with_name)

expect(wezterm.link?).to eq(false)
end
end

0 comments on commit b9ae09c

Please sign in to comment.