Class: CommandTree::TextMenu

Inherits:
Object
  • Object
show all
Defined in:
lib/command_tree/text_menu.rb

Overview

responsible for showing the menu in terminal

Instance Method Summary collapse

Constructor Details

#initialize(item_width) ⇒ TextMenu

Returns a new instance of TextMenu.



7
8
9
10
# File 'lib/command_tree/text_menu.rb', line 7

def initialize(item_width)
  @item_width = item_width
  @items = []
end

Instance Method Details

#add(item) ⇒ Object



26
27
28
# File 'lib/command_tree/text_menu.rb', line 26

def add(item)
  items << item
end

#renderObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/command_tree/text_menu.rb', line 12

def render
  _, screen_width = IO.console.winsize
  items_per_row = screen_width / item_width

  names = items.dup.map! { |item| item_name(item) }
  descs = items.dup.map! { |item| item_desc(item) }

  until names.empty?
    puts names.shift(items_per_row).join
    row_descs = descs.shift(items_per_row).join
    puts row_descs unless row_descs.strip.empty?
  end
end