-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheadless
executable file
·62 lines (52 loc) · 978 Bytes
/
headless
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
#!/bin/env ruby
def usage
puts "Usage: headless -nNUM file1 file2 ...\nSomething like tail -n +NUM"
exit
end
def ARGV.n
return @n if @n
input = []
until empty?
arg = shift
if m = /-n(\d*)/.match(arg)
@n = (m[1].empty? ? shift : m[1])
else
input << arg
end
end
input.each{|inp| push inp}
usage unless /^\d+$/.match @n
@n = @n.to_i
end
class << ARGF
def name
f = self.file
f.is_a?(File) ? f.path : 'stdin'
end
def another?
@current ||= ARGF.file
bool = @current != ARGF.file
@current = ARGF.file
bool
end
end
output = Hash.new do |h, k|
h[k] = []
end
def output.flush
return values if keys.count == 1
map do |file_name, lines|
"==> #{file_name} <==\n#{lines.join}"
end.join("\n")
end
# main
ignore = ARGV.n
ARGF.each_line do |line|
ignore = ARGV.n if ARGF.another?
if ignore == 0
output[ARGF.name] << line
else
ignore = ignore.pred
end
end
puts output.flush