Class: RubyMethodTracer::Formatters::BaseFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_method_tracer/formatters/base_formatter.rb

Overview

Base class for formatting trace output

Direct Known Subclasses

TreeFormatter

Instance Method Summary collapse

Instance Method Details

#colorize(text, color) ⇒ String

Apply color to text using ANSI escape codes

Parameters:

  • text (String)

    Text to colorize

  • color (Symbol)

    Color name

Returns:

  • (String)

    Colorized text



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_method_tracer/formatters/base_formatter.rb', line 26

def colorize(text, color)
  colors = {
    red: "31",
    green: "32",
    yellow: "33",
    blue: "34",
    magenta: "35",
    cyan: "36",
    white: "37",
    reset: "0"
  }
  "\e[#{colors[color]}m#{text}\e[#{colors[:reset]}m"
end

#format(_data) ⇒ Object

Abstract method to be implemented by subclasses

Parameters:

  • _data (Object)

    Data to format

Raises:

  • (NotImplementedError)

    Must be implemented by subclass



44
45
46
# File 'lib/ruby_method_tracer/formatters/base_formatter.rb', line 44

def format(_data)
  raise NotImplementedError, "#{self.class} must implement #format"
end

#format_time(seconds) ⇒ String

Format timing value into human-readable string

Parameters:

  • seconds (Float)

    Time in seconds

Returns:

  • (String)

    Formatted time string



11
12
13
14
15
16
17
18
19
# File 'lib/ruby_method_tracer/formatters/base_formatter.rb', line 11

def format_time(seconds)
  if seconds >= 1.0
    "#{seconds.round(3)}s"
  elsif seconds >= 0.001
    "#{(seconds * 1000).round(1)}ms"
  else
    "#{(seconds * 1_000_000).round(0)}µs"
  end
end