Module: NNQ::CLI::Term

Defined in:
lib/nnq/cli/term.rb

Overview

Stateless terminal formatting and stderr writing helpers shared by every code path that emits verbose-driven log lines (event monitor callbacks in BaseRunner / PipeRunner, SocketSetup attach helpers, parallel/pipe Ractor workers).

Pure module functions: no state, no instance, safe to call from any thread or Ractor.

Class Method Summary collapse

Class Method Details

.format_attach(kind, url, timestamps) ⇒ String

Formats an “attached endpoint” log line (Bound to / Connecting to).

Parameters:

  • kind (:bind, :connect)
  • url (String)
  • timestamps (Symbol, nil)

Returns:

  • (String)


83
84
85
86
# File 'lib/nnq/cli/term.rb', line 83

def format_attach(kind, url, timestamps)
  verb = kind == :bind ? "Bound to" : "Connecting to"
  "#{log_prefix(timestamps)}nnq: #{verb} #{url}"
end

.format_event(event, timestamps) ⇒ String

Formats one NNQ::MonitorEvent into a single log line (no trailing newline).

Parameters:

  • event (NNQ::MonitorEvent)
  • timestamps (Symbol, nil)

Returns:

  • (String)


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/nnq/cli/term.rb', line 38

def format_event(event, timestamps)
  prefix = log_prefix(timestamps)
  case event.type
  when :message_sent
    "#{prefix}nnq: >> #{Formatter.preview(event.detail[:body])}"
  when :message_received
    "#{prefix}nnq: << #{Formatter.preview(event.detail[:body])}"
  else
    ep     = event.endpoint ? " #{event.endpoint}" : ""
    detail = format_event_detail(event.detail)
    "#{prefix}nnq: #{event.type}#{ep}#{detail}"
  end
end

.format_event_detail(detail) ⇒ String

Renders MonitorEvent#detail as a suffix for log lines. Rewrites plain peer-close exceptions (EOFError) to “closed by peer”.

Parameters:

  • detail (Hash, Object, nil)

Returns:

  • (String)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nnq/cli/term.rb', line 59

def format_event_detail(detail)
  return "" if detail.nil?
  return " #{detail}" unless detail.is_a?(Hash)

  error = detail[:error]
  reason = detail[:reason]

  case error
  when nil
    reason ? " (#{reason})" : ""
  when EOFError
    " (closed by peer)"
  else
    " (#{reason || error.message})"
  end
end

.log_prefix(timestamps) ⇒ String

Returns a stderr log line prefix with a UTC ISO8601 timestamp at the requested precision (:s/:ms/:us), or “” when nil.

Parameters:

  • timestamps (Symbol, nil)

    :s, :ms, :us, or nil (disabled)

Returns:

  • (String)


22
23
24
25
26
27
28
29
# File 'lib/nnq/cli/term.rb', line 22

def log_prefix(timestamps)
  case timestamps
  when nil then ""
  when :s  then "#{Time.now.utc.strftime("%FT%T")}Z "
  when :ms then "#{Time.now.utc.strftime("%FT%T.%3N")}Z "
  when :us then "#{Time.now.utc.strftime("%FT%T.%6N")}Z "
  end
end

.write_attach(kind, url, timestamps, io: $stderr) ⇒ void

This method returns an undefined value.

Writes one “Bound to / Connecting to” line to io (default $stderr).

Parameters:

  • kind (:bind, :connect)
  • url (String)
  • timestamps (Symbol, nil)
  • io (#write) (defaults to: $stderr)


108
109
110
# File 'lib/nnq/cli/term.rb', line 108

def write_attach(kind, url, timestamps, io: $stderr)
  io.write("#{format_attach(kind, url, timestamps)}\n")
end

.write_event(event, timestamps, io: $stderr) ⇒ void

This method returns an undefined value.

Writes one formatted event line to io (default $stderr).

Parameters:

  • event (NNQ::MonitorEvent)
  • timestamps (Symbol, nil)
  • io (#write) (defaults to: $stderr)

    writable sink, default $stderr



95
96
97
# File 'lib/nnq/cli/term.rb', line 95

def write_event(event, timestamps, io: $stderr)
  io.write("#{format_event(event, timestamps)}\n")
end