Class: AmazingPrint::Formatters::GetChildItem

Inherits:
Object
  • Object
show all
Defined in:
lib/amazing_print/formatters/mswin_helper.rb

Constant Summary collapse

FILE_ATTRIBUTE_ARCHIVE =

docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants

0x20
FILE_ATTRIBUTE_READONLY =
0x1
FILE_ATTRIBUTE_HIDDEN =
0x2
FILE_ATTRIBUTE_SYSTEM =
0x4

Instance Method Summary collapse

Constructor Details

#initialize(fname) ⇒ GetChildItem

Returns a new instance of GetChildItem.



15
16
17
18
19
# File 'lib/amazing_print/formatters/mswin_helper.rb', line 15

def initialize(fname)
  @fname = fname
  @stat = File.send(File.symlink?(@fname) ? :lstat : :stat, @fname)
  @attrs = Kernel32::GetFileAttributesA @fname
end

Instance Method Details

#last_write_timeObject



38
39
40
# File 'lib/amazing_print/formatters/mswin_helper.rb', line 38

def last_write_time
  @stat.mtime.strftime '%Y-%m-%d     %H:%M'
end

#lengthObject



42
43
44
# File 'lib/amazing_print/formatters/mswin_helper.rb', line 42

def length
  @stat.file? ? @stat.size.to_s : ''
end

#modeObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/amazing_print/formatters/mswin_helper.rb', line 27

def mode
  r = ['-'] * 6
  r[0] = 'd' if @stat.directory?
  r[1] = 'a' unless (@attrs & FILE_ATTRIBUTE_ARCHIVE).zero?
  r[2] = 'r' unless (@attrs & FILE_ATTRIBUTE_READONLY).zero?
  r[3] = 'h' unless (@attrs & FILE_ATTRIBUTE_HIDDEN).zero?
  r[4] = 's' unless (@attrs & FILE_ATTRIBUTE_SYSTEM).zero?
  r[5] = 'l' if File.symlink? @fname
  r.join
end

#nameObject



46
47
48
# File 'lib/amazing_print/formatters/mswin_helper.rb', line 46

def name
  @fname
end

#to_sObject



50
51
52
53
54
55
56
57
58
# File 'lib/amazing_print/formatters/mswin_helper.rb', line 50

def to_s
  format '%-12<Mode>s %<LastWriteTime>s %14<Length>s %<Name>s',
         {
           Mode: mode,
           LastWriteTime: last_write_time,
           Length: length,
           Name: name
         }
end