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.

Defined Under Namespace

Modules: HelpDispatch

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



62
63
64
65
66
67
68
69
70
# File 'lib/omnizip/cli.rb', line 62

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



53
54
55
56
# File 'lib/omnizip/cli.rb', line 53

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