Class: Rfd::Item

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rfd/item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, dir: nil, name: nil, stat: nil, window_width: nil) ⇒ Item

Returns a new instance of Item.



8
9
10
11
# File 'lib/rfd/item.rb', line 8

def initialize(path: nil, dir: nil, name: nil, stat: nil, window_width: nil)
  @path, @dir, @name, @stat, @window_width, @marked = path, dir || File.dirname(path), name || File.basename(path), stat, window_width, false
  @stat = File.lstat self.path unless stat
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



5
6
7
# File 'lib/rfd/item.rb', line 5

def dir
  @dir
end

#indexObject

Returns the value of attribute index.



6
7
8
# File 'lib/rfd/item.rb', line 6

def index
  @index
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/rfd/item.rb', line 5

def name
  @name
end

#statObject (readonly)

Returns the value of attribute stat.



5
6
7
# File 'lib/rfd/item.rb', line 5

def stat
  @stat
end

Instance Method Details

#<=>(o) ⇒ Object



202
203
204
205
206
207
208
209
210
# File 'lib/rfd/item.rb', line 202

def <=>(o)
  if directory? && !o.directory?
    1
  elsif !directory? && o.directory?
    -1
  else
    name <=> o.name
  end
end

#atimeObject



70
71
72
# File 'lib/rfd/item.rb', line 70

def atime
  stat.atime.strftime('%Y-%m-%d %H:%M:%S')
end

#basenameObject



17
18
19
# File 'lib/rfd/item.rb', line 17

def basename
  @basename ||= File.basename name, extname
end

#colorObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rfd/item.rb', line 48

def color
  if symlink?
    Curses::COLOR_MAGENTA
  elsif hidden?
    Curses::COLOR_GREEN
  elsif directory?
    Curses::COLOR_CYAN
  elsif executable?
    Curses::COLOR_RED
  else
    Curses::COLOR_WHITE
  end
end

#ctimeObject



74
75
76
# File 'lib/rfd/item.rb', line 74

def ctime
  stat.ctime.strftime('%Y-%m-%d %H:%M:%S')
end

#current_markObject



169
170
171
# File 'lib/rfd/item.rb', line 169

def current_mark
  marked? ? '*' : ' '
end

#directory?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rfd/item.rb', line 102

def directory?
  @directory ||= if symlink?
    begin
      File.stat(path).directory?
    rescue Errno::ENOENT
      false
    end
  else
    stat.directory?
  end
end

#display_nameObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rfd/item.rb', line 35

def display_name
  @display_name ||= begin
    n = full_display_name
    if mb_size(n) <= @window_width - 15
      n
    elsif symlink?
      mb_left n, @window_width - 16
    else
      "#{mb_left(basename, @window_width - 16 - extname.size)}#{extname}"
    end
  end
end

#executable?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/rfd/item.rb', line 122

def executable?
  stat.executable?
end

#extnameObject



21
22
23
# File 'lib/rfd/item.rb', line 21

def extname
  @extname ||= File.extname name
end

#full_display_nameObject



29
30
31
32
33
# File 'lib/rfd/item.rb', line 29

def full_display_name
  n = @name.dup
  n << " -> #{target}" if symlink?
  n
end

#gz?Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rfd/item.rb', line 138

def gz?
  @gz_ ||= begin
    if directory?
      false
    else
      File.binread(realpath, 2).unpack('n').first == 0x1f8b
    end
  rescue
    false
  end
end

#hidden?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/rfd/item.rb', line 118

def hidden?
  name.start_with?('.') && (name != '.') && (name != '..')
end

#join(*ary) ⇒ Object



25
26
27
# File 'lib/rfd/item.rb', line 25

def join(*ary)
  File.join path, ary
end

#marked?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/rfd/item.rb', line 165

def marked?
  @marked
end

#mb_char_size(c) ⇒ Object



182
183
184
# File 'lib/rfd/item.rb', line 182

def mb_char_size(c)
  c == '' ? 1 : c.bytesize == 1 ? 1 : 2
end

#mb_left(str, size) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/rfd/item.rb', line 173

def mb_left(str, size)
  len = 0
  index = str.each_char.with_index do |c, i|
    break i if len + mb_char_size(c) > size
    len += mb_size c
  end
  str[0, index]
end

#mb_ljust(str, size) ⇒ Object



190
191
192
# File 'lib/rfd/item.rb', line 190

def mb_ljust(str, size)
  "#{str}#{' ' * [0, size - mb_size(str)].max}"
end

#mb_size(str) ⇒ Object



186
187
188
# File 'lib/rfd/item.rb', line 186

def mb_size(str)
  str.each_char.inject(0) {|l, c| l += mb_char_size(c)}
end

#modeObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rfd/item.rb', line 82

def mode
  @mode ||= begin
    m = stat.mode
    ft = directory? ? 'd' : symlink? ? 'l' : '-'
    ret = [(m & 0700) / 64, (m & 070) / 8, m & 07].inject(ft) do |str, s|
      str += "#{s & 4 == 4 ? 'r' : '-'}#{s & 2 == 2 ? 'w' : '-'}#{s & 1 == 1 ? 'x' : '-'}"
    end
    if m & 04000 != 0
      ret[3] = directory? ? 's' : 'S'
    end
    if m & 02000 != 0
      ret[6] = directory? ? 's' : 'S'
    end
    if m & 01000 == 512
      ret[-1] = directory? ? 't' : 'T'
    end
    ret
  end
end

#mtimeObject



78
79
80
# File 'lib/rfd/item.rb', line 78

def mtime
  stat.mtime.strftime('%Y-%m-%d %H:%M:%S')
end

#pathObject



13
14
15
# File 'lib/rfd/item.rb', line 13

def path
  @path ||= File.join @dir, @name
end

#realpathObject



154
155
156
# File 'lib/rfd/item.rb', line 154

def realpath
  @realpath ||= File.realpath path
end

#sizeObject



62
63
64
# File 'lib/rfd/item.rb', line 62

def size
  directory? ? 0 : stat.size
end

#size_or_dirObject



66
67
68
# File 'lib/rfd/item.rb', line 66

def size_or_dir
  directory? ? '<DIR>' : size.to_s
end

#symlink?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/rfd/item.rb', line 114

def symlink?
  stat.symlink?
end

#targetObject



150
151
152
# File 'lib/rfd/item.rb', line 150

def target
  File.readlink path if symlink?
end

#to_sObject



194
195
196
# File 'lib/rfd/item.rb', line 194

def to_s
  "#{current_mark}#{mb_ljust(display_name, @window_width - 15)}#{size_or_dir.rjust(13)}"
end

#to_strObject



198
199
200
# File 'lib/rfd/item.rb', line 198

def to_str
  path
end

#toggle_markObject



158
159
160
161
162
163
# File 'lib/rfd/item.rb', line 158

def toggle_mark
  unless %w(. ..).include? name
    @marked = !@marked
    true
  end
end

#zip?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rfd/item.rb', line 126

def zip?
  @zip_ ||= begin
    if directory?
      false
    else
      File.binread(realpath, 4).unpack('V').first == 0x04034b50
    end
  rescue
    false
  end
end