From 42de45f074704e378ededa7fd21e44ec6b3fde02 Mon Sep 17 00:00:00 2001 From: Piotr Murach Date: Sat, 24 Aug 2024 16:12:11 +0200 Subject: [PATCH] Add TTY::Link::Terminals::Kitty to detect hyperlinks support in kitty --- lib/tty/link/terminals/kitty.rb | 48 +++++++++++++++++++++++++++++++ spec/unit/link_spec.rb | 9 ++++++ spec/unit/terminals/kitty_spec.rb | 33 +++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 lib/tty/link/terminals/kitty.rb create mode 100644 spec/unit/terminals/kitty_spec.rb diff --git a/lib/tty/link/terminals/kitty.rb b/lib/tty/link/terminals/kitty.rb new file mode 100644 index 0000000..6e28137 --- /dev/null +++ b/lib/tty/link/terminals/kitty.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +module TTY + class Link + module Terminals + # Responsible for detecting hyperlink support in the kitty terminal + # + # @api private + class Kitty < Abstract + # The kitty terminal name pattern + # + # @return [Regexp] + # + # @api private + KITTY = /kitty/i.freeze + private_constant :KITTY + + private + + # Detect kitty terminal + # + # @example + # kitty.name? + # # => true + # + # @return [Boolean] + # + # @api private + def name? + !(term =~ KITTY).nil? + end + + # Detect any kitty version to support terminal hyperlinks + # + # @example + # kitty.version? + # # => true + # + # @return [Boolean] + # + # @api private + def version? + true + end + end # Kitty + end # Terminals + end # Link +end # TTY diff --git a/spec/unit/link_spec.rb b/spec/unit/link_spec.rb index 17da17c..abac0a2 100644 --- a/spec/unit/link_spec.rb +++ b/spec/unit/link_spec.rb @@ -117,6 +117,15 @@ end end + context "when kitty" do + it "supports links on any version" do + env = {"TERM" => "xterm-kitty"} + link = described_class.new(env: env, output: output) + + expect(link.link?).to eq(true) + end + end + context "when VTE" do it "supports links above the 0.50.1 version" do env = {"VTE_VERSION" => "5001"} diff --git a/spec/unit/terminals/kitty_spec.rb b/spec/unit/terminals/kitty_spec.rb new file mode 100644 index 0000000..f578146 --- /dev/null +++ b/spec/unit/terminals/kitty_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +RSpec.describe TTY::Link::Terminals::Kitty, "#link?" do + let(:semantic_version) { TTY::Link::SemanticVersion } + + it "supports links on any version" do + env = {"TERM" => "xterm-kitty"} + kitty = described_class.new(semantic_version, env) + + expect(kitty.link?).to eq(true) + end + + it "supports a terminal name without the 'xterm-' prefix" do + env = {"TERM" => "Kitty"} + kitty = described_class.new(semantic_version, env) + + expect(kitty.link?).to eq(true) + end + + it "doesn't support links without a terminal name" do + env = {"TERM" => nil} + kitty = described_class.new(semantic_version, env) + + expect(kitty.link?).to eq(false) + end + + it "doesn't support links with a non-kitty terminal name" do + env = {"TERM" => "other-terminal"} + kitty = described_class.new(semantic_version, env) + + expect(kitty.link?).to eq(false) + end +end