blob: 006aaf4ceb3f5fc0ad5eb0e9a4bd6dfcc4161bae (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
class Buffer
def initialize(size)
@size = size
@buff = []
@buff_type = []
@current = 0
@wrap = false
@before = false
@after = false
end
def size()
return @buff.length
end
def maxlen()
maxlen = 0
@buff.each do |string|
if string.length > maxlen
maxlen = string.length
end
end
return maxlen
end
def push(string,type=0)
if(string.chomp.empty?) then string = " " end
string.split( /\r?\n/ ).each do |line|
@buff[@current] = line
@buff_type[@current] = type
if(@size > 0)
@current = (1+@current) % @size
else
@current = 1+@current
end
if(@current == 0) then @wrap = true end
end
end
def yield(size,offset=0,&block)
if(size < 0) then size = 0 end
range = Range.new(0,@current-1).to_a
if(@wrap)
range = Range.new(@current,@size-1).to_a + range
end
range = range.last(size+offset)[0,size]
@before = (size+offset < @buff.length)
@after = (offset != 0 and size < @buff.length)
if(block)
range.each do |i|
yield [@buff[i],@buff_type[i]]
end
else
return range.collect{|r| [@buff[r],@buff_type[r]]}
end
end
def has_after?()
return @after
end
def has_before?()
return @before
end
def clear()
@current = 0
@buff = []
@buff_type = []
end
end
|