Module: Yuslow::StdoutPrinter

Extended by:
StdoutPrinter
Included in:
StdoutPrinter
Defined in:
lib/yuslow/stdout_printer.rb

Instance Method Summary collapse

Instance Method Details

#colorize(text, color) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yuslow/stdout_printer.rb', line 43

def colorize(text, color)
  code =
    case color
    when :black    then '30'
    when :red      then '31'
    when :green    then '32'
    when :yellow   then '33'
    when :white    then '1;37'
    when :bg_black then '40'
    when :bg_red   then '41'
    when :bg_green then '42'
    else
      0
    end

  "\e[#{code}m#{text}\e[0m"
end

#execute(operations, colorize = true) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/yuslow/stdout_printer.rb', line 5

def execute(operations, colorize = true)
  output = []

  operations.each_with_index.map do |operation, index|
    output << "Thread[#{index + 1}]:"
    output << stringify_operation(operation, 1, colorize)
    output << nil
  end

  print output.join "\n"
end

#stringify_operation(operation, indent, colorize) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yuslow/stdout_printer.rb', line 17

def stringify_operation(operation, indent, colorize)
  output = []
  indentation = '  ' * indent

  if operation.elapsed
    identifier = operation.identifier
    identifier = colorize(identifier, :green) if colorize

    elapsed = operation.elapsed
    elapsed = colorize(elapsed, :white) if colorize

    output << "#{indentation}#{identifier} elapsed #{elapsed} ms"
  else
    text = "#{operation.identifier} did not finish"
    text = colorize(text, :yellow) if colorize

    output << "#{indentation}#{text}"
  end

  operation.children.each do |child_operation|
    output << stringify_operation(child_operation, indent + 1, colorize)
  end

  output.join "\n"
end