Module: Esse::Output

Included in:
CLI::EventListener, CLI::Index::BaseOperation
Defined in:
lib/esse/primitives/output.rb

Class Method Summary collapse

Class Method Details

.colorize(text, *attributes) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/esse/primitives/output.rb', line 20

def colorize(text, *attributes)
  if defined? Rainbow
    attributes.reduce(Rainbow(text)) { |p, a| p.public_send(a) }
  else
    text
  end
end

.formatted_runtime(number) ⇒ Object



12
13
14
# File 'lib/esse/primitives/output.rb', line 12

def formatted_runtime(number)
  colorize(sprintf('%.3f ms', number), :lightgray)
end


40
41
42
43
44
45
# File 'lib/esse/primitives/output.rb', line 40

def print_backtrace(error, limit: -1, **options)
  return unless error.respond_to?(:backtrace)
  return if error.backtrace.nil?

  error.backtrace[0..limit].each { |frame| print_error(frame, **options) }
end


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/esse/primitives/output.rb', line 28

def print_error(message_or_error, backtrace: false, **options)
  options[:level] ||= :error
  message = message_or_error.to_s

  print_message(message, output: :stderr, **options)

  if message_or_error.is_a?(Exception) && backtrace
    limit = backtrace.is_a?(Integer) ? backtrace : -1
    print_backtrace(message_or_error, limit: limit, level: options[:level])
  end
end


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/esse/primitives/output.rb', line 47

def print_message(message, level: :info, output: $stdout, newline: true, **fields)
  output = \
    case output
    when :stdout, 'stdout'
      $stdout
    when :stderr, 'stderr'
      $stderr
    when IO, StringIO
      output
    else
      raise ArgumentError, "Invalid output #{output.inspect}"
    end

  message = format(message, **fields)
  newline ? output.puts(message) : output.print(message)
end

.runtime_padding(number, extra = 2) ⇒ Object



16
17
18
# File 'lib/esse/primitives/output.rb', line 16

def runtime_padding(number, extra = 2)
  ' ' * (extra + sprintf('%.3f ms', number).size)
end