Sha256: af81df0dedc423c38688f7e6dc6d9a217d252f3975c06bef089b64e1541c1586

Contents?: true

Size: 945 Bytes

Versions: 3

Compression:

Stored size: 945 Bytes

Contents

# frozen_string_literal: true

module MakeMenu
  # A set of BADGES to display above the menu
  class BadgeSet
    def initialize
      @badges = []
    end

    # Add a new badge to the set
    # @param [String] label Optional label to print to the left of the badge value
    # @param [Proc] block Block to run each time the menu is re-drawn
    def add(label = '', &block)
      @badges << {
        label: label,
        handler: block
      }
    end

    # Print badges in a wrapping horizontal row
    def display
      rows = []
      row = ''
      @badges.each do |badge|
        label = badge[:label]
        value = badge[:handler].call
        if row.decolor.size + label.decolor.size + value.decolor.size >= (0.7 * ::TTY::Screen.cols)
          rows << row
          row = ''
        end
        row += " #{label}#{value} "
      end
      rows << row

      puts rows.join("\n\n").align_block(:center)
      puts
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
make_menu-2.1.2 lib/make_menu/badge_set.rb
make_menu-2.1.1 lib/make_menu/badge_set.rb
make_menu-2.1.0 lib/make_menu/badge_set.rb