Module: Omnizip::Cli::Shared

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

Overview

Shared helpers used by the Thor command groups defined in lib/omnizip/cli.rb (ProfileCommands, ArchiveCommands, Cli).

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



20
21
22
23
24
25
26
27
28
# File 'lib/omnizip/cli/shared.rb', line 20

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



11
12
13
14
# File 'lib/omnizip/cli/shared.rb', line 11

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