Class: MakeMenu::BadgeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/make_menu/badge_set.rb

Overview

A set of BADGES to display above the menu

Instance Method Summary collapse

Constructor Details

#initializeBadgeSet

Returns a new instance of BadgeSet.



6
7
8
# File 'lib/make_menu/badge_set.rb', line 6

def initialize
  @badges = []
end

Instance Method Details

#add(label = '', &block) ⇒ Object

Add a new badge to the set

Parameters:

  • label (String) (defaults to: '')

    Optional label to print to the left of the badge value

  • block (Proc)

    Block to run each time the menu is re-drawn



13
14
15
16
17
18
# File 'lib/make_menu/badge_set.rb', line 13

def add(label = '', &block)
  @badges << {
    label: label,
    handler: block
  }
end

#displayObject

Print badges in a wrapping horizontal row



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/make_menu/badge_set.rb', line 21

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