lib/ppcurses/menu/Menu.rb in ppcurses-0.0.21 vs lib/ppcurses/menu/Menu.rb in ppcurses-0.0.22

- old
+ new

@@ -3,117 +3,89 @@ require_relative 'BaseMenu.rb' require 'curses' module PPCurses - #noinspection RubyResolve + #noinspection RubyResolve class Menu < BaseMenu - def initialize( menu_items, action_items ) - @items = Array.new - @actions = Array.new - @selection = 0 - - max_menu_width = 0 - - menu_items.each do |item| - @items.push item - if item.length > max_menu_width then max_menu_width = item.length end - end - - unless action_items.nil? - action_items.each do |item| - @actions.push item - end - end - - w_height = @items.length + 4 - w_width = max_menu_width + 4 - @win = Window.new(w_height,w_width,(lines-w_height) / 2, (cols-w_width)/2) - - @win.timeout=-1 - # Enables reading arrow keys in getch - @win.keypad(true) - end - - def show - @win.box('|', '-') + def show + @win.box(self.side_wall_char, self.top_bot_wall_char) y = 2 x = 2 - for i in 0...@items.length + (0...@menu_items.length).each { |i| @win.setpos(y, x) - if @selection == i then @win.attron(A_REVERSE) end - @win.addstr(@items[i]) - if @selection == i then @win.attroff(A_REVERSE) end + @win.attron(A_REVERSE) if @selection == i + @win.addstr(@menu_items[i].display_string) + @win.attroff(A_REVERSE) if @selection == i y += 1 - end + } @win.refresh - @sub_menu.show() if @sub_menu - end + @sub_menu.show if @sub_menu + end - def set_global_action(action) - @global_action = action - end + def set_global_action(action) + @global_action = action + end - def menu_selection + def menu_selection while 1 c = @win.getch not_processed = !self.handle_menu_selection(c) - if c == 27 # ESCAPE - self.hide() + if c == ESCAPE + self.hide break end - if not_processed then - @sub_menu.handle_menu_selection(c) if @sub_menu - end + @sub_menu.handle_menu_selection(c) if not_processed && @sub_menu end - end - - def handle_menu_selection(c) - n_choices = @items.length + end - if c == KEY_UP - if @selection == 0 then @selection = n_choices-1 else @selection -= 1 end - self.show() - return true - end + def handle_menu_selection(c) + n_choices = @menu_items.length - if c == KEY_DOWN - if @selection == n_choices-1 then @selection = 0 else @selection += 1 end - self.show() - return true - end + if c == KEY_UP + (@selection == 0) ? @selection = n_choices - 1 : @selection -= 1 + self.show + return true + end - if c == 10 # ENTER + if c == KEY_DOWN + (@selection == n_choices-1) ? @selection = 0 : @selection += 1 + self.show + return true + end - unless @global_action.nil? - @global_action.execute() - end + if c == ENTER - unless @actions.nil? or @actions[@selection].nil? - @actions[@selection].execute() - end + unless @global_action.nil? + @global_action.execute + end - self.show() - return true - end + unless @menu_items[@selection].action.nil? + @menu_items[@selection].action.execute + end - false - end + self.show + return true + end + item_consumed = @menu_items[@selection].handle_key(c) + if item_consumed + self.show + end - def close - @win.close() - end + item_consumed + end - end + + end end