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 =

Returns Human-readable byte size unit labels.

Returns:

  • (Array)

    Human-readable byte size unit labels.

['B', 'KB', 'MB', 'GB'].freeze

Instance Method Summary collapse

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.



26
27
28
29
30
31
32
# File 'lib/rosett_ai/formatting.rb', line 26

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").



14
15
16
17
18
19
20
21
22
# File 'lib/rosett_ai/formatting.rb', line 14

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