Module: Color

Defined in:
lib/fresco/runtime/runtime.rb

Overview

ANSI colorization for log lines. Honors NO_COLOR (no-color.org) — any non-empty value disables. We don’t auto-detect a TTY: log output usually goes to a terminal, journalctl, or a TTY-aware aggregator, all of which render ANSI fine. Pipe to a file? ‘NO_COLOR=1 ./bin/server`.

Spinel-shape constraints worth flagging up front, since they shape the awkward bits below:

- `wrap` does stepwise concat (vs a single `+` chain) so each
  intermediate is narrowly typed String, matching the
  build_headers / url_encode pattern in this file.
- The disabled branch goes through the same `out = ""; out += s`
  concat instead of `return s` so wrap's return type doesn't fork
  into String|RbVal — Spinel would widen the function (and every
  caller's parameter) to RbVal, then the enabled branch's
  `out += s` would fail to compile against an RbVal `s`.
- For the same reason, `color_method` / `color_status` *always*
  route through a Color helper, including their fallback path —
  a bare `return m` would be String while the other returns are
  RbVal-shaped, and the unification widens `m` itself, which then
  propagates back into Color.red(m) → wrap → broken codegen.

Class Method Summary collapse

Class Method Details

.blue(s = "") ⇒ Object



1532
# File 'lib/fresco/runtime/runtime.rb', line 1532

def self.blue(s = "");    wrap("34", s); end

.bold(s = "") ⇒ Object



1528
# File 'lib/fresco/runtime/runtime.rb', line 1528

def self.bold(s = "");    wrap("1",  s); end

.cyan(s = "") ⇒ Object



1534
# File 'lib/fresco/runtime/runtime.rb', line 1534

def self.cyan(s = "");    wrap("36", s); end

.dim(s = "") ⇒ Object



1527
# File 'lib/fresco/runtime/runtime.rb', line 1527

def self.dim(s = "");     wrap("2",  s); end

.enabled?Boolean

Returns:

  • (Boolean)


1509
1510
1511
# File 'lib/fresco/runtime/runtime.rb', line 1509

def self.enabled?
  ENV.fetch("NO_COLOR", "") == ""
end

.green(s = "") ⇒ Object



1530
# File 'lib/fresco/runtime/runtime.rb', line 1530

def self.green(s = "");   wrap("32", s); end

.magenta(s = "") ⇒ Object



1533
# File 'lib/fresco/runtime/runtime.rb', line 1533

def self.magenta(s = ""); wrap("35", s); end

.plain(s = "") ⇒ Object

No-op coloring: emits the input untouched but routes through ‘wrap` so callers that need a uniform return type (color_method, color_status) can use it as a fallback without forking the return into String|RbVal — see the note on Color above.



1540
# File 'lib/fresco/runtime/runtime.rb', line 1540

def self.plain(s = "");   wrap("",   s); end

.red(s = "") ⇒ Object



1529
# File 'lib/fresco/runtime/runtime.rb', line 1529

def self.red(s = "");     wrap("31", s); end

.wrap(code = "", s = "") ⇒ Object



1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
# File 'lib/fresco/runtime/runtime.rb', line 1513

def self.wrap(code = "", s = "")
  if enabled?
    out  = "\e["
    out += code
    out += "m"
    out += s
    out += "\e[0m"
    return out
  end
  out = ""
  out += s
  out
end

.yellow(s = "") ⇒ Object



1531
# File 'lib/fresco/runtime/runtime.rb', line 1531

def self.yellow(s = "");  wrap("33", s); end