Class: Reportinator
Overview
Pretifies reports
Class Method Summary collapse
-
.generate_duration_from_interval(start_time_s:, end_time_s:, precision: 2, abbreviate: false) ⇒ Object
Generate human readable string of days, hours, minutes, seconds (and milliseconds) from a start count of seconds and end count of seconds.
-
.generate_duration_string(seconds_s, precision: 2, abbreviate: false) ⇒ Object
Generate human readable string of days, hours, minutes, seconds (and milliseconds) from a floating-point count of seconds.
Instance Method Summary collapse
-
#generate_banner(message, width = nil) ⇒ Object
Generates a banner for a message based on the length of the message or a given width.
- #generate_config_walk(keys, depth = 0) ⇒ Object
- #generate_duration_from_interval(start_time_s:, end_time_s:, precision: 2, abbreviate: false) ⇒ Object
- #generate_duration_string(seconds_s, precision: 2, abbreviate: false) ⇒ Object
- #generate_heading(message) ⇒ Object
- #generate_module_progress(module_name:, filename:, operation:) ⇒ Object
- #generate_progress(message) ⇒ Object
Class Method Details
.generate_duration_from_interval(start_time_s:, end_time_s:, precision: 2, abbreviate: false) ⇒ Object
Generate human readable string of days, hours, minutes, seconds (and milliseconds) from a start count of seconds and end count of seconds.
77 78 79 80 |
# File 'lib/ceedling/reportinator.rb', line 77 def self.generate_duration_from_interval(start_time_s:, end_time_s:, precision: 2, abbreviate: false) return '' if start_time_s.nil? || end_time_s.nil? Reportinator.generate_duration_string( end_time_s - start_time_s, precision: precision, abbreviate: abbreviate ) end |
.generate_duration_string(seconds_s, precision: 2, abbreviate: false) ⇒ Object
Generate human readable string of days, hours, minutes, seconds (and
milliseconds) from a floating-point count of seconds. The optional
precision keyword controls decimal places on the sub-minute seconds
field (default: 2). When abbreviate is true, "hour", "minute", and
"second" are shortened to "hr", "min", and "sec" (pluralized as normal).
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 67 68 69 70 71 72 73 |
# File 'lib/ceedling/reportinator.rb', line 19 def self.generate_duration_string(seconds_s, precision: 2, abbreviate: false) # Calculate duration as integer milliseconds duration_ms = (seconds_s * 1000).to_i # Collect human readable time string tidbits duration = [] # Singular/plural unit label, with optional abbreviation for hr/min/sec unit = ->(count, full, abbrev = nil) { word = (abbreviate && abbrev) ? abbrev : full "#{count} #{word}#{'s' if count > 1}" } # Singular / plural whole days if duration_ms >= DurationCounts::DAY_MS days = duration_ms / DurationCounts::DAY_MS duration << unit.call(days, 'day') 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 << unit.call(hours, 'hour', 'hr') 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 << unit.call(minutes, 'minute', 'min') 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 to requested precision) if duration_ms >= DurationCounts::SECOND_MS seconds = (duration_ms.to_f / 1000.0).round(precision) duration << "#{seconds} #{abbreviate ? 'secs' : 'seconds'}" # End duration string duration_ms = 0 end # Singular / plural whole milliseconds (only if original duration less than 1 second) if duration_ms > 0 duration << unit.call(duration_ms, 'millisecond') 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.("Hello world!") => "------------\nHello world!\n------------\n"
rp.("Hello world!", 3) => "---\nHello world!\n---\n"
106 107 108 109 110 111 112 |
# File 'lib/ceedling/reportinator.rb', line 106 def (, width=nil) # --------- # <Message> # --------- dash_count = ((width.nil?) ? Unicode::DisplayWidth.of( .strip ) : width) return "#{'-' * dash_count}\n#{}\n#{'-' * dash_count}\n" end |
#generate_config_walk(keys, depth = 0) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/ceedling/reportinator.rb', line 136 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_from_interval(start_time_s:, end_time_s:, precision: 2, abbreviate: false) ⇒ Object
86 87 88 |
# File 'lib/ceedling/reportinator.rb', line 86 def generate_duration_from_interval(start_time_s:, end_time_s:, precision: 2, abbreviate: false) return Reportinator.generate_duration_from_interval( start_time_s: start_time_s, end_time_s: end_time_s, precision: precision, abbreviate: abbreviate ) end |
#generate_duration_string(seconds_s, precision: 2, abbreviate: false) ⇒ Object
82 83 84 |
# File 'lib/ceedling/reportinator.rb', line 82 def generate_duration_string(seconds_s, precision: 2, abbreviate: false) return Reportinator.generate_duration_string( seconds_s, precision: precision, abbreviate: abbreviate ) end |
#generate_heading(message) ⇒ Object
114 115 116 117 118 |
# File 'lib/ceedling/reportinator.rb', line 114 def generate_heading() # <Message> # --------- return "\n#{}\n#{'-' * Unicode::DisplayWidth.of( .strip )}" end |
#generate_module_progress(module_name:, filename:, operation:) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ceedling/reportinator.rb', line 125 def generate_module_progress(module_name:, filename:, operation:) # <Operation [module_name::]filename>..." # Sanitze -- ensure it's a string and strip any filename extension _module_name = module_name.to_s().ext('') # If filename is the module name, don't add the module label label = (File.basename(filename).ext('') == _module_name) ? '' : "#{_module_name}::" return generate_progress("#{operation} #{label}#{filename}") end |
#generate_progress(message) ⇒ Object
120 121 122 123 |
# File 'lib/ceedling/reportinator.rb', line 120 def generate_progress() # <Message>... return "#{}..." end |