Module: RosettAi::Formatting
- Included in:
- Thor::Tasks::Backup, Thor::Tasks::BuildEngine, Thor::Tasks::BuildPackage
- Defined in:
- lib/rosett_ai/formatting.rb
Overview
Human-readable formatting for byte sizes and durations. Include in any class that needs to display file sizes or elapsed times.
Constant Summary collapse
- SIZE_UNITS =
['B', 'KB', 'MB', 'GB'].freeze
Instance Method Summary collapse
-
#format_duration(seconds) ⇒ Object
Formats a duration in seconds as a human-readable string.
-
#format_size(bytes) ⇒ Object
Formats a byte count as a human-readable string (e.g. "2.0 KB").
Instance Method Details
#format_duration(seconds) ⇒ Object
Formats a duration in seconds as a human-readable string. Returns "Xs" for < 60s, "Xm Ys" for >= 60s.
25 26 27 28 29 30 31 |
# File 'lib/rosett_ai/formatting.rb', line 25 def format_duration(seconds) if seconds >= 60 format('%<min>dm %<sec>.0fs', min: (seconds / 60).to_i, sec: seconds % 60) else format('%<sec>.1fs', sec: seconds) end end |
#format_size(bytes) ⇒ Object
Formats a byte count as a human-readable string (e.g. "2.0 KB").
13 14 15 16 17 18 19 20 21 |
# File 'lib/rosett_ai/formatting.rb', line 13 def format_size(bytes) unit_index = 0 size = bytes.to_f while size >= 1024 && unit_index < SIZE_UNITS.length - 1 size /= 1024 unit_index += 1 end format('%<size>.1f %<unit>s', size: size, unit: SIZE_UNITS[unit_index]) end |