Module: Omnizip::CliOutputFormatter

Defined in:
lib/omnizip/cli/output_formatter.rb

Overview

Output formatting utilities for CLI commands.

Provides methods for formatting compression statistics, error messages, and other CLI output in a user-friendly manner.

Class Method Summary collapse

Class Method Details

.format_algorithms_table(algorithms) ⇒ String

Format algorithm information as a table.

Parameters:

Returns:

  • (String)

    Formatted table



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/omnizip/cli/output_formatter.rb', line 74

def format_algorithms_table(algorithms)
  return "No algorithms registered." if algorithms.empty?

  lines = []
  lines << "Available compression algorithms:"
  lines << ""

  max_name = algorithms.map { |a| a.name.to_s.length }.max
  max_desc = algorithms.map { |a| a.description.length }.max

  algorithms.each do |algo|
    name = algo.name.to_s.ljust(max_name)
    desc = algo.description.ljust(max_desc)
    version = "v#{algo.version}"
    lines << "  #{name} - #{desc} (#{version})"
  end

  lines.join("\n")
end

.format_compression_stats(input_size, output_size, elapsed_time) ⇒ String

Format compression statistics for display.

Parameters:

  • input_size (Integer)

    Original size in bytes

  • output_size (Integer)

    Compressed size in bytes

  • elapsed_time (Float)

    Time taken in seconds

Returns:

  • (String)

    Formatted statistics



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/omnizip/cli/output_formatter.rb', line 32

def format_compression_stats(input_size, output_size, elapsed_time)
  ratio = if input_size.positive?
            (output_size.to_f / input_size * 100).round(2)
          else
            0.0
          end

  [
    "Input size:  #{format_size(input_size)}",
    "Output size: #{format_size(output_size)}",
    "Ratio:       #{ratio}%",
    "Time:        #{elapsed_time.round(3)}s",
  ].join("\n")
end

.format_error(error) ⇒ String

Format error message for display.

Parameters:

  • error (Exception)

    The error object

Returns:

  • (String)

    Formatted error message



66
67
68
# File 'lib/omnizip/cli/output_formatter.rb', line 66

def format_error(error)
  "Error: #{error.message}"
end

.format_size(bytes) ⇒ String

Format file size in human-readable format.

Parameters:

  • bytes (Integer)

    Size in bytes

Returns:

  • (String)

    Formatted size



51
52
53
54
55
56
57
58
59
60
# File 'lib/omnizip/cli/output_formatter.rb', line 51

def format_size(bytes)
  units = %w[B KB MB GB TB]
  return "0 B" if bytes.zero?

  exp = (Math.log(bytes) / Math.log(1024)).floor
  exp = [exp, units.length - 1].min

  size = (bytes.to_f / (1024**exp)).round(2)
  "#{size} #{units[exp]}"
end

.verbose_puts(message, verbose) ⇒ void

This method returns an undefined value.

Print verbose message if verbose mode is enabled.

Parameters:

  • message (String)

    The message to print

  • verbose (Boolean)

    Whether verbose mode is enabled



99
100
101
# File 'lib/omnizip/cli/output_formatter.rb', line 99

def verbose_puts(message, verbose)
  puts message if verbose
end