Class: MakeMenu::Menu
- Inherits:
-
Object
- Object
- MakeMenu::Menu
- 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
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#highlights ⇒ Object
readonly
Returns the value of attribute highlights.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#makefile ⇒ Object
readonly
Returns the value of attribute makefile.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #add_badge(label = '', on: ' ON '.green_bg.bold, off: ' OFF '.red_bg.dark, &block) ⇒ Object
- #add_field(field, &block) ⇒ Object
- #add_group(group) ⇒ Object
- #add_item(item) ⇒ Object
- #badges ⇒ Object
- #clear_screen? ⇒ Boolean
- #colorize(text) ⇒ Object
- #display_badges ⇒ Object
- #display_fields ⇒ Object
- #display_header ⇒ Object
- #execute_option(selected) ⇒ Object
- #fields ⇒ Object
- #group_title_color ⇒ Object
- #header(&block) ⇒ Object
-
#initialize(makefile) ⇒ Menu
constructor
A new instance of Menu.
- #pause_on_success? ⇒ Boolean
- #run {|_self| ... } ⇒ Object
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 = [] = { group_title_color: :underline, clear_screen: true, pause_on_success: false } @highlights = {} @header = nil @field_set = nil @badge_set = nil end |
Instance Attribute Details
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
32 33 34 |
# File 'lib/make_menu/menu.rb', line 32 def groups @groups end |
#highlights ⇒ Object (readonly)
Returns the value of attribute highlights.
32 33 34 |
# File 'lib/make_menu/menu.rb', line 32 def highlights @highlights end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
32 33 34 |
# File 'lib/make_menu/menu.rb', line 32 def items @items end |
#makefile ⇒ Object (readonly)
Returns the value of attribute makefile.
32 33 34 |
# File 'lib/make_menu/menu.rb', line 32 def makefile @makefile end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
32 33 34 |
# File 'lib/make_menu/menu.rb', line 32 def 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 |
#badges ⇒ Object
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? [: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_badges ⇒ Object
116 117 118 |
# File 'lib/make_menu/menu.rb', line 116 def display_badges @badge_set.display if @badge_set end |
#display_fields ⇒ Object
104 105 106 |
# File 'lib/make_menu/menu.rb', line 104 def display_fields @field_set.display if @field_set end |
#display_header ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/make_menu/menu.rb', line 87 def display_header if @header @header.call else logo = " #{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 |
#fields ⇒ Object
100 101 102 |
# File 'lib/make_menu/menu.rb', line 100 def fields @field_set ||= FieldSet.new end |
#group_title_color ⇒ Object
140 141 142 |
# File 'lib/make_menu/menu.rb', line 140 def group_title_color [: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? [:pause_on_success] end |
#run {|_self| ... } ⇒ Object
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 |