Sha256: caf57abfcf00344448129e21a7379cd56b6dc1ee04143b952552b7dcce473ec2

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

# -*- encoding : utf-8 -*-

module PPCurses

  PP_MIXED_STATE = -1
  PP_OFF_STATE = 0
  PP_ON_STATE = 1

  SELECTED_CHAR = '✓'

  class MenuItem
    attr_accessor :title

    # Who to notify when menu is selected chosen?
    # The target should be a method selector, and
    attr_accessor :target

    attr_accessor :state
    attr_accessor :selectable

    def initialize( title )
      @title = title
      @state = PP_OFF_STATE
      @selectable = false
    end

    def display_string
      if @state == PP_OFF_STATE
        return '  ' + @title
      end

      SELECTED_CHAR + ' ' + @title
    end


    def toggle_on_off_state
      if @state == PP_OFF_STATE
        @state = PP_ON_STATE
        return
      end

      if @state == PP_ON_STATE
        @state = PP_OFF_STATE
      end

    end

    def call_target
      unless @target.nil?
        case @target.arity
          when 0
            @target.call
          when 1
            @target.call(self)
          else
            raise ArgumentError, "Too many parameters for target don't know what to do"
        end
      end
    end



    def handle_key(key)

      if key == ' ' and @selectable
        toggle_on_off_state
        call_target
        return true
      end

      if key == ENTER
        call_target
        return true
      end

      false
    end


  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ppcurses-0.1.2 lib/ppcurses/menu/menu_item.rb
ppcurses-0.1.1 lib/ppcurses/menu/menu_item.rb
ppcurses-0.1.0 lib/ppcurses/menu/menu_item.rb
ppcurses-0.0.25 lib/ppcurses/menu/menu_item.rb
ppcurses-0.0.24 lib/ppcurses/menu/menu_item.rb