Class: PPCurses::Menu

Inherits:
BaseMenu show all
Defined in:
lib/ppcurses/menu/Menu.rb

Overview

noinspection RubyResolve

Instance Attribute Summary

Attributes inherited from BaseMenu

#menu_items, #selection, #side_wall_char, #top_bot_wall_char

Instance Method Summary collapse

Methods inherited from BaseMenu

#build_menu_items, #close, #create_window, #find_max_menu_width, #hide, #initialize, #selected_menu_name, #set_sub_menu

Constructor Details

This class inherits a constructor from PPCurses::BaseMenu

Instance Method Details

#handle_menu_selection(c) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ppcurses/menu/Menu.rb', line 51

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


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ppcurses/menu/Menu.rb', line 33

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

#set_global_action(action) ⇒ Object



29
30
31
# File 'lib/ppcurses/menu/Menu.rb', line 29

def set_global_action(action)
  @global_action = action
end

#showObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ppcurses/menu/Menu.rb', line 11

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(A_REVERSE) if @selection == i
    @win.addstr(@menu_items[i].display_string)
    @win.attroff(A_REVERSE) if @selection == i
    y += 1
  }

   @win.refresh

   @sub_menu.show if @sub_menu
end