Class: Io::PromptMenu

Inherits:
Object
  • Object
show all
Includes:
Io
Defined in:
lib/rbcurse/io.rb

Overview

An encapsulated form of yesterday’s Most Menu It keeps the internals away from the user. Its not really OOP in the sense that the PromptMenu is not a MenuItem. That’s how it is in our Menu system, and that led to a lot of painful coding (at least for me). This is quite simple. A submenu contains a PromptMenu in its action object and is evaluated in a switch. A recursive loop handles submenus.

Prompting of menu options with suboptions etc. A block of code or symbol or proc is executed for any leaf node This allows us to define different menus for different objects on the screen, and not have to map all kinds of control keys for operations, and have the user remember them. Only one key invokes the menu and the rest are ordinary characters.

Constant Summary

Constants included from Io

ERROR_COLOR_PAIR, FOOTER_COLOR_PAIR, LINEONE, MAIN_WINDOW_COLOR_PAIR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Io

#askchoice, #askyesno, #askyesnocancel, #clear_error, #clear_this, #get_string, #newaskyesno, #old_print_header, #old_print_top_right, #print_action, #print_error, #print_footer_help, #print_header, #print_headers, #print_help, #print_help_page, #print_in_middle, #print_key_labels, #print_key_labels_row, #print_screen_labels, #print_status, #print_this, #print_top_right, #rbgetstr

Constructor Details

#initialize(caller, text = "Choose:") ⇒ PromptMenu

Returns a new instance of PromptMenu.



587
588
589
590
591
# File 'lib/rbcurse/io.rb', line 587

def initialize caller,  text="Choose:"
  @caller = caller
  @text = text
  @options = []
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



586
587
588
# File 'lib/rbcurse/io.rb', line 586

def options
  @options
end

#textObject (readonly)

Returns the value of attribute text.



585
586
587
# File 'lib/rbcurse/io.rb', line 585

def text
  @text
end

Instance Method Details

#add(menuitem) ⇒ Object



592
593
594
# File 'lib/rbcurse/io.rb', line 592

def add menuitem
  @options << menuitem
end

#create_mitem(*args) ⇒ Object



595
596
597
# File 'lib/rbcurse/io.rb', line 595

def create_mitem *args
  item = CMenuItem.new(*args.flatten)
end

#display(win, r, c, color) ⇒ Object

Display the top level menu and accept user input Calls actions or symbols upon selection, or traverses submenus

Parameters:

  • win

    window

  • r,

    c row and col to display on

  • color

    text color (use $datacolor if in doubt)

  • menu

    array of CMenuItem structs

Returns:

  • retvalue of last call or send, or 0



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'lib/rbcurse/io.rb', line 605

def display win, r, c, color
  menu = @options
  $log.debug " DISP MENU "
  ret = 0
  while true
    str = @text.dup
    h = {}
    valid = []
    menu.each{ |item|
      str << "(%c) %s " % [ item.hotkey, item.label ]
      h[item.hotkey] = item
      valid << item.hotkey
    }
    #$log.debug " valid are #{valid} "
    color = $datacolor
    print_this(win, str, color, r, c)
    ch=win.getchar()
    #$log.debug " got ch #{ch} "
    next if ch < 0 or ch > 255
    ch = ch.chr
    index = valid.index ch
    if index.nil?
      clear_this win, r, c, color, str.length
      print_this(win, "Not valid. Valid are #{valid}", color, r,c)
      sleep 1
      next
    end
    #$log.debug " index is #{index} "
    item = h[ch]
    desc = item.desc
    #desc ||= "Could not find desc for #{ch} "
    desc ||= ""
    clear_this win, r, c, color, str.length
    print_this(win, desc, color, r,c)
    action = item.action
    case action
      #when Array
    when PromptMenu
      # submenu
      menu = action.options
      str = "%s: " % action.text 
    when Proc
      ret = action.call
      break
    when Symbol
      ret = @caller.send(action)
      break
    else 
      $log.debug " Unidentified flying class #{action.class} "
      break
    end
  end # while
  return ret # ret val of last send or call
end