class CommandTree::TextMenu

responsible for showing the menu in terminal

Attributes

item_width[R]
items[R]

Public Class Methods

new(item_width) click to toggle source
# File lib/command_tree/text_menu.rb, line 7
def initialize(item_width)
  @item_width = item_width
  @items = []
end

Public Instance Methods

add(item) click to toggle source
# File lib/command_tree/text_menu.rb, line 26
def add(item)
  items << item
end
render() click to toggle source
# 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

Private Instance Methods

item_desc(item) click to toggle source
# File lib/command_tree/text_menu.rb, line 47
def item_desc(item)
  desc = item.desc.to_s
  return desc.ljust(item_width) if desc.strip.empty?

  return (desc[0...item_width - 3] + '...').light_black if desc.length >= item_width
  return desc.ljust(item_width).light_black
end
item_name(item) click to toggle source
# File lib/command_tree/text_menu.rb, line 34
def  item_name(item)
  prefix = item.prefix
  arrow = ' → '

  name_width = item_width - prefix.length - arrow.length
  name =  (item.is_a?(Group) ? '+' : '') + item.name
  name = name.ljust(name_width)

  colored_name = item.is_a?(Group) ? name.light_magenta.bold : name.cyan

  (prefix.green + arrow.light_black + colored_name)
end