Class: TTY::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/link.rb,
lib/tty/link/errors.rb,
lib/tty/link/version.rb,
lib/tty/link/ansi_link.rb,
lib/tty/link/terminals.rb,
lib/tty/link/plain_link.rb,
lib/tty/link/terminals/wt.rb,
lib/tty/link/terminals/rio.rb,
lib/tty/link/terminals/vte.rb,
lib/tty/link/terminals/foot.rb,
lib/tty/link/terminals/hyper.rb,
lib/tty/link/terminals/iterm.rb,
lib/tty/link/terminals/kitty.rb,
lib/tty/link/terminals/tabby.rb,
lib/tty/link/semantic_version.rb,
lib/tty/link/terminals/mintty.rb,
lib/tty/link/terminals/vscode.rb,
lib/tty/link/terminals/contour.rb,
lib/tty/link/terminals/domterm.rb,
lib/tty/link/terminals/konsole.rb,
lib/tty/link/terminals/wezterm.rb,
lib/tty/link/terminals/abstract.rb,
lib/tty/link/terminals/jediterm.rb,
lib/tty/link/hyperlink_parameter.rb,
lib/tty/link/terminals/alacritty.rb,
lib/tty/link/terminals/terminology.rb

Overview

Responsible for detecting and generating terminal hyperlinks

Defined Under Namespace

Modules: Terminals Classes: ANSILink, AbstractMethodError, HyperlinkParameter, PlainLink, SemanticVersion, ValueError

Constant Summary collapse

Error =

Raised to signal an error condition

Class.new(StandardError)
VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV, hyperlink: :auto, output: $stdout, plain: DEFAULT_TEMPLATE) ⇒ Link

Create a TTY::Link instance

Examples:

link = TTY::Link.new
link = TTY::Link.new(env: {"VTE_VERSION" => "7603"})
link = TTY::Link.new(hyperlink: :always)
link = TTY::Link.new(output: $stderr)
link = TTY::Link.new(plain: ":name (:url)")

Parameters:

  • env (ENV, Hash{String => String}) (defaults to: ENV)

    the environment variables

  • hyperlink (String, Symbol) (defaults to: :auto)

    the hyperlink detection out of always, auto or never

  • output (IO) (defaults to: $stdout)

    the output stream, defaults to $stdout

  • plain (String) (defaults to: DEFAULT_TEMPLATE)

    the plain URL template



141
142
143
144
145
146
147
# File 'lib/tty/link.rb', line 141

def initialize(env: ENV, hyperlink: :auto, output: $stdout,
               plain: DEFAULT_TEMPLATE)
  @env = env
  @hyperlink_parameter = hyperlink_parameter(hyperlink_env || hyperlink)
  @output = output
  @plain = plain
end

Class Method Details

.link?(env: ENV, output: $stdout) ⇒ Boolean

Detect terminal hyperlink support

Examples:

TTY::Link.link?
# => true
TTY::Link.link?(env: {"VTE_VERSION" => "7603"})
# => true
TTY::Link.link?(output: $stderr)
# => false

Parameters:

  • env (ENV, Hash{String => String}) (defaults to: ENV)

    the environment variables

  • output (IO) (defaults to: $stdout)

    the output stream, defaults to $stdout

Returns:

  • (Boolean)

See Also:



110
111
112
# File 'lib/tty/link.rb', line 110

def self.link?(env: ENV, output: $stdout)
  new(env: env, output: output).link?
end

Generate terminal hyperlink

Examples:

TTY::Link.link_to("TTY Toolkit", "https://ttytoolkit.org")
TTY::Link.link_to("https://ttytoolkit.org")
TTY::Link.link_to("TTY Toolkit", "https://ttytoolkit.org",
                  attrs: {id: "tty-toolkit"})
TTY::Link.link_to("TTY Toolkit", "https://ttytoolkit.org",
                  env: {"VTE_VERSION" => "7603"})
TTY::Link.link_to("TTY Toolkit", "https://ttytoolkit.org",
                  hyperlink: :always)
TTY::Link.link_to("TTY Toolkit", "https://ttytoolkit.org",
                  output: $stderr)
TTY::Link.link_to("TTY Toolkit", "https://ttytoolkit.org",
                  plain: ":name (:url)")

Parameters:

  • name (String)

    the name for the URL

  • url (String, nil) (defaults to: nil)

    the URL target

  • attrs (Hash{Symbol => String}) (defaults to: {})

    the URL attributes

  • env (ENV, Hash{String => String}) (defaults to: ENV)

    the environment variables

  • hyperlink (String, Symbol) (defaults to: :auto)

    the hyperlink detection out of always, auto or never

  • output (IO) (defaults to: $stdout)

    the output stream, defaults to $stdout

  • plain (String) (defaults to: DEFAULT_TEMPLATE)

    the plain URL template

Returns:

  • (String)

See Also:



80
81
82
83
84
# File 'lib/tty/link.rb', line 80

def self.link_to(name, url = nil, attrs: {}, env: ENV, hyperlink: :auto,
                 output: $stdout, plain: DEFAULT_TEMPLATE)
  new(env: env, hyperlink: hyperlink, output: output, plain: plain)
    .link_to(name, url, attrs: attrs)
end

Instance Method Details

#link?Boolean

Detect terminal hyperlink support

Examples:

link.link?
# => true

Returns:

  • (Boolean)


194
195
196
197
198
# File 'lib/tty/link.rb', line 194

def link?
  return false unless tty?

  terminals.any?(&:link?)
end

Generate terminal hyperlink

Examples:

link.link_to("TTY Toolkit", "https://ttytoolkit.org")
link.link_to("https://ttytoolkit.org")
link.link_to("TTY Toolkit", "https://ttytoolkit.org",
             attrs: {id: "tty-toolkit"})
link.link_to("https://ttytoolkit.org",
             attrs: {id: "tty-toolkit", title: "TTY Toolkit"})

Parameters:

  • name (String)

    the name for the URL

  • url (String, nil) (defaults to: nil)

    the URL target

  • attrs (Hash{Symbol => String}) (defaults to: {})

    the URL attributes

Returns:

  • (String)


175
176
177
178
179
180
181
182
183
# File 'lib/tty/link.rb', line 175

def link_to(name, url = nil, attrs: {})
  url ||= name

  if ansi_link?
    ansi_link(name, url, attrs).to_s
  else
    plain_link(name, url).to_s
  end
end