Sha256: 0565e80cdef301bc432a0513ee6d569275a7e9be66bb16a309a99567eab019bd

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

require 'remedy/view'
require 'remedy/size'
require 'remedy/content'

module Remedy
  class Viewport
    def draw content, center = Size.new(0,0), header = [], footer = []
      range = range_find content, center, content_size(header,footer)

      if content.size.fits_into? range then
        viewable_content = content
      else
        viewable_content = content.excerpt *range
      end

      view = View.new viewable_content, header, footer

      ANSI.screen.safe_reset!
      Console.output << view
    end

    def range_find partial, center, heightwidth
      row_size, col_size = heightwidth
      row_limit, col_limit = partial.size

      center_row, center_col = center.coords

      row_range = center_range center_row, row_size, row_limit
      col_range = center_range center_col, col_size, col_limit
      [row_range, col_range]
    end

    def content_size header, footer
      trim = Size [header.length + footer.length, 0]
      size - trim
    end

    def size
      Size Console.size
    end

    def center_range center, width, limit
      range_start = center - (width / 2)

      if range_start + width > limit then
        range_start = limit - width
      end

      if range_start < 0 then
        range_start = 0
      end

      range_end = range_start + width

      (range_start...range_end)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
remedy-0.1.0 lib/remedy/viewport.rb
remedy-0.0.5 lib/remedy/viewport.rb
remedy-0.0.4 lib/remedy/viewport.rb