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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
require 'code/extensions/basic.rb'
class Directory
BAD_TIME = Time.at(0)
MOVIE_EXTENSIONS = %w(avi mpg mpeg mp4 mp5 ogv ogm wmv mkv flv fid vob div divx)
class Entry #{{{
# Let's just cache every shit, because i don't want
# to call File methods all the time
def initialize(dirname, basename=nil)
if basename
@path = File.join(dirname, basename)
@dirname = dirname
@basename = basename
else
@path = dirname
@dirname = File.dirname(dirname)
@basename = File.basename(dirname)
end
@name, @ext = @basename.split_at_last_dot
# @ext = @basename.from_last('.') || ''
@movie = MOVIE_EXTENSIONS.include?(@ext)
@size = 0
@exists = false
@rights = '----------'
@readlink = ''
@symlink = false
@writable = false
@infostring = ''
@executable = false
@type = :nonexistent
@mtime = BAD_TIME
@ctime = BAD_TIME
@marked = false
end
attr_reader *%w(
basename mtime rights type path ext
infostring readlink basename size ctime name
)
attr_accessor(:marked)
def to_s() @path end
def exists?() @exists end
def marked?() @marked end
def symlink?() @symlink end
def movie?() @movie end
def broken_symlink?() @symlink and !@exists end
def dir?() @type == :dir end
def file?() @type == :file end
def writable?() @writable end
def executable?() @executable end
def mimetype()
if @type == :dir
nil
else
Fm::MIMETYPES[@ext]
end
end
def delete!
if @type == :dir
Dir.delete(@path) rescue nil
else
File.delete(@path) rescue nil
end
end
def refresh
if File.exists?(@path)
if File.ctime(@path) != @ctime
get_data
end
else
get_data
end
end
def sh
@path.sh
end
def in? path
to_s[0, path.size] == path
end
def get_data
@size = 0
@infostring = ''
@exists = File.exists?(@path)
if @exists
@writable = File.writable?(@path)
@symlink = File.symlink?(@path)
if @symlink
@readlink = File.readlink(@path)
end
if File.directory?(@path)
@type = :dir
begin
sz = Dir.entries(@path).size - 2
@size = sz
rescue
sz = "?"
end
@infostring << "#{sz}"
elsif File.socket?(@path)
@type = :socket
else
@type = :file
@size = File.size(@path)
if File.size?(@path)
@infostring << " #{File.size(@path).bytes 2}"
else
@infostring << ""
end
end
@rights = File.modestr(@path)
@executable = File.executable?(@path)
@mtime = File.mtime(@path)
@ctime = File.ctime(@path)
else
if File.symlink?(@path)
@readlink = File.readlink(@path)
@infostring = '->'
@symlink = true
else
@symlink = false
end
@executable = false
@writable = false
@type = :nonexistent
@rights = '----------'
@mtime = BAD_TIME
@ctime = BAD_TIME
end
end
end #}}}
PLACEHOLDER = Entry.new('/', 'placeholder')
def initialize(path, allow_delay=false)
@path = path
@pos = 0
@files = [PLACEHOLDER]
@file_size = 0
@pointed_file = nil
@width = 1000
@read = false
@empty = true
@scheduled = false
refresh
end
def read_dir
@mtime = File.mtime(@path)
@files = Dir.new(@path).to_a
if OPTIONS['hidden']
@files -= ['.', '..', 'lost+found']
else
@files.reject!{|x| x[0] == ?. or x == 'lost+found'}
end
if @files.empty?
@files = ['.']
end
@files_raw = @files.map{|bn| File.join(@path, bn)}
@files.map!{|basename| Entry.new(@path, basename)}
end
attr_reader(:path, :files, :pos, :width, :files_raw,
:file_size, :read)
attr_accessor(:scheduled)
def scheduled?() @scheduled end
def read?() @read end
def pos=(x)
# if @files.size <= 1 or x < 0
# x = 0
# elsif x > @files.size
# x = @files.size - 1
# end
@pos = x
make_sure_cursor_is_in_range()
@pointed_file = @files[x]
resize
end
def recheck_stuff()
# log "pointed file: #@pointed_file"
# log @files_raw
# log ""
if test = @files_raw.index(@pointed_file)
# log("if")
@pos = test
else
# log("else")
make_sure_cursor_is_in_range()
end
end
def make_sure_cursor_is_in_range()
if @files.size <= 1 or @pos < 0
@pos = 0
elsif @pos > @files.size
@pos = @files.size - 1
end
end
def find_file(x)
x = File.basename(x)
files.each_with_index do |file, i|
if file.basename == x
self.pos = i
end
end
end
def empty?()
Dir.entries(@path).size <= 2
end
def restore()
for f in @files
f.marked = false
end
end
def pointed_file=(x)
if @files_raw.include?(x)
@pointed_file = x
@pos = @files_raw.index(x)
else
self.pos = 0
end
resize
end
def size() @files.size end
def resize()
pos = Fm.get_offset(self, lines)
if @files.empty?
@width = 0
else
@width = 0
@files[pos, lines-2].each_with_index do |fn, ix|
ix += pos
sz = fn.basename.size + fn.infostring.size + 2
@width = sz if @width < sz
end
# @width = @files[pos,lines-2].map{|x| File.basename(x).size}.max
end
end
def get_file_info()
@file_size = 0
@files.each do |f|
f.refresh
@file_size += f.size if f.file?
end
@read = true
end
# def refresh()
# @files = Dir.new(@path).to_a
# if OPTIONS['hidden']
# @files -= ['.', '..', 'lost+found']
# else
# @files.reject!{|x| x[0] == ?. or x == 'lost+found'}
# end
# if @files.empty?
# @files = ['.']
# end
# @files.map!{|basename| Entry.new(@path, basename)}
#
# if @pos >= @files.size
# @pos = @files.size - 1
# elsif @files.include?(@pointed_file)
# @pos = @files.index(@pointed_file)
# end
# end
def refresh(info=false)
if File.mtime(@path) != @mtime
read_dir
end
if info
log("getting file info of #{@path}")
get_file_info
end
sort
end
def schedule()
@scheduled = true
Fm.schedule(self)
end
def refresh!()
oldfile = @pointed_file
read_dir
get_file_info
sort
if @files.include? oldfile
self.pointed_file = oldfile
end
end
def sort_sub(x, y)
case OPTIONS['sort']
when :name
x.basename <=> y.basename
when :ext
x.ext <=> y.ext
when :type
x.ext.filetype <=> y.ext.filetype
when :size
x.size <=> y.size
when :ctime
x.ctime <=> y.ctime
when :mtime
x.mtime <=> y.mtime
else
x.basename <=> y.basename
end
end
def sort()
@files = @files.sort {|x,y|
if OPTIONS['dir_first']
if x.dir?
if y.dir? then sort_sub(x, y) else -1 end
else
if y.dir? then 1 else sort_sub(x, y) end
end
else
sort_sub(x, y)
end
}
@files.reverse! if OPTIONS['sort_reverse']
@files_raw = @files.map{|x| x.to_s}
end
end
|