Module: Term::ANSIColor::Hyperlink

Included in:
Term::ANSIColor
Defined in:
lib/term/ansicolor/hyperlink.rb

Instance Method Summary collapse

Instance Method Details



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/term/ansicolor/hyperlink.rb', line 6

def hyperlink(link, string = nil, id: nil, as_link: false)
  block_given? && string != nil && !respond_to?(:to_str) and
    raise ArgumentError,
    "Require either the string argument or a block argument"
  if link.nil?
    link = ''
  end
  if as_link && !link.empty?
    string ||= link
  end
  result = ''
  if Term::ANSIColor.coloring?
    result = "\e]8;#{"id=#{id}" unless id.nil?};".dup << link.to_str << "\e\\"
  end
  if block_given?
    result << yield.to_s
  elsif string.respond_to?(:to_str)
    result << string.to_str
  elsif respond_to?(:to_str)
    result << to_str
  else
    return result # only switch on
  end
  result << "\e]8;;\e\\" if Term::ANSIColor.coloring?
  result
end