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