lib/vedeu/cursor/cursor.rb in vedeu-0.5.3 vs lib/vedeu/cursor/cursor.rb in vedeu-0.5.4

- old
+ new

@@ -5,11 +5,18 @@ # class Cursor include Vedeu::Model include Vedeu::Toggleable + extend Forwardable + def_delegators :border, + :bx, + :bxn, + :by, + :byn + # @!attribute [r] attributes # @return [Hash] attr_reader :attributes # @!attribute [r] name @@ -26,17 +33,17 @@ # @!attribute [r] state # @return [Boolean|Symbol] attr_reader :state - # @!attribute [rw] x + # @!attribute [w] x # @return [Fixnum] - attr_accessor :x + attr_writer :x - # @!attribute [rw] y + # @!attribute [sw] y # @return [Fixnum] - attr_accessor :y + attr_writer :y # Returns a new instance of Vedeu::Cursor. # # @param attributes [Hash] # @option attributes name [String] The name of the interface this cursor @@ -48,19 +55,17 @@ # @option attributes x [Fixnum] The terminal x coordinate for the cursor. # @option attributes y [Fixnum] The terminal y coordinate for the cursor. # # @return [Vedeu::Cursor] def initialize(attributes = {}) - # Hack because Repository#by_name creates Cursor objects with just a - # name. + # @todo Hack because Repository#by_name creates Cursor objects with just a + # name. Intend to remove this. attributes = { name: attributes } if attributes.is_a?(String) @attributes = defaults.merge!(attributes) @attributes.each { |key, value| instance_variable_set("@#{key}", value) } - - @position = Vedeu::Position[@y, @x] end # Override Ruby's Object#inspect method to provide a more helpful output. # # @return [String] @@ -78,14 +83,14 @@ # original position. # # @return [String] def to_s if block_given? - "#{position}#{yield}#{position}#{visibility}" + "#{position}#{yield}#{visibility}" else - "#{position}#{visibility}" + "#{visibility}" end end alias_method :to_str, :to_s @@ -97,18 +102,18 @@ # # @return [Vedeu::Escape] def hide super - Vedeu::Output.render(visibility) + Vedeu::Output.render(visibility) if Vedeu.ready? end # Return the position of this cursor. # # @return [Vedeu::Position] def position - @position ||= Vedeu::Position[y, x] + @position = Vedeu::Position[y, x] end # Show a named cursor, or without a name, the cursor of the currently # focussed interface. # @@ -117,15 +122,40 @@ # # @return [Vedeu::Escape] def show super - Vedeu::Output.render(visibility) + Vedeu::Output.render(visibility) if Vedeu.ready? end + # @return [Fixnum] The column/character coordinate. + def x + @x = bx if @x < bx + @x = bxn if @x > bxn + + @attributes[:x] = @x + + @x + end + + # @return [Fixnum] The row/line coordinate. + def y + @y = by if @y < by + @y = byn if @y > byn + + @attributes[:y] = @y + + @y + end + private + # @see Vedeu::Borders#by_name + def border + @border ||= Vedeu.borders.by_name(name) + end + # The default values for a new instance of this class. # # @return [Hash] def defaults { @@ -141,12 +171,16 @@ # Returns the escape sequence for setting the visibility of the cursor. # # @return [String] def visibility - return Vedeu::Escape.new(Vedeu::Esc.show_cursor) if visible? + if visible? + Vedeu::Escape.new(position: position, value: Vedeu::Esc.show_cursor) - Vedeu::Escape.new(Vedeu::Esc.hide_cursor) + else + Vedeu::Escape.new(position: position, value: Vedeu::Esc.hide_cursor) + + end end end # Cursor end # Vedeu