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
|
class Directory
def initialize(path)
@path = path
@pos = 0
@pointed_file = ''
@width = 0
refresh
end
attr_reader(:path, :files, :pos, :width, :infos)
def pos=(x)
@pos = x
@pointed_file = @files[x]
resize
end
def pointed_file=(x)
if @files.include?(x)
@pointed_file = x
@pos = @files.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
# log File.basename(fn) + @infos[ix]
sz = File.basename(fn).size + @infos[ix].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_infos()
@infos = []
@files.each do |fn|
if File.directory?(fn)
begin
sz = Dir.entries(fn).size - 2
rescue
sz = "?"
end
@infos << "#{sz}"
else
if File.size?(fn)
@infos << " #{File.size(fn).bytes 2}"
else
@infos << ""
end
end
end
end
def refresh()
glob = Dir.new(@path).to_a.sort!
if OPTIONS['hidden']
glob -= ['.', '..', 'lost+found']
else
glob.reject!{|x| x[0] == ?. or x == 'lost+found'}
end
if glob.empty?
glob = ['.']
end
glob.map!{|x| File.join(@path, x)}
dirs = glob.select{|x| File.directory?(x)}
@files = dirs + (glob - dirs)
get_file_infos
resize
if @pos >= @files.size
@pos = @files.size - 1
elsif @files.include?(@pointed_file)
@pos = @files.index(@pointed_file)
end
end
end
class File
MODES_HASH = {
'0' => '---',
'1' => '--x',
'2' => '-w-',
'3' => '-wx',
'4' => 'r--',
'5' => 'r-x',
'6' => 'rw-',
'7' => 'rwx'
}
def self.modestr(f)
unless exists?(f)
return '----------'
end
if symlink?(f)
result = 'l'
elsif directory?(f)
result = 'd'
else
result = '-'
end
s = ("%o" % File.stat(f).mode)[-3..-1]
for m in s.each_char
result << MODES_HASH[m]
end
result
end
end
class Numeric
def limit(max, min = 0)
self < min ? min : (self > max ? max : self)
end
def bytes n_round = 2
n = 1024
a = %w(B K M G T Y)
i = 0
flt = self.to_f
while flt > n and i < a.length - 1
flt /= n
i += 1
end
# flt = flt.round(n_round)
r = 10 ** n_round
flt *= r
flt = flt.round.to_f / r
int = flt.to_i
flt = int if int == flt
return flt.to_s + ' ' + a[i]
end
end
class Array
def wrap(n)
n.times { push shift }
end
end
class String
def clear
replace String.new
end
def sh
res = dup
res.gsub!('\\\\', "\000")
res.gsub!(' ', '\\ ')
res.gsub!('(', '\\(')
res.gsub!(')', '\\)')
res.gsub!('*', '\\*')
res.gsub!('\'', '\\\'')
res.gsub!('"', '\\"')
res.gsub!("\000", '\\\\')
return res
end
end
|