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 = '', &block) ⇒ Object
- #add_field(label = '', value_from_file: nil, color: :normal, none: '[none]'.dark, &block) ⇒ Object
- #add_group(group) ⇒ Object
- #add_item(item) ⇒ Object
- #badges ⇒ Object
- #badges_first? ⇒ Boolean
- #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
Returns a new instance of Menu.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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, badges_first: true } @highlights = {} @header = nil @field_set = nil @badge_set = nil end |
Instance Attribute Details
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
33 34 35 |
# File 'lib/make_menu/menu.rb', line 33 def groups @groups end |
#highlights ⇒ Object (readonly)
Returns the value of attribute highlights.
33 34 35 |
# File 'lib/make_menu/menu.rb', line 33 def highlights @highlights end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
33 34 35 |
# File 'lib/make_menu/menu.rb', line 33 def items @items end |
#makefile ⇒ Object (readonly)
Returns the value of attribute makefile.
33 34 35 |
# File 'lib/make_menu/menu.rb', line 33 def makefile @makefile end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
33 34 35 |
# File 'lib/make_menu/menu.rb', line 33 def @options end |
Instance Method Details
#add_badge(label = '', &block) ⇒ Object
115 116 117 |
# File 'lib/make_menu/menu.rb', line 115 def add_badge(label = '', &block) badges.add label, &block end |
#add_field(label = '', value_from_file: nil, color: :normal, none: '[none]'.dark, &block) ⇒ Object
103 104 105 |
# File 'lib/make_menu/menu.rb', line 103 def add_field(label = '', value_from_file: nil, color: :normal, none: '[none]'.dark, &block) fields.add label, value_from_file: value_from_file, color: color, none: none, &block end |
#add_group(group) ⇒ Object
137 138 139 140 |
# File 'lib/make_menu/menu.rb', line 137 def add_group(group) groups << group group end |
#add_item(item) ⇒ Object
142 143 144 145 |
# File 'lib/make_menu/menu.rb', line 142 def add_item(item) items << item item end |
#badges ⇒ Object
119 120 121 |
# File 'lib/make_menu/menu.rb', line 119 def badges @badge_set ||= BadgeSet.new end |
#badges_first? ⇒ Boolean
159 160 161 |
# File 'lib/make_menu/menu.rb', line 159 def badges_first? [:badges_first] end |
#clear_screen? ⇒ Boolean
151 152 153 |
# File 'lib/make_menu/menu.rb', line 151 def clear_screen? [:clear_screen] end |
#colorize(text) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/make_menu/menu.rb', line 163 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
123 124 125 |
# File 'lib/make_menu/menu.rb', line 123 def display_badges @badge_set.display if @badge_set end |
#display_fields ⇒ Object
111 112 113 |
# File 'lib/make_menu/menu.rb', line 111 def display_fields @field_set.display if @field_set end |
#display_header ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/make_menu/menu.rb', line 94 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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/make_menu/menu.rb', line 73 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
107 108 109 |
# File 'lib/make_menu/menu.rb', line 107 def fields @field_set ||= FieldSet.new end |
#group_title_color ⇒ Object
147 148 149 |
# File 'lib/make_menu/menu.rb', line 147 def group_title_color [:group_title_color] end |
#header(&block) ⇒ Object
90 91 92 |
# File 'lib/make_menu/menu.rb', line 90 def header(&block) @header = block end |
#pause_on_success? ⇒ Boolean
155 156 157 |
# File 'lib/make_menu/menu.rb', line 155 def pause_on_success? [:pause_on_success] end |
#run {|_self| ... } ⇒ Object
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 65 66 67 68 69 70 71 |
# File 'lib/make_menu/menu.rb', line 35 def run yield self if block_given? Builder.build(makefile, self) loop do system 'clear' if clear_screen? display_header if badges_first? display_badges display_fields else display_fields display_badges end 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 |