Class: Reportinator

Inherits:
Object show all
Defined in:
lib/ceedling/reportinator.rb

Overview

Pretifies reports

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_duration(start_time_s:, end_time_s:) ⇒ Object

Generate human readable string of days, hours, minutes, seconds (and milliseconds) from a start count of seconds and end count of seconds.



16
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ceedling/reportinator.rb', line 16

def self.generate_duration(start_time_s:, end_time_s:)
  return '' if start_time_s.nil? or end_time_s.nil?

  # Calculate duration as integer milliseconds
  duration_ms = ((end_time_s - start_time_s) * 1000).to_i

  # Collect human readable time string tidbits
  duration = []

  # Singular / plural whole days
  if duration_ms >= DurationCounts::DAY_MS
    days = duration_ms / DurationCounts::DAY_MS
    duration << "#{days} day#{'s' if days > 1}"
    duration_ms -= (days * DurationCounts::DAY_MS)
    # End duration string if remainder is less than 1 second (e.g. no 2 days 13 milliseconds)
    duration_ms = 0 if duration_ms < 1000
  end

  # Singular / plural whole hours
  if duration_ms >= DurationCounts::HOUR_MS
    hours = duration_ms / DurationCounts::HOUR_MS
    duration << "#{hours} hour#{'s' if hours > 1}"
    duration_ms -= (hours * DurationCounts::HOUR_MS)
    # End duration string if remainder is less than 1 second (e.g. no 2 days 13 milliseconds)
    duration_ms = 0 if duration_ms < 1000
  end

  # Singular / plural whole minutes
  if duration_ms >= DurationCounts::MINUTE_MS
    minutes = duration_ms / DurationCounts::MINUTE_MS
    duration << "#{minutes} minute#{'s' if minutes > 1}"
    duration_ms -= (minutes * DurationCounts::MINUTE_MS)
    # End duration string if remainder is less than 1 second (e.g. no 2 days 13 milliseconds)
    duration_ms = 0 if duration_ms < 1000
  end

  # Plural fractional seconds (rounded)
  if duration_ms >= DurationCounts::SECOND_MS
    seconds = (duration_ms.to_f() / 1000.0).round(2)
    duration << "#{seconds} seconds"
    # End duration string
    duration_ms = 0
  end

  # Singular / plural whole milliseconds (only if orginal duration less than 1 second)
  if duration_ms > 0
    duration << "#{duration_ms} millisecond#{'s' if duration_ms > 1}"
  end

  return duration.join(' ')
end

Instance Method Details

#generate_banner(message, width = nil) ⇒ Object

Generates a banner for a message based on the length of the message or a given width.

Attributes

  • message: The message to put.

  • width: The width of the message. If nil the size of the banner is

determined by the length of the message.

Examples

rp = Reportinator.new
rp.generate_banner("Hello world!") => "------------\nHello world!\n------------\n" 
rp.generate_banner("Hello world!", 3) => "---\nHello world!\n---\n"


88
89
90
91
92
93
94
# File 'lib/ceedling/reportinator.rb', line 88

def generate_banner(message, width=nil)
  # ---------
  # <Message>
  # ---------
  dash_count = ((width.nil?) ? Unicode::DisplayWidth.of( message.strip ) : width)
  return "#{'-' * dash_count}\n#{message}\n#{'-' * dash_count}\n"
end

#generate_config_walk(keys, depth = 0) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/ceedling/reportinator.rb', line 115

def generate_config_walk(keys, depth=0)
  # :key ↳ :key ↳ :key

  _keys = keys.clone
  _keys = _keys.slice(0, depth) if depth > 0
  _keys.reject! { |key| key.nil? }
  return _keys.map{|key| ":#{key}"}.join('')
end

#generate_duration(start_time_s:, end_time_s:) ⇒ Object



68
69
70
# File 'lib/ceedling/reportinator.rb', line 68

def generate_duration(start_time_s:, end_time_s:)
  return Reportinator.generate_duration( start_time_s: start_time_s, end_time_s: end_time_s )
end

#generate_heading(message) ⇒ Object



96
97
98
99
100
# File 'lib/ceedling/reportinator.rb', line 96

def generate_heading(message)
  # <Message>
  # ---------
  return "\n#{message}\n#{'-' * Unicode::DisplayWidth.of( message.strip )}"
end

#generate_module_progress(module_name:, filename:, operation:) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/ceedling/reportinator.rb', line 107

def generate_module_progress(module_name:, filename:, operation:)
  # <Operation [module_name::]filename>..."

  # If filename is the module name, don't add the module label
  label = (File.basename(filename).ext('') == module_name.to_s) ? '' : "#{module_name}::"
  return generate_progress("#{operation} #{label}#{filename}")
end

#generate_progress(message) ⇒ Object



102
103
104
105
# File 'lib/ceedling/reportinator.rb', line 102

def generate_progress(message)
  # <Message>...
  return "#{message}..."
end