Sha256: 68b714bcca7a385a7a944cd915176fefd7706dbb10a09a2960553ce28640f42a

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

require 'wisper'

module Daigaku
  module Views

    class Menu
      include Views
      include Wisper::Publisher

      TOP_BAR_TEXT = [
        'Use *UP KEY* and *DOWN KEY* for menu navigation',
        'Enter menu with *RETURN*',
        'Go back with *BACKSPACE*',
        'Exit with *ESC*'
      ].join('  |  ')

      def initialize
        @position = 0
      end

      def enter(*args)
        if self.class.private_method_defined?(:before_enter)
          before_enter(*args)
        end

        @window = default_window
        top_bar = TopBar.new(@window, TOP_BAR_TEXT)
        show sub_window_below_top_bar(@window, top_bar)
      end

      def reenter(*args)
        if self.class.private_method_defined?(:before_reenter)
          before_reenter(*args)
        end

        enter(*args)
      end

      protected

      def show(window)
        draw(window, @position)
        interact_with(window)
      end

      def draw(window, active_index = 0)
        window.attrset(A_NORMAL)
        window.setpos(0, 1)
        window.print_markdown header_text

        items.each_with_index do |item, index|
          window.setpos(index + 2, 1)
          window.print_indicator(models[index])
          window.attrset(index == active_index ? A_STANDOUT : A_NORMAL)
          window.write " #{item.to_s} "
          window.attrset(A_NORMAL)
          window.write " #{items_info[index].try(:join, ' ')}"
        end

        window.refresh
      end

      def interact_with(window)
        raise "Please implement the method #interact_with!"
      end

      def models
        raise "Please implement the method #models!"
      end

      def items
        raise "Please implement the method #items!"
      end

      def header_text
        raise "Please implement the method #header_text!"
      end

      def items_info
        @items_info || []
      end

      def items_info=(items_info)
        @items_info = items_info
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
daigaku-0.3.0 lib/daigaku/views/menu.rb
daigaku-0.2.0 lib/daigaku/views/menu.rb
daigaku-0.1.1 lib/daigaku/views/menu.rb
daigaku-0.1.0 lib/daigaku/views/menu.rb