Class: Remedy::Viewport

Inherits:
Object
  • Object
show all
Defined in:
lib/remedy/viewport.rb

Instance Method Summary collapse

Instance Method Details

#available_space(header, footer) ⇒ Object

This determines the maximum amount of room left available for Content after taking into consideration the height of the Header and Footer



33
34
35
36
# File 'lib/remedy/viewport.rb', line 33

def available_space header, footer
  trim = Size [header.height + footer.height, 0]
  size - trim
end

#draw(content, scroll = Size.zero, header = Partial.new, footer = Partial.new) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/remedy/viewport.rb', line 8

def draw content, scroll = Size.zero, header = Partial.new, footer = Partial.new
  range = range_find content, scroll, available_space(header,footer)

  viewable_content = content.excerpt *range

  view = View.new viewable_content, header, footer

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

#get_range(offset, actual, available) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/remedy/viewport.rb', line 42

def get_range offset, actual, available
  # if the actual content can fit into the available space, then we're done
  return (0...actual) if actual <= available

  # otherwise start looking at the scrolling offset, if any

  # clamp the offset within the possible range of the actual content
  if offset < 0 then
    range_start = 0
  elsif offset > actual then
    range_start = actual
  else
    range_start = offset
  end

  # determine the subset of content that can be displayed
  range_end = range_start + (available - offset)

  (range_start...range_end)
end

#range_find(partial, scroll, available_heightwidth) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/remedy/viewport.rb', line 19

def range_find partial, scroll, available_heightwidth
  avail_height, avail_width = available_heightwidth
  partial_height, partial_width = partial.size

  center_row, center_col = scroll

  row_range = get_range center_row, partial_height, avail_height
  col_range = get_range center_col, partial_width, avail_width

  [row_range, col_range]
end

#sizeObject



38
39
40
# File 'lib/remedy/viewport.rb', line 38

def size
  Size Console.size
end