Sha256: 1de8f7bd85f7a64a16183db658a8547dc3742ef59f6ff6a2e8c2ca334f49a773

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

module Vedeu
  class Menu
    def initialize(collection)
      @collection = collection
      @current    = 0
      @selected   = nil
    end

    def current
      @current
    end

    def selected
      @selected
    end

    def current_item
      [@current, @collection[@current]]
    end

    def selected_item
      return nil unless @selected

      [@selected, @collection[@selected]]
    end

    def items
      items = []
      @collection.each_with_index do |item, index|
        if index    == @current && index == @selected
          items << [true, true, item]

        elsif index == @current
          items << [false, true, item]

        elsif index == @selected
          items << [true, false, item]

        else
          items << [false, false, item]

        end
      end
      items
    end

    def render
      lines = []
      items.each do |(sel, cur, item)|
        if sel && cur
          lines << "*> #{item}"

        elsif cur
          lines << " > #{item}"

        elsif sel
          lines << "*  #{item}"

        else
          lines << "   #{item}"

        end
      end
      lines
    end

    def top
      @current = 0
    end

    def bottom
      @current = last
    end

    def next
      @current += 1 if @current < last

      self
    end

    def prev
      @current -= 1 if @current > 0

      self
    end

    def select
      @selected = @current
    end

    def deselect
      @selected = nil
    end

    def last
      @collection.size - 1
    end

    def size
      @collection.size
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
vedeu-0.0.38 lib/vedeu/support/menu.rb
vedeu-0.0.37 lib/vedeu/support/menu.rb
vedeu-0.0.36 lib/vedeu/support/menu.rb
vedeu-0.0.35 lib/vedeu/support/menu.rb
vedeu-0.0.34 lib/vedeu/support/menu.rb
vedeu-0.0.33 lib/vedeu/support/menu.rb
vedeu-0.0.32 lib/vedeu/support/menu.rb