From 918c41845797acaf5500df29da8b45e0490cdea7 Mon Sep 17 00:00:00 2001 From: Piotr Murach Date: Sun, 25 Aug 2024 13:11:51 +0200 Subject: [PATCH] Add TTY::Link::Terminals::Rio to detect hyperlinks support in Rio --- lib/tty/link/terminals/rio.rb | 54 +++++++++++++++++++++++++ spec/unit/link_spec.rb | 12 ++++++ spec/unit/terminals/rio_spec.rb | 70 +++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 lib/tty/link/terminals/rio.rb create mode 100644 spec/unit/terminals/rio_spec.rb diff --git a/lib/tty/link/terminals/rio.rb b/lib/tty/link/terminals/rio.rb new file mode 100644 index 0000000..6437781 --- /dev/null +++ b/lib/tty/link/terminals/rio.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require_relative "abstract" + +module TTY + class Link + module Terminals + # Responsible for detecting hyperlink support in the Rio terminal + # + # @api private + class Rio < Abstract + # The Rio terminal name pattern + # + # @return [Regexp] + # + # @api private + RIO = /rio/i.freeze + private_constant :RIO + + private + + # Detect Rio terminal + # + # @example + # rio.name? + # # => true + # + # @return [Boolean] + # + # @api private + def name? + !(term_program =~ RIO).nil? + end + + # Detect whether the Rio version supports terminal hyperlinks + # + # @example + # rio.version? + # # => true + # + # @return [Boolean] + # + # @api private + def version? + return false unless term_program_version + + current_semantic_version = semantic_version(term_program_version) + + current_semantic_version >= semantic_version(0, 0, 28) + end + end # Rio + end # Terminals + end # Link +end # TTY diff --git a/spec/unit/link_spec.rb b/spec/unit/link_spec.rb index 5e19acb..38d9bac 100644 --- a/spec/unit/link_spec.rb +++ b/spec/unit/link_spec.rb @@ -147,6 +147,18 @@ end end + context "when Rio" do + it "supports links above the 0.0.28 version" do + env = { + "TERM_PROGRAM" => "rio", + "TERM_PROGRAM_VERSION" => "0.0.28" + } + 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/rio_spec.rb b/spec/unit/terminals/rio_spec.rb new file mode 100644 index 0000000..5bc41c8 --- /dev/null +++ b/spec/unit/terminals/rio_spec.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +RSpec.describe TTY::Link::Terminals::Rio, "#link?" do + let(:env_with_name) { {"TERM_PROGRAM" => "rio"} } + let(:semantic_version) { TTY::Link::SemanticVersion } + + it "doesn't support links without the term program environment variable" do + rio = described_class.new(semantic_version, {}) + + expect(rio.link?).to eq(false) + end + + it "doesn't support links without a terminal program name" do + env = {"TERM_PROGRAM" => nil} + rio = described_class.new(semantic_version, env) + + expect(rio.link?).to eq(false) + end + + it "doesn't support links with a non-Rio program name" do + env = {"TERM_PROGRAM" => "other-terminal"} + rio = described_class.new(semantic_version, env) + + expect(rio.link?).to eq(false) + end + + it "supports links above the 0.1.8 version" do + env = { + "TERM_PROGRAM" => "Rio", + "TERM_PROGRAM_VERSION" => "0.2.3" + } + rio = described_class.new(semantic_version, env) + + expect(rio.link?).to eq(true) + end + + it "supports links above the 0.0.28 version" do + env = env_with_name.merge({"TERM_PROGRAM_VERSION" => "0.0.29"}) + rio = described_class.new(semantic_version, env) + + expect(rio.link?).to eq(true) + end + + it "supports links on the 0.0.28 version" do + env = env_with_name.merge({"TERM_PROGRAM_VERSION" => "0.0.28"}) + rio = described_class.new(semantic_version, env) + + expect(rio.link?).to eq(true) + end + + it "doesn't support links below the 0.0.28 version" do + env = env_with_name.merge({"TERM_PROGRAM_VERSION" => "0.0.27"}) + rio = described_class.new(semantic_version, env) + + expect(rio.link?).to eq(false) + end + + it "doesn't support links without a version" do + env = env_with_name.merge({"TERM_PROGRAM_VERSION" => nil}) + rio = described_class.new(semantic_version, env) + + expect(rio.link?).to eq(false) + end + + it "doesn't support links without the term program version env variable" do + rio = described_class.new(semantic_version, env_with_name) + + expect(rio.link?).to eq(false) + end +end