-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf.rb
54 lines (45 loc) · 914 Bytes
/
f.rb
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
class IO
TAIL_BUF_LENGTH = 1 << 16
def tail(n, offset=0, filter='')
return [] if n < 1
seek -TAIL_BUF_LENGTH, SEEK_END
n = n + offset
buf = ""
if (!filter.empty?)
while buf.scan(filter).length <= n
t = read(TAIL_BUF_LENGTH)
buf = t + buf
seek 2 * -TAIL_BUF_LENGTH, SEEK_CUR
end
else
while buf.count("\n") <= n
t = read(TAIL_BUF_LENGTH)
buf = t + buf
seek 2 * -TAIL_BUF_LENGTH, SEEK_CUR
end
end
i = 0
o = 0
lines = []
buf.split(/\n|\r/).reverse!.each do |l|
if filter and !l.include? filter
next
end
if (o < offset)
o += 1
next
else
i += 1
lines.push(l)
end
if i == (n - offset)
break
end
end
lines
end
end
def get_filtered_text(filter = 'Notice')
t = `grep "\|#{filter}\:"`
p t
end