Class: MakeMenu::Menu

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

Overview

This class builds and displays a number-selection menu from a Makefile then prompts for a number and executes the target.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(makefile) ⇒ Menu



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/make_menu/menu.rb', line 14

def initialize(makefile)
  @makefile = makefile

  @groups = []
  @items = []

  @options = {
    group_title_color: :underline,
    clear_screen: true,
    pause_on_success: false
  }
  @highlights = {}

  @header = nil
  @field_set = nil
  @badge_set = nil
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



32
33
34
# File 'lib/make_menu/menu.rb', line 32

def groups
  @groups
end

#highlightsObject (readonly)

Returns the value of attribute highlights.



32
33
34
# File 'lib/make_menu/menu.rb', line 32

def highlights
  @highlights
end

#itemsObject (readonly)

Returns the value of attribute items.



32
33
34
# File 'lib/make_menu/menu.rb', line 32

def items
  @items
end

#makefileObject (readonly)

Returns the value of attribute makefile.



32
33
34
# File 'lib/make_menu/menu.rb', line 32

def makefile
  @makefile
end

#optionsObject (readonly)

Returns the value of attribute options.



32
33
34
# File 'lib/make_menu/menu.rb', line 32

def options
  @options
end

Instance Method Details

#add_badge(label = '', on: ' ON '.green_bg.bold, off: ' OFF '.red_bg.dark, &block) ⇒ Object



108
109
110
# File 'lib/make_menu/menu.rb', line 108

def add_badge(label = '', on: ' ON '.green_bg.bold, off: ' OFF '.red_bg.dark, &block)
  badges.add label, on: on, off: off, &block
end

#add_field(field, &block) ⇒ Object



96
97
98
# File 'lib/make_menu/menu.rb', line 96

def add_field(field, &block)
  fields.add field, &block
end

#add_group(group) ⇒ Object



130
131
132
133
# File 'lib/make_menu/menu.rb', line 130

def add_group(group)
  groups << group
  group
end

#add_item(item) ⇒ Object



135
136
137
138
# File 'lib/make_menu/menu.rb', line 135

def add_item(item)
  items << item
  item
end

#badgesObject



112
113
114
# File 'lib/make_menu/menu.rb', line 112

def badges
  @badge_set ||= BadgeSet.new
end

#clear_screen?Boolean



144
145
146
# File 'lib/make_menu/menu.rb', line 144

def clear_screen?
  options[:clear_screen]
end

#colorize(text) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/make_menu/menu.rb', line 152

def colorize(text)
  highlights.each do |word, color|
    case color
    when Array
      color.each { |c| text.gsub!(word, word.send(c)) }
    else
      text.gsub!(word, word.send(color))
    end
  end
  text
end

#display_badgesObject



116
117
118
# File 'lib/make_menu/menu.rb', line 116

def display_badges
  @badge_set.display if @badge_set
end

#display_fieldsObject



104
105
106
# File 'lib/make_menu/menu.rb', line 104

def display_fields
  @field_set.display if @field_set
end

#display_headerObject



87
88
89
90
91
92
93
94
# File 'lib/make_menu/menu.rb', line 87

def display_header
  if @header
    @header.call
  else
     = "  #{Dir.pwd.split('/').last.upcase}  ".invert.bold.to_s
    puts "\n#{logo.align(:center)}\n \n"
  end
end

#execute_option(selected) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/make_menu/menu.rb', line 66

def execute_option(selected)
  selected = selected.to_i

  items.each do |item|
    next unless item.option_number == selected

    system 'clear' if clear_screen?

    item.execute

    if pause_on_success?
      puts "\nPress ENTER to continue....".dark
      gets
    end
  end
end

#fieldsObject



100
101
102
# File 'lib/make_menu/menu.rb', line 100

def fields
  @field_set ||= FieldSet.new
end

#group_title_colorObject



140
141
142
# File 'lib/make_menu/menu.rb', line 140

def group_title_color
  options[:group_title_color]
end

#header(&block) ⇒ Object



83
84
85
# File 'lib/make_menu/menu.rb', line 83

def header(&block)
  @header = block
end

#pause_on_success?Boolean



148
149
150
# File 'lib/make_menu/menu.rb', line 148

def pause_on_success?
  options[:pause_on_success]
end

#run {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/make_menu/menu.rb', line 34

def run
  yield self if block_given?

  Builder.build(makefile, self)

  loop do
    system 'clear' if clear_screen?

    display_header
    display_badges
    display_fields

    puts colorize(MakeMenu::Text::Table.new(groups).to_s)
    puts
    puts 'Press ESC to quit'.align(:center).bold
    puts

    prompt = 'Select option: '.align(:center)

    begin
      execute_option(MakeMenu::Console::Prompter.prompt(prompt).strip)
      puts
    rescue MakeMenu::Console::Prompter::PressedEscape
      break
    end
  end

  puts

  system 'clear' if clear_screen?
end