Class: Ferrum::Log::Default

Inherits:
Object
  • Object
show all
Includes:
Paint
Defined in:
lib/ferrum/log/default.rb,
sig/ferrum/log/default.rbs

Overview

Formats CDP messages as Ferrum always has: the direction, the elapsed time and the message as JSON. On a terminal the arrow is colored by kind, the time dimmed and the method bold.

Constant Summary

Constants included from Paint

Paint::COLORS

Instance Method Summary collapse

Methods included from Paint

#color?

Constructor Details

#initialize(color: false) ⇒ Default

Returns a new instance of Default.

Parameters:

  • color: (Boolean) (defaults to: false)


15
16
17
# File 'lib/ferrum/log/default.rb', line 15

def initialize(color: false)
  @color = color
end

Instance Method Details

#received(message, raw, elapsed, _command = nil) ⇒ String

Parameters:

  • message (Hash{String => Object}, nil)
  • raw (String)
  • elapsed (Float)
  • _command ([String, ::Float], nil) (defaults to: nil)

Returns:

  • (String)


35
36
37
38
39
40
41
42
43
# File 'lib/ferrum/log/default.rb', line 35

def received(message, raw, elapsed, _command = nil)
  arrow = if message.nil? || !message.key?("id") then :magenta
          elsif message.key?("error") then :red
          else :green
          end
  body = message ? payload(message, message["method"]) : raw

  "    #{paint('◀', arrow)} #{paint(elapsed, :dim)} #{body}\n"
end

#sent(message, elapsed) ⇒ String

Parameters:

  • message (Hash{Symbol => Object})
  • elapsed (Float)

Returns:

  • (String)


24
25
26
# File 'lib/ferrum/log/default.rb', line 24

def sent(message, elapsed)
  "\n\n#{paint('▶', :cyan)} #{paint(elapsed, :dim)} #{payload(message, message[:method])}"
end