Module: TTY::Link

Defined in:
lib/tty/link.rb,
lib/tty/link/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

ESC =
"\u001B["
OSC =
"\u001B]"
BEL =
"\u0007"
SEP =
";"
ITERM =
/iTerm(\s*\d+){0,1}.app/x.freeze
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

Render terminal link

Parameters:

  • name (String)
  • url (String)

Returns:

  • (String)


65
66
67
68
69
70
71
# File 'lib/tty/link.rb', line 65

def link_to(name, url)
  if support_link?
    [ OSC, "8", SEP, SEP, url, BEL, name, OSC, "8", SEP, SEP, BEL ].join("")
  else
    "#{name} -> #{url}"
  end
end

.parse_version(version) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse version number

Parameters:

  • version (String)


21
22
23
24
25
26
27
28
# File 'lib/tty/link.rb', line 21

def parse_version(version)
  if (matches = version.match(/^(\d{1,2})(\d{2})$/))
    major, minor, patch = 0, matches[1].to_i, matches[2].to_i
  else
    major, minor, patch = version.split(".").map(&:to_i)
  end
  { major: major, minor: minor, patch: patch }
end

.support_link?(output: $stdout) ⇒ Boolean

Check if link is supported

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tty/link.rb', line 36

def support_link?(output: $stdout)
  return false unless output.tty?

  if ENV["TERM_PROGRAM"] =~ ITERM
    version = parse_version(ENV["TERM_PROGRAM_VERSION"])

    return version[:major] > 3 || version[:major] == 3 && version[:minor] > 0
  end

  # uses VTE terminal
  if ENV["VTE_VERSION"]
    version = parse_version(ENV["VTE_VERSION"])

    return version[:major] > 0 || version[:minor] > 50 ||
      version[:minor] == 50 && version[:patch] > 0
  end

  return false
end