-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathdisplay.rb
65 lines (54 loc) · 1.31 KB
/
display.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
55
56
57
58
59
60
61
62
63
64
65
module RubyLS
class Display
def initialize(options)
@long_format = options[:long_format]
@include_hidden = options[:include_hidden]
@max_byte_count = options[:max_byte_count]
end
def render(path)
if Dir.exist?(path)
render_directory(path)
else
render_file(path)
end
end
private
def render_directory(dirname)
list_total_blocks(dirname) if @long_format
Dir.foreach(dirname) { |e| render_file(e) }
end
def list_total_blocks(dirname)
puts "total #{total_blocks(dirname)}"
end
def total_blocks(dirname)
entries(dirname).reduce(0) do |sum, e|
path = File.join(dirname, e)
sum + blocks_allocated(path)
end
end
def entries(dirname)
Dir.entries(dirname).select { |e| show?(e) }
end
def blocks_allocated(path)
File.stat(path).blocks
end
def render_file(filename)
if show?(filename)
puts formatted(filename)
end
end
def show?(filename)
@include_hidden || !hidden?(filename)
end
def hidden?(filename)
filename[0] == '.'
end
def formatted(filename)
if @long_format
File.open(filename) { |f| RubyLS::FileDetails.new(f, @max_byte_count) }
else
filename
end
end
end
end