Sha256: 1df405cb7d7147495bb03138586ef73bc88474182fa1c1c433d53fd2679ad162

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

module Vedeu

  # Handle the refreshing (redrawing) of a cursor, without redrawing the whole
  # interface; unless the cursor's offset has caused the view to change.
  #
  class RefreshCursor

    # @param (see #initialize)
    def self.render(name)
      new(name).render
    end

    # Returns a new instance of Vedeu::RefreshCursor.
    #
    # @param name [String] The name of the cursor.
    # @return [Vedeu::RefreshCursor]
    def initialize(name)
      @name = name
    end

    # @return [Array]
    def render
      Vedeu::Refresh.by_name(name) if refresh_view?

      Vedeu::Terminal.output(new_cursor.to_s)
    end

    private

    # @!attribute [r] name
    # @return [String]
    attr_reader :name

    # @return [Boolean]
    def refresh_view?
      new_cursor.ox >= interface_width || new_cursor.oy >= interface_height
    end

    # @return [Vedeu::Cursor]
    def new_cursor
      @new_cursor ||= Vedeu::Cursor.new(cursor.attributes.merge(position))
    end

    # @return [Hash<Symbol => Fixnum>]
    def position
      {
        x: validated_position.x,
        y: validated_position.y,
      }
    end

    # @return [Vedeu::PositionValidator]
    def validated_position
      @position ||= Vedeu::PositionValidator.validate(interface,
                                                      cursor.x,
                                                      cursor.y)
    end

    # @return [Vedeu::Cursor]
    def cursor
      @cursor ||= Vedeu.cursors.find(name)
    end

    # @return [Fixnum]
    def interface_height
      interface.border.height
    end

    # @return [Fixnum]
    def interface_width
      interface.border.width
    end

    # @return [Vedeu::Interface]
    def interface
      @interface ||= Vedeu.interfaces.find(name)
    end

  end # RefreshCursor

end # Vedeu

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.4.11 lib/vedeu/cursor/refresh_cursor.rb