Class: ColorLS::FileInfo

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/colorls/fileinfo.rb

Constant Summary collapse

@@users =

rubocop:disable Style/ClassVars

{}
@@groups =

rubocop:disable Style/ClassVars

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, parent:, path: nil, link_info: true) ⇒ FileInfo

Returns a new instance of FileInfo.



14
15
16
17
18
19
20
21
22
# File 'lib/colorls/fileinfo.rb', line 14

def initialize(name:, parent:, path: nil, link_info: true)
  @name = name
  @parent = parent
  @path = path.nil? ? File.join(parent, name) : path
  @stats = File.lstat(@path)
  @show_name = nil

  handle_symlink(@path) if link_info && @stats.symlink?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/colorls/fileinfo.rb', line 12

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



12
13
14
# File 'lib/colorls/fileinfo.rb', line 12

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/colorls/fileinfo.rb', line 12

def path
  @path
end

#statsObject (readonly)

Returns the value of attribute stats.



12
13
14
# File 'lib/colorls/fileinfo.rb', line 12

def stats
  @stats
end

Class Method Details

.dir_entry(dir, child, link_info: true) ⇒ Object



28
29
30
# File 'lib/colorls/fileinfo.rb', line 28

def self.dir_entry(dir, child, link_info: true)
  FileInfo.new(name: child, parent: dir, link_info: link_info)
end

.info(path, link_info: true) ⇒ Object



24
25
26
# File 'lib/colorls/fileinfo.rb', line 24

def self.info(path, link_info: true)
  FileInfo.new(name: File.basename(path), parent: File.dirname(path), path: path, link_info: link_info)
end

Instance Method Details

#dead?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/colorls/fileinfo.rb', line 39

def dead?
  @dead
end

#groupObject



52
53
54
55
56
57
58
59
# File 'lib/colorls/fileinfo.rb', line 52

def group
  return @@groups[@stats.gid] if @@groups.key? @stats.gid

  group = Etc.getgrgid(@stats.gid)
  @@groups[@stats.gid] = group.nil? ? @stats.gid.to_s : group.name
rescue ArgumentError
  @stats.gid.to_s
end

target of a symlink (only available for symlinks)



62
63
64
# File 'lib/colorls/fileinfo.rb', line 62

def link_target
  @target
end

#ownerObject



43
44
45
46
47
48
49
50
# File 'lib/colorls/fileinfo.rb', line 43

def owner
  return @@users[@stats.uid] if @@users.key? @stats.uid

  user = Etc.getpwuid(@stats.uid)
  @@users[@stats.uid] = user.nil? ? @stats.uid.to_s : user.name
rescue ArgumentError
  @stats.uid.to_s
end

#showObject



32
33
34
35
36
37
# File 'lib/colorls/fileinfo.rb', line 32

def show
  return @show_name unless @show_name.nil?

  @show_name = @name.encode(Encoding.find('filesystem'), Encoding.default_external,
                            invalid: :replace, undef: :replace)
end

#to_sObject



66
67
68
# File 'lib/colorls/fileinfo.rb', line 66

def to_s
  name
end