Module: Omnizip::Shared

Included in:
ArchiveCommands, Cli, ProfileCommands
Defined in:
lib/omnizip/cli.rb

Overview

Shared helpers used by the Thor command groups defined below (ProfileCommands, ArchiveCommands, Cli). Defined here because the command classes include it at class-body time; a separate file under Omnizip::Cli would re-trigger this file's autoload mid-load.

Instance Method Summary collapse

Instance Method Details

#format_bytes(bytes) ⇒ String

Format a byte count with a binary-unit suffix.

Parameters:

  • bytes (Integer)

    the byte count

Returns:

  • (String)

    human-readable size (e.g. "1.5 MB")



40
41
42
43
44
45
46
47
48
# File 'lib/omnizip/cli.rb', line 40

def format_bytes(bytes)
  return "0 B" if bytes.zero?

  units = %w[B KB MB GB TB]
  exp = (Math.log(bytes) / Math.log(1024)).to_i
  exp = [exp, units.size - 1].min

  "%.1f %s" % [bytes.to_f / (1024**exp), units[exp]]
end

#handle_error(error) ⇒ Object

Print a formatted error message and exit nonzero.

Parameters:

  • error (StandardError)

    the error to display



31
32
33
34
# File 'lib/omnizip/cli.rb', line 31

def handle_error(error)
  warn Omnizip::CliOutputFormatter.format_error(error)
  exit 1
end