module Omnibus::Reports

Constants

PADDING

Public Instance Methods

column_width(items, column_name) click to toggle source

Determine how wide a column should be, taking into account both the column name as well as all data in that column. If no data will be stored in the column, the width is 0 (i.e., nothing should be printed, not even the column header)

# File lib/omnibus/reports.rb, line 27
def column_width(items, column_name)
  widest_item = items.max_by(&:size)
  if widest_item
    widest = (widest_item.size >= column_name.size) ? widest_item : column_name
    widest.size + PADDING
  else
    0
  end
end
non_nil_values(hashes, selector_key) click to toggle source
# File lib/omnibus/reports.rb, line 37
def non_nil_values(hashes, selector_key)
  hashes.map { |v| v[selector_key] }.compact
end
pretty_version_map(project) click to toggle source
# File lib/omnibus/reports.rb, line 41
def pretty_version_map(project)
  out = ""
  version_map = project.library.version_map

  # Pull out data to print out
  versions = non_nil_values(version_map.values, :version)
  guids = non_nil_values(version_map.values, :version_guid)

  # We only want the versions that have truly been overridden;
  # because we want to output a column only if something was
  # overridden, but nothing if no packages were changed
  overridden_versions = non_nil_values(version_map.values.select { |v| v[:overridden] }, :default_version)

  # Determine how wide the printed table columns need to be
  name_width = column_width(version_map.keys, "Component")
  version_width = column_width(versions, "Installed Version")
  guid_width = column_width(guids, "Version GUID")
  override_width = column_width(overridden_versions, "Overridden From")

  total_width = name_width + version_width + guid_width + override_width
  divider = "-" * total_width

  # Print out the column headers
  out << "Component".ljust(name_width)
  out << "Installed Version".ljust(version_width)
  out << "Version GUID".ljust(guid_width)
  # Only print out column if something was overridden
  out << "Overridden From".ljust(override_width) if override_width > 0
  out << "\n"
  out << divider << "\n"

  # Print out the table body
  version_map.keys.sort.each do |name|
    version = version_map[name][:version]
    version_guid = version_map[name][:version_guid]

    default_version = version_map[name][:default_version]
    overridden = version_map[name][:overridden]

    out << "#{name}".ljust(name_width)
    out << version.to_s.ljust(version_width)
    out << version_guid.to_s.ljust(guid_width) if version_guid
    # Only print out column if something was overridden
    out << default_version.ljust(override_width) if overridden
    out << "\n"
  end
  out
end