-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathfile_details.rb
69 lines (55 loc) · 1.19 KB
/
file_details.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
66
67
68
69
require 'etc'
module RubyLS
class FileDetails
FTYPES = {
'blockSpecial' => 'b',
'characterSpecial' => 'c',
'directory' => 'd',
'link' => 'l',
'socket' => 's',
'fifo' => 'P',
'file' => '-'
}
def initialize(file, max_byte_count = nil)
@file = file
@stat = File.stat(@file)
@permissions = RubyLS::Permissions.new(@stat)
@max_byte_count = max_byte_count
end
def to_s
"#{mode} #{links.to_s.rjust(2)} #{owner} #{group} #{bytes} #{mtime} #{path}"
end
def [](key)
send(key)
end
private
attr_reader :permissions
def mode
ftype = FTYPES[@stat.ftype]
"#{ftype}#{permissions}"
end
def links
@stat.nlink
end
def owner
Etc.getpwuid(@stat.uid).name
end
def group
Etc.getgrgid(@stat.gid).name
end
def bytes
col_width = (@max_byte_count || size).to_s.length + 1
size.to_s.rjust(col_width)
end
def size
@stat.size
end
def mtime
time = @stat.mtime
time.strftime('%b %e %H:%M')
end
def path
@file.path
end
end
end