From fe9e18de12085bde49735a9cfc24c4e1ad92d19e Mon Sep 17 00:00:00 2001 From: Piotr Murach Date: Sun, 18 Aug 2024 19:06:29 +0200 Subject: [PATCH] Add TTY::Link::Terminals::Alacritty to detect hyperlinks support in Alacritty --- lib/tty/link/terminals/alacritty.rb | 71 +++++++++++++++++++++++++++ spec/unit/link_spec.rb | 9 ++++ spec/unit/terminals/alacritty_spec.rb | 33 +++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 lib/tty/link/terminals/alacritty.rb create mode 100644 spec/unit/terminals/alacritty_spec.rb diff --git a/lib/tty/link/terminals/alacritty.rb b/lib/tty/link/terminals/alacritty.rb new file mode 100644 index 0000000..3243d85 --- /dev/null +++ b/lib/tty/link/terminals/alacritty.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require_relative "abstract" + +module TTY + class Link + module Terminals + # Responsible for detecting hyperlink support in the Alacritty terminal + # + # @api private + class Alacritty < Abstract + # The Alacritty terminal name pattern + # + # @return [Regexp] + # + # @api private + ALACRITTY = /alacritty/x.freeze + private_constant :ALACRITTY + + # The term environment variable name + # + # @return [String] + # + # @api private + TERM = "TERM" + private_constant :TERM + + private + + # Detect Alacritty terminal + # + # @example + # alacritty.name? + # # => true + # + # @return [Boolean] + # + # @api private + def name? + !(term =~ ALACRITTY).nil? + end + + # Detect any Alacritty version to support terminal hyperlinks + # + # @example + # alacritty.version? + # # => true + # + # @return [Boolean] + # + # @api private + def version? + true + end + + # Read the term environment variable + # + # @example + # alacritty.term + # # => "alacritty" + # + # @return [String, nil] + # + # @api private + def term + env[TERM] + end + end # Alacritty + end # Terminals + end # Link +end # TTY diff --git a/spec/unit/link_spec.rb b/spec/unit/link_spec.rb index 0d5a436..04ad2aa 100644 --- a/spec/unit/link_spec.rb +++ b/spec/unit/link_spec.rb @@ -45,6 +45,15 @@ end end + context "when Alacritty" do + it "supports links on any version" do + env = {"TERM" => "alacritty"} + link = described_class.new(env: env, output: output) + + expect(link.link?).to eq(true) + end + end + context "when iTerm" do it "supports a terminal program name with a version number" do env = { diff --git a/spec/unit/terminals/alacritty_spec.rb b/spec/unit/terminals/alacritty_spec.rb new file mode 100644 index 0000000..466b4e8 --- /dev/null +++ b/spec/unit/terminals/alacritty_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +RSpec.describe TTY::Link::Terminals::Alacritty, "#link?" do + let(:semantic_version) { TTY::Link::SemanticVersion } + + it "supports links on any version" do + env = {"TERM" => "alacritty"} + alacritty = described_class.new(semantic_version, env) + + expect(alacritty.link?).to eq(true) + end + + it "supports a terminal name with a '-direct' suffix" do + env = {"TERM" => "alacritty-direct"} + alacritty = described_class.new(semantic_version, env) + + expect(alacritty.link?).to eq(true) + end + + it "doesn't support links without a terminal name" do + env = {"TERM" => nil} + alacritty = described_class.new(semantic_version, env) + + expect(alacritty.link?).to eq(false) + end + + it "doesn't support links with a non-Alacritty terminal name" do + env = {"TERM" => "non-Alacritty"} + iterm = described_class.new(semantic_version, env) + + expect(iterm.link?).to eq(false) + end +end