Sha256: 9f522c368f4626c425a0631e69e8557f19fde8590767ac05d40a01f0be36f65f

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

module VER
  class Text
    Index = Struct.new(:widget, :idx, :y, :x)
    class Index
      include Comparable

      def initialize(widget, idx)
        self.widget, self.idx = widget, idx.to_str
      end

      def to_index
        self
      end

      def <=>(other)
        self_idx, other_idx = idx, other.to_index.idx
        return  1 if widget.compare(self_idx, '>',  other_idx)
        return -1 if widget.compare(self_idx, '<',  other_idx)
        return  0 if widget.compare(self_idx, '==', other_idx)
      end

      def delta(other)
        y_diff = other.y - y

        if y_diff == 0
          (other.x - x).abs
        else
          y_diff.abs
        end
      end

      def y
        self[:y] ||= idx.split('.', 2)[0].to_i
      end

      def x
        self[:x] ||= idx.split('.', 2)[1].to_i
      end

      def linestart
        "#{idx} linestart"
      end

      def lineend
        "#{idx} lineend"
      end

      def wordstart
        "#{idx} wordstart"
      end

      def wordend
        "#{idx} wordend"
      end

      def next
        self.class.new(widget, "#{y + 1}.#{x}")
      end

      def prev
        self.class.new(widget, "#{y - 1}.#{x}")
      end

      def split
        return y, x
      end

      def inspect
        "#<Text::Index #{self}>"
      end

      def to_str
        idx
      end

      def to_s
        "#{y}.#{x}"
      end

      def to_i
        y
      end

      def to_tcl
        Tk::TclString.new(to_s)
      end

      def upto(other)
        if block_given?
          y.upto(other.y) do |line|
            yield self.class.new(widget, "#{line}.0")
          end
        else
          Enumerator.new(self, :upto, other)
        end
      end

      def is_a?(obj)
        obj == String || super
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ver-2009.11.29 lib/ver/text/index.rb
ver-2009.11.28 lib/ver/text/index.rb