Sha256: b067f6d639420d98d54d06191e044cf22713c3d32be484132d4689f9c99ffd59

Contents?: true

Size: 1.43 KB

Versions: 4

Compression:

Stored size: 1.43 KB

Contents

require_relative 'BaseAction.rb'

#noinspection RubyResolve
module PPCurses

  class GetEnumeratedStringAction < PromptAction

    # enumeration is a list of possible values
    # i.e. CD, Vinyl, MP3
    def initialize(prompt, enumeration) 
      super(prompt)

      # verify enumeration is an array
      unless enumeration.respond_to?('each_with_index') then
        raise
      end

      @options = enumeration
      @current_option = 0
    end

    def print_prompt
      super()
      @options.each_with_index  do |option, index|
        @win.addstr(option)
        if index == @current_option then
          @win.addstr(' [X] ')
        else
          @win.addstr(' [ ] ')
        end

      end
    end

    def execute
      print_prompt()
      # Enables reading arrow keys in getch 
      @win.keypad(true)
      while 1
        noecho
        c = @win.getch

        if c == KEY_LEFT then @current_option = @current_option-1 end
        if c == KEY_RIGHT then @current_option= @current_option+1 end
        if c == 10 then break end

        if @current_option < 0 then @current_option = @options.length-1 end
        if @current_option > @options.length-1 then @current_option = 0 end

        echo
        print_prompt()
      end
      echo
      # Go to next line so that further actions to overwrite
      # the choice
      @win.setpos(@win.cury() + 1, x_padding())
    end

    def data
      @options[@current_option]
    end 

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ppcurses-0.0.24 lib/ppcurses/actions/GetEnumeratedStringAction.rb
ppcurses-0.0.23 lib/ppcurses/actions/GetEnumeratedStringAction.rb
ppcurses-0.0.22 lib/ppcurses/actions/GetEnumeratedStringAction.rb
ppcurses-0.0.21 lib/ppcurses/actions/GetEnumeratedStringAction.rb