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

- old
+ new

@@ -1,117 +1,117 @@ # Curses reference: # http://www.ruby-doc.org/stdlib-1.9.3/libdoc/curses/rdoc/Curses.html require_relative 'BaseMenu.rb' -require "curses" +require 'curses' module PPCurses - class Menu < BaseMenu + #noinspection RubyResolve + class Menu < BaseMenu - def initialize( menuItems, actionItems ) - @items = Array.new + def initialize( menu_items, action_items ) + @items = Array.new + @actions = Array.new + @selection = 0 - @maxMenuWidth = 0 + max_menu_width = 0 - menuItems.each do |item| - @items.push item - if item.length > @maxMenuWidth then @maxMenuWidth = item.length end - end + menu_items.each do |item| + @items.push item + if item.length > max_menu_width then max_menu_width = item.length end + end - @selection = 0 + unless action_items.nil? + action_items.each do |item| + @actions.push item + end + end - unless actionItems.nil? - @actions = Array.new - actionItems.each do |item| - @actions.push item - end - end - - winHeight = @items.length + 4 - winWidth = @maxMenuWidth + 4 - @win = Window.new(winHeight,winWidth,(lines-winHeight) / 2, (cols-winWidth)/2) + 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) + @win.timeout=-1 + # Enables reading arrow keys in getch + @win.keypad(true) end - def show() - @win.box("|", "-") - y = 2 - x = 2 + def show + @win.box('|', '-') + y = 2 + x = 2 - for i in 0...@items.length - @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 - y += 1 - end + for i in 0...@items.length + @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 + y += 1 + end - @win.refresh + @win.refresh - @subMenu.show() if @subMenu + @sub_menu.show() if @sub_menu end - def setGlobalAction(action) - @gAction = action + def set_global_action(action) + @global_action = action end - def getMenuSelection() + def menu_selection - while(1) - c = @win.getch + while 1 + c = @win.getch - processed = self.handleMenuSelection(c) + not_processed = !self.handle_menu_selection(c) - if c == 27 then # ESCAPE - self.hide() - break - end + if c == 27 # ESCAPE + self.hide() + break + end - if processed == false then - @subMenu.handleMenuSelection(c) if @subMenu - end + if not_processed then + @sub_menu.handle_menu_selection(c) if @sub_menu + end - end + end end - def handleMenuSelection(c) + def handle_menu_selection(c) n_choices = @items.length - if c == KEY_UP then + if c == KEY_UP if @selection == 0 then @selection = n_choices-1 else @selection -= 1 end self.show() return true end - if c == KEY_DOWN then + if c == KEY_DOWN if @selection == n_choices-1 then @selection = 0 else @selection += 1 end self.show() return true end - if c == 10 then # ENTER + if c == 10 # ENTER - unless @gAction.nil? - @gAction.execute() + unless @global_action.nil? + @global_action.execute() end unless @actions.nil? or @actions[@selection].nil? - @actions[@selection].execute() + @actions[@selection].execute() end self.show() return true end - return false + false end - def close() + def close @win.close() end end