Sha256: 4e5f2ee9057035db6949c3abae61d6595efa535925001cfa789153b1588f7ab3

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

module PPCurses
  #noinspection RubyResolve
  class Menu < BaseMenu

    attr_accessor :selection

    def show
      @win.box(self.side_wall_char, self.top_bot_wall_char)
      y = 2
      x = 2

      (0...@menu_items.length).each { |i|
        @win.setpos(y, x)
        @win.attron(Curses::A_REVERSE) if @selection == i
        @win.addstr(@menu_items[i].display_string)
        @win.attroff(Curses::A_REVERSE) if @selection == i
        y += 1
      }

       @win.refresh

       @sub_menu.show if @sub_menu
    end

    def set_global_action(action)
      PPCurses.implements_protocol(action, %w(execute))
      @global_action = action
    end

    def menu_selection

      while 1
        c = @win.getch

        not_processed = !self.handle_menu_selection(c)

        if c == ESCAPE
          self.hide
          break
        end

        @sub_menu.handle_menu_selection(c) if not_processed && @sub_menu

      end

    end

    def handle_menu_selection(c)
      n_choices = @menu_items.length

      if c == KEY_UP
        (@selection == 0) ? @selection = n_choices - 1 : @selection -= 1
        self.show
        return true
      end

      if c == KEY_DOWN
        (@selection == n_choices-1) ? @selection = 0 : @selection += 1
        self.show
        return true
      end

      if c == ENTER && !@global_action.nil?

        unless @global_action.nil?
          @global_action.execute
        end

        self.show
        return true
      end

      item_consumed = @menu_items[@selection].handle_key(c)
      if item_consumed
        self.show
      end

      item_consumed
    end


  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ppcurses-0.1.2 lib/ppcurses/menu/Menu.rb
ppcurses-0.1.1 lib/ppcurses/menu/Menu.rb
ppcurses-0.1.0 lib/ppcurses/menu/Menu.rb