Class: ScripTTY::MultilineBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptty/multiline_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height, width) ⇒ MultilineBuffer

Returns a new instance of MultilineBuffer.



24
25
26
27
28
# File 'lib/scriptty/multiline_buffer.rb', line 24

def initialize(height, width)
  @height = height
  @width = width
  clear!
end

Instance Attribute Details

#contentObject (readonly)

contents of the screen buffer (mainly for debugging/testing)



23
24
25
# File 'lib/scriptty/multiline_buffer.rb', line 23

def content
  @content
end

#heightObject (readonly)

buffer height (in lines)



21
22
23
# File 'lib/scriptty/multiline_buffer.rb', line 21

def height
  @height
end

#widthObject (readonly)

buffer width (in columns)



22
23
24
# File 'lib/scriptty/multiline_buffer.rb', line 22

def width
  @width
end

Instance Method Details

#clear!Object

Clear the buffer



31
32
33
34
35
36
37
# File 'lib/scriptty/multiline_buffer.rb', line 31

def clear!
  @content = []   # Array of mutable Strings
  @height.times {
    @content << " "*@width
  }
  nil
end

#get_at(row, column, limit = 1) ⇒ Object

Return characters starting at the specified location.

The limit parameter specifies the maximum number of bytes to return. If limit is negative, then everything up to the end of the line is returned.



65
66
67
68
69
70
71
72
# File 'lib/scriptty/multiline_buffer.rb', line 65

def get_at(row, column, limit=1)
  return nil if row < 0 or row >= height or column < 0 or column >= width # XXX should we raise an exception here?
  if limit >= 0
    @content[row][column,limit]
  else
    @content[row][column..-1]
  end
end

#replace_at(row, column, value) ⇒ Object

Write a string (or array of strings) to the specified location.

row & column are zero-based

Returns the characters that were replaced



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/scriptty/multiline_buffer.rb', line 44

def replace_at(row, column, value)
  if value.is_a?(Array)
    orig = []
    value.each_with_index do |string, i|
      orig << replace_at(row+i, column, string)
    end
    orig
  else
    # value is a string
    return nil if row < 0 or row >= height or column < 0 or column >= width # XXX should we raise an exception here?
    orig = @content[row][column,value.length]
    @content[row][column,value.length] = value
    @content[row] = @content[row][0,width]    # truncate to maximum width
    orig
  end
end

#scroll_down_region(row0, col0, row1, col1, count) ⇒ Object

Scroll the specified rectangle down by the specified number of lines. Return true.



82
83
84
# File 'lib/scriptty/multiline_buffer.rb', line 82

def scroll_down_region(row0, col0, row1, col1, count)
  scroll_region_vertical(:down, row0, col0, row1, col1, count)
end

#scroll_left_region(row0, col0, row1, col1, count) ⇒ Object



86
87
88
# File 'lib/scriptty/multiline_buffer.rb', line 86

def scroll_left_region(row0, col0, row1, col1, count)
  scroll_region_horizontal(:left, row0, col0, row1, col1, count)
end

#scroll_right_region(row0, col0, row1, col1, count) ⇒ Object



90
91
92
# File 'lib/scriptty/multiline_buffer.rb', line 90

def scroll_right_region(row0, col0, row1, col1, count)
  scroll_region_horizontal(:right, row0, col0, row1, col1, count)
end

#scroll_up_region(row0, col0, row1, col1, count) ⇒ Object

Scroll the specified rectangle up by the specified number of lines. Return true.



76
77
78
# File 'lib/scriptty/multiline_buffer.rb', line 76

def scroll_up_region(row0, col0, row1, col1, count)
  scroll_region_vertical(:up, row0, col0, row1, col1, count)
end