Class: Audition::Report::Style

Inherits:
Object
  • Object
show all
Defined in:
lib/audition/report/style.rb

Overview

ANSI + OSC 8 styling with graceful degradation. Color and hyperlinks are decided once, at construction; pass color: false for pipes, NO_COLOR, or dumb terminals.

Constant Summary collapse

GLYPHS =
Ractor.make_shareable(
  {
    error: ["✖", "x"], warning: ["⚠", "!"],
    info: ["ℹ", "i"], pass: ["✔", "ok"],
    section: ["◆", "*"], fix: ["✎", "+"]
  }
)
PAINTS =
%i[red yellow green cyan magenta dim bold].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color:, hyperlinks:) ⇒ Style

Returns a new instance of Style.



28
29
30
31
32
# File 'lib/audition/report/style.rb', line 28

def initialize(color:, hyperlinks:)
  @pastel = Pastel.new(enabled: color)
  @color = color
  @hyperlinks = hyperlinks
end

Class Method Details

.detect(io: $stdout) ⇒ Object



22
23
24
25
26
# File 'lib/audition/report/style.rb', line 22

def self.detect(io: $stdout)
  on = io.respond_to?(:tty?) && io.tty? &&
    !ENV.key?("NO_COLOR") && ENV["TERM"] != "dumb"
  new(color: on, hyperlinks: on && TTY::Link.link?)
end

Instance Method Details

#color? ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/audition/report/style.rb', line 34

def color?
  @color
end

#glyph(kind) ⇒ Object



38
39
40
# File 'lib/audition/report/style.rb', line 38

def glyph(kind)
  GLYPHS.fetch(kind)[@color ? 0 : 1]
end

OSC 8 hyperlink wrapping "path:line" display text in a file:// URI; supporting terminals make it clickable. tty-link emits when it detects support; when hyperlinks are forced on despite no detection (tests, --force scenarios) fall back to the raw OSC 8 template, since tty-link's fallback is "text -> url" prose.



62
63
64
65
66
67
68
69
70
71
# File 'lib/audition/report/style.rb', line 62

def link(text, absolute_path)
  return text unless @hyperlinks

  uri = "file://#{absolute_path}"
  if TTY::Link.link?
    TTY::Link.link_to(text, uri)
  else
    "\e]8;;#{uri}\e\\#{text}\e]8;;\e\\"
  end
end

#severity_color(severity, text) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/audition/report/style.rb', line 48

def severity_color(severity, text)
  case severity
  when :error then red(text)
  when :warning then yellow(text)
  else cyan(text)
  end
end