Sha256: 44e0dafd4dede6cc8e51fdc773d0b1ea204a152620d470faa2483b96fd34b87e

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

module Ruco
  class Window
    OFFSET = 5

    attr_accessor :position, :lines, :columns, :top, :left
    attr_reader :cursor

    def initialize(lines, columns)
      @lines = lines
      @columns = columns
      @top = 0
      @left = 0
      @cursor = Position.new(0,0)
    end

    def crop(content)
      lines = content[visible_lines] || []
      lines[@lines-1] ||= nil
      lines.map do |line|
        line ||= ''
        line[visible_columns] || ''
      end
    end

    def position=(pos)
      self.top = pos.line - line_offset unless visible_lines.include?(pos.line)
      self.left = pos.column - column_offset  unless visible_columns.include?(pos.column)
      @cursor = Position.new(pos.line - @top, pos.column - @left)
    end

    def color_mask(selection)
      mask = Array.new(lines)
      return mask unless selection

      mask.map_with_index do |_,line|
        visible = visible_area(line)
        next unless selection.overlap?(visible)

        first = [selection.first, visible.first].max
        last = [selection.last, visible.last].min

        [
          [first[1]-left, Curses::A_REVERSE],
          [last[1]-left, Curses::A_NORMAL]
        ]
      end
    end

    def top=(x)
      @top = [x,0].max
    end

    def left=(x)
      @left = [x,0].max
    end

    private

    def visible_area(line)
      line += @top
      start_of_line = [line, @left]
      last_visible_column = @left + @columns
      end_of_line = [line, last_visible_column]
      start_of_line..end_of_line
    end

    def line_offset
      @lines / 2
    end

    def column_offset
      @columns / 2
    end

    def visible_lines
      @top..(@top+@lines-1)
    end

    def visible_columns
      @left..(@left+@columns-1)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruco-0.0.40 lib/ruco/window.rb
ruco-0.0.39 lib/ruco/window.rb
ruco-0.0.38 lib/ruco/window.rb
ruco-0.0.37 lib/ruco/window.rb