rbfind 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE +34 -0
  3. data/Rakefile +18 -0
  4. data/bin/rbfind +474 -0
  5. data/lib/humansiz.rb +129 -0
  6. data/lib/rbfind.rb +895 -0
  7. metadata +65 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWM0NTUwYWZjYTk3NGNmNDQ4ZmZmZDA5Zjk4MDI1ZWVlMWYxOTliNA==
5
+ data.tar.gz: !binary |-
6
+ OGFjM2UxYjQwNjFmMmI2ZGI2ZDkyNDE0MDJlOTBmNzA2ZTJlMTdhYw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ODdhNDUxNjRiYjlhYTE1ZDFlYzA2ODRiNDJmNGIwZDhkNTE5MDU2ZDEzZGVm
10
+ ZWNhZTJmODI0ZDczZGE3MDEyOTUyZDY5YWJmNGYxZGY5NTQ4YzA4MmY4ZGY2
11
+ MTczYWYyODYwMzBjMTE3N2JhNTRjYWZhZWExYzY1OWM0Yzc0NmU=
12
+ data.tar.gz: !binary |-
13
+ NGQ0OTg5N2UyYmQ5ODlmMmNiYTJiMjc5ZmNkMjRjNjFmYjVmNjYxZjU4MWY1
14
+ MTJkMTk2OTQxYmRmMmNjOTAyZWEyYTkyYzBjMTRjNWE4Mzg0NmE3ZWQ2N2Zm
15
+ MmRhODE1OTQ3ODAzYzQ2ODRhZjZlMmJiZmU5NzM2Y2UxNzQwODk=
data/LICENSE ADDED
@@ -0,0 +1,34 @@
1
+ ____ _ _____ _ _
2
+ | _ \| |__ | ___(_)_ __ __| |
3
+ | |_) | '_ \| |_ | | '_ \ / _` |
4
+ | _ <| |_) | _| | | | | | (_| |
5
+ |_| \_\_.__/|_| |_|_| |_|\__,_|
6
+
7
+
8
+ Copyright (c) 2008-2013, Bertram Scharpf <software@bertram-scharpf.de>.
9
+ All rights reserved.
10
+
11
+ Redistribution and use in source and binary forms, with or without
12
+ modification, are permitted provided that the following conditions are
13
+ met:
14
+
15
+ * Redistributions of source code must retain the above copyright
16
+ notice, this list of conditions and the following disclaimer.
17
+
18
+ * Redistributions in binary form must reproduce the above copyright
19
+ notice, this list of conditions and the following disclaimer in
20
+ the documentation and/or other materials provided with the
21
+ distribution.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
+ TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
+ OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ #
2
+ # Rakefile -- build RbFind project
3
+ #
4
+
5
+ task "README" do |t|
6
+ File.open t.name, "w" do |readme|
7
+ readme.puts `ruby -I lib bin/rbfind --version`
8
+ readme.puts
9
+ readme.puts `ruby -I lib bin/rbfind --examples`
10
+ end
11
+ end
12
+
13
+ task :default => "README"
14
+
15
+ task :clean do |t|
16
+ sh "rm", "-fv", "README"
17
+ end
18
+
data/bin/rbfind ADDED
@@ -0,0 +1,474 @@
1
+ #!/usr/bin/ruby
2
+
3
+ #
4
+ # rbfind -- Ruby Find with many features
5
+ #
6
+
7
+ require "rbfind"
8
+
9
+ PROGRAM = <<EOT
10
+ rbfind #{RbFind::VERSION} -- A find tool using Ruby
11
+
12
+ Author: Bertram Scharpf <software@bertram-scharpf.de>
13
+ License: BSD
14
+ EOT
15
+
16
+
17
+ require 'getoptlong'
18
+
19
+ OPTIONS = [
20
+ # options arg description
21
+ [ %w(--help -h), nil, "print this help"],
22
+ [ %w(--examples -X), nil, "print this help and examples"],
23
+ [ %w(--version -V), nil, "print version information"],
24
+ [ %w(--verbose -v), nil, "standard Ruby error reports"],
25
+ [ %w(--show -o), nil, "just show Ruby block built from options"],
26
+ [ %w(--bw -w), nil, "black and white on -p or default output"],
27
+ [ %w(--colored -c), nil, "force color on -p or default output"],
28
+ [ %w(--depth -d), nil, "yield directory after its contents"],
29
+ [ %w(--maxdepth -m), :num, "maxium step depth"],
30
+ [ %w(--follow -y), nil, "follow symbolic links"],
31
+ [ %w(--sort -s), :str, "=reverse/desc/-1,unsorted; default is sorted"],
32
+ [ %w(--sort-by -S), :str, "sort expression ('name' is name)"],
33
+ [ %w(--require -r), :rb, "require library"],
34
+ [ %w(--fileutils -U), nil, "include FileUtils module"],
35
+ [ %w(--puts-path -p), nil, "do 'puts path/cpath' on true block"],
36
+ [ %w(--ls-l -P), nil, "do 'ls -l' style output on true block"],
37
+ [ %w(--wider -+), nil, "widen fields in long output format (--ls-l)"],
38
+ [ %w(--lines -l), :blk, "surround block by 'lines { |$_,$.| ... }'"],
39
+ [ %w(--reallines -L), :blk, "same as -l but stop at any null character"],
40
+ [ %w(--grep -g), :blk, "grep files (implies -pL)"],
41
+ [ %w(--igrep -G), :blk, "case insensitive grep"],
42
+ [ %w(--binary -b), nil, "grep even binary files"],
43
+ [ %w(--no-vcs -C), nil, "prune version control dirs (CVS/.svn/.git)"],
44
+ [ %w(--no-swap -W), nil, "ignore Vim swapfiles"],
45
+ [ %w(--skip -k), :lst, "filenames to skip"],
46
+ [ %w(--demand -D), :lst, "skip all filenames but these"],
47
+ [ %w(--ext -e), :lst, "skip all filename extensions but these"],
48
+ [ %w(--visible -I), nil, "skip all hidden (starting with .dot)"],
49
+ [ %w(--begin -B), :blk, "eval block before begin"],
50
+ [ %w(--end -E), :blk, "eval block after end"],
51
+ [ %w(--file -f), :blk, "read block expression from file"],
52
+ [ %w(--encoding -K), :str, "encoding extern[:intern] (same as ruby -E)"],
53
+ ]
54
+
55
+ def usage
56
+ puts <<EOT
57
+ Usage:
58
+
59
+ rbfind [options] path... 'block'
60
+
61
+ EOT
62
+ OPTIONS.each { |(long,short),arg,desc|
63
+ puts " %-12s %2s %-5s %s" % [long,short,arg.to_s.upcase,desc]
64
+ }
65
+ puts <<'EOT'
66
+
67
+ "--" stops option processing.
68
+
69
+ The last argument is a block that will be executed on every found
70
+ file object. It may be omitted; then "puts path" is assumed or "cpath"
71
+ in case the output is a terminal.
72
+
73
+ Default sort order is alphabetical.
74
+
75
+ EOT
76
+ end
77
+
78
+ def usage_examples
79
+ usage
80
+ puts <<'EOT'
81
+ Examples:
82
+ $ rbfind -p 'file and age < 5.m'
83
+ $ rbfind -p 'file and age > 1.d'
84
+ $ rbfind -p '(90.d .. 183.d) === age'
85
+
86
+ $ rbfind -Cg require
87
+ $ rbfind -Cp
88
+ $ rbfind 'filesize < 100.kB and grep /require/'
89
+ $ rbfind -g 'define\s*ALLOC_N'
90
+ $ rbfind myproject -e rb -l '~/require/ and puts $_'
91
+
92
+ $ rbfind -pe '.rb c h'
93
+ $ rbfind /usr/include -e .h -g EACCES
94
+ $ rbfind -d -m3 -- /mnt/data
95
+ $ rbfind 'filesize > 10.MiB and colsep size.to_hib, path'
96
+
97
+ $ rbfind -S "name =~ /^\.*/ ; $'.downcase"
98
+ $ rbfind -B '$s=0' -E 'puts $s.to_h' 'filesize {|s|$s+=s}'
99
+ $ rbfind 'puts path if ext == ".rb"'
100
+ $ rbfind -p 'ext == ".rb"'
101
+ $ rbfind /usr/include -p -D '\.h$' -l '~/EACCES/'
102
+ $ rbfind /usr/include -pD 'pg|pq|postgres' dir
103
+ $ rbfind 'no_svn ; puts path'
104
+ $ rbfind 'prune if name == ".svn" ; puts path'
105
+ $ rbfind -p 'prune if name == ".svn"'
106
+ $ rbfind myproject 'name[/\.rb$/] and lines { |l,i| l=~/require/ and puts l }'
107
+ # rbfind /etc 'lines { |l,| l["192.168."] and (puts path ; break) }'
108
+ $ rbfind /etc 'readable? or raise "Access denied: #{path}" ;
109
+ > lines { |l,| l["192.168."] and (puts path ; break) }'
110
+
111
+ $ rbfind 'col_sep stype+modes, size[10], path'
112
+ $ rbfind 'tab_sep stype+modes, size[10], path'
113
+ $ rbfind 'spc_sep stype+modes, size.to_h, user, group, mtime.lsish, path'
114
+ $ rbfind 'spacesep stype+modes, size.to_h, user, group, mtime.lsish, path'
115
+ $ rbfind 'spacesep stype+modes, size.to_h, user, group, mtime!, path'
116
+ $ rbfind 'spacesep stype+modes, size.to_h, user, group, mtime.i, path'
117
+ $ rbfind 'spacesep mtime.u, path'
118
+ $ rbfind 'spacesep modes, user|8, group|8, size[8], cpath + carrow.to_s'
119
+
120
+ $ rbfind 'spacesep digest_md5, size[8], path'
121
+ $ rbfind 'spacesep digest_sha256, size[8], path'
122
+
123
+ $ rbfind 'rename name.downcase'
124
+ $ rbfind 'ext == ".tgz" and rename without_ext+".tar.gz"'
125
+
126
+ See the RbFind class documentation or the "rbfind.rb" source file for more
127
+ sophisticated examples and for a full list of file examination methods.
128
+
129
+ Valid units are 1.s, 1.m, 1.h, 1.d, 1.w for time values and
130
+ 1.kB == 1000, 1.kiB == 1024, 1.MB, 1.MiB for file sizes.
131
+ EOT
132
+ end
133
+
134
+
135
+ require "humansiz"
136
+ require "English"
137
+
138
+
139
+ class Time
140
+
141
+ # autoload several formating methods
142
+ def method_missing sym, *args, &block
143
+ if require "time" then
144
+ send sym, *args, &block
145
+ else
146
+ super
147
+ end
148
+ end
149
+
150
+ # several common schemes
151
+ def i ; iso8601 ; end
152
+ def x ; xmlschema ; end
153
+ def r ; rfc822 ; end
154
+ def u ; utc.strftime "%Y-%m-%d %H:%M:%S" ; end
155
+
156
+ end
157
+
158
+
159
+ class Integer
160
+ # convenient formatting, right justification
161
+ def method_missing sym, *args
162
+ case sym.to_s # .to_s for Ruby 1.8
163
+ when /\Aw_?(\d+)/ then "%#{$1}d" % self
164
+ else super
165
+ end
166
+ end
167
+ end
168
+
169
+ class String
170
+ # convenient formatting, left justification
171
+ def method_missing sym, *args
172
+ case sym.to_s # .to_s for Ruby 1.8
173
+ when /\Aw_?(\d+)/ then ljust $1.to_i
174
+ else super
175
+ end
176
+ end
177
+ def | width
178
+ ljust width
179
+ end
180
+ # conventient date/time construction
181
+ def time
182
+ Time.parse self
183
+ end
184
+ end
185
+
186
+ class NilClass
187
+ # This makes it very easy to compare the return value of `filesize'.
188
+ def < oth ; false ; end
189
+ def <= oth ; false ; end
190
+ def > oth ; false ; end
191
+ def >= oth ; false ; end
192
+ def between? min, max ; false ; end
193
+
194
+ def [] width ; " "*width ; end
195
+ def | width ; " "*width ; end
196
+ end
197
+
198
+
199
+ def set_encoding extern, intern = nil
200
+ Encoding.default_external = extern if extern and not extern.empty?
201
+ Encoding.default_internal = intern if intern and not intern.empty?
202
+ [ $stdin, $stdout, $stderr].each do |io|
203
+ io.set_encoding extern, intern
204
+ end
205
+ end
206
+
207
+
208
+ class RbFindX < RbFind
209
+
210
+ # Alias the question marks away from all method names because
211
+ # they're a nuisance on the command line.
212
+ alias hidden hidden?
213
+ alias visible visible?
214
+ alias broken_link broken_link?
215
+ alias dir dir?
216
+ alias binary binary?
217
+ alias bin bin?
218
+ alias vimswap vimswap?
219
+
220
+ def blockdev ; blockdev? ; end
221
+ def chardev ; chardev? ; end
222
+ def directory ; directory? ; end
223
+ def executable ; executable? ; end
224
+ def executable_real ; executable_real? ; end
225
+ def file ; file? ; end
226
+ def grpowned ; grpowned? ; end
227
+ def owned ; owned? ; end
228
+ def pipe ; pipe? ; end
229
+ def readable ; readable? ; end
230
+ def readable_real ; readable_real? ; end
231
+ def setgid ; setgid? ; end
232
+ def setuid ; setuid? ; end
233
+ def socket ; socket? ; end
234
+ def sticky ; sticky? ; end
235
+ def symlink ; symlink? ; end
236
+ def writable ; writable? ; end
237
+ def writable_real ; writable_real? ; end
238
+ def zero ; zero? ; end
239
+
240
+ # convenient digest and time format methods
241
+ def method_missing sym, *args, &block
242
+ case sym.to_s
243
+ when /\Adigest_(.*)/, /\A([a-z]+[0-9]+)\z/ then
244
+ m = $1
245
+ c = m.upcase
246
+ begin
247
+ (Digest.const_get c).hexdigest read
248
+ rescue NameError
249
+ if m =~ /sha\d\d\d/ then m = "sha2" end
250
+ require "digest/#{m}" and retry
251
+ raise
252
+ end
253
+ when /\A([amc]time)!\z/ then
254
+ (send $1).strftime "%Y-%m-%d %H:%M:%S %z"
255
+ else
256
+ super
257
+ end
258
+ end
259
+
260
+ end
261
+
262
+
263
+ @params = {}
264
+
265
+ opts = GetoptLong.new *OPTIONS.map { |(long,short),arg,|
266
+ [long,short,(arg ? GetoptLong::REQUIRED_ARGUMENT : GetoptLong::NO_ARGUMENT)]
267
+ }
268
+ opts.ordering = GetoptLong::PERMUTE
269
+ opts.quiet = true
270
+ begin
271
+ opts.each do |opt,arg|
272
+ case opt
273
+ when '--help' then usage ; exit
274
+ when '--version' then puts PROGRAM ; exit
275
+ when '--examples' then usage_examples ; exit
276
+ when '--verbose' then @verbose = true
277
+ when '--show' then @show = true
278
+ when '--bw' then @color = false
279
+ when '--colored' then @color = true
280
+ when '--depth' then @params[ :depth] = true
281
+ when '--maxdepth' then @params[ :max_depth] = arg
282
+ when '--follow' then @params[ :follow] = true
283
+ when '--sort' then @params[ :sort] = arg
284
+ when '--sort-by' then
285
+ @params[ :sort] = instance_eval "proc { |name| #{arg} }"
286
+ when '--require' then require arg
287
+ when '--fileutils' then require "fileutils" ; include FileUtils
288
+ when '--puts-path' then @puts = true
289
+ when '--ls-l' then @puts = true ; @wd = 6
290
+ when '--wider' then @wd = @wd ? @wd.succ : 4
291
+ when '--lines' then @lines = :plain ; @block = arg
292
+ when '--reallines' then @lines = :plain ; @real = true ; @block = arg
293
+ when '--grep' then @lines = :grep ; @block = arg
294
+ when '--igrep' then @lines = :grep ; @icase = true ; @block = arg
295
+ when '--binary' then @binary = true
296
+ when '--no-vcs' then @vcs = true
297
+ when '--no-swap' then @vim = true
298
+ when '--skip' then @skip = arg
299
+ when '--demand' then @demand = arg
300
+ when '--ext' then @ext = arg
301
+ when '--visible' then @visible = true
302
+ when '--begin' then @blkbegin = arg
303
+ when '--end' then @blkend = arg
304
+ when '--file' then @block = File.read arg
305
+ when '--encoding' then e, i = arg.split ":" ; set_encoding e, i
306
+ end
307
+ end
308
+ rescue GetoptLong::InvalidOption
309
+ $stderr.puts $!
310
+ $stderr.puts
311
+ usage
312
+ exit 9
313
+ rescue
314
+ $stderr.puts $!
315
+ exit 8
316
+ end
317
+
318
+ def color_on stream
319
+ unless @color.nil? then
320
+ @color
321
+ else
322
+ !File::ALT_SEPARATOR && stream.tty?
323
+ end
324
+ end
325
+
326
+ if color_on $stderr then
327
+ def show_error e
328
+ $stderr.puts "\e[31m#{e.class}: \e[1m#{e}\e[m"
329
+ end
330
+ else
331
+ def show_error e
332
+ $stderr.puts "#{e.class}: #{e}"
333
+ end
334
+ end
335
+
336
+ def error_handle
337
+ if @verbose then
338
+ yield
339
+ else
340
+ begin
341
+ yield
342
+ rescue ArgumentError
343
+ show_error $!
344
+ if $!.message[ "invalid byte sequence"] then
345
+ $stderr.puts <<-EOT
346
+ Hint: Try to give me a different default encoding by explicitly giving one or
347
+ by changing the locale.
348
+
349
+ $ rbfind -K ascii-8bit ...
350
+ $ LC_ALL="C" rbfind ...
351
+
352
+ EOT
353
+ end
354
+ exit 1
355
+ rescue
356
+ show_error $!
357
+ exit 1
358
+ rescue NoMemoryError, ScriptError, SignalException
359
+ show_error $!
360
+ exit 2
361
+ end
362
+ end
363
+ end
364
+
365
+
366
+ error_handle do
367
+
368
+ co = color_on $stdout
369
+ opath = co ? "cpath" : "path"
370
+ case @lines
371
+ when :plain then
372
+ @puts and @block = "( #@block ) and colsep #{opath}, $., $_"
373
+ @real and @block = "break if ~/\0/o ; #@block"
374
+ @block = "lines { |line,num| $_, $. = line, num ; #@block }"
375
+
376
+ when :grep then
377
+ RE = Regexp.new @block, @icase && Regexp::IGNORECASE
378
+ @block = "grep RE"
379
+ @block << ", true" if co
380
+ @binary or @block = "not binary? and #@block"
381
+
382
+ else
383
+ @block ||= if $*.last and not (File.exists? $*.last) then
384
+ $*.pop.dup
385
+ else
386
+ @puts ||= true
387
+ "true"
388
+ end
389
+ if @puts then
390
+ @block = "( #@block ) and "
391
+ @block << if @wd then
392
+ "spcsep stype+modes, user|#@wd, group|#@wd, size.w#@wd, " +
393
+ "mtime.lsish, #{opath} + #{co ? 'carrow' : 'arrow'}.to_s"
394
+ else
395
+ "puts #{opath}"
396
+ end
397
+ end
398
+
399
+ end
400
+
401
+ if @skip then
402
+ RE_SKIP = Regexp.new @skip, File::ALT_SEPARATOR&&Regexp::IGNORECASE
403
+ @block.insert 0, "next if name =~ RE_SKIP ; "
404
+ end
405
+
406
+ if @demand then
407
+ RE_DEMAND = Regexp.new @demand, File::ALT_SEPARATOR&&Regexp::IGNORECASE
408
+ @block.insert 0, "next unless name =~ RE_DEMAND ; "
409
+ end
410
+
411
+ if @ext then
412
+ RE_EXT = Regexp.new '\A\.(' +
413
+ @ext.split(/ +|,/).map { |e| e[ /\A\.?(.*)\z/, 1] }.join('|') + ')\z',
414
+ File::ALT_SEPARATOR&&Regexp::IGNORECASE
415
+ @block.insert 0, "ext =~ RE_EXT or next ; "
416
+ end
417
+
418
+ @visible and @block.insert 0, "visible? or prune ; "
419
+
420
+ @vcs and @block.insert 0, "no_vcs ; "
421
+ @vim and @block.insert 0, "vimswap? and next ; "
422
+
423
+ args = $*.dup
424
+ if args.empty? and not $stdin.tty? then
425
+ $stdin.each_line { |l|
426
+ l.chomp!
427
+ args.push l
428
+ }
429
+ end
430
+
431
+ if @show then
432
+ class Proc
433
+ def to_s
434
+ "#<%s:0x%08x>" % [ self.class, object_id]
435
+ end
436
+ end
437
+ INDENT = " "*2
438
+ def puts_val *args
439
+ l = [ INDENT, *args].join ""
440
+ puts l
441
+ end
442
+ puts "arguments:"
443
+ args.each { |a| puts_val a }.any? or puts_val "(none)"
444
+ puts "parameters:"
445
+ @params.each { |k,v| puts_val k, "=", v }.any? or puts_val "(none)"
446
+ puts "block:"
447
+ self.class.constants.grep( /^RE(_[A-Z]+)?$/).each { |re|
448
+ v = self.class.const_get re
449
+ puts_val re, "=", v
450
+ }
451
+ puts_val @block
452
+ exit
453
+ end
454
+
455
+ @params[ :error] = proc do
456
+ case $!
457
+ when Errno::EPIPE then raise
458
+ when NameError, TypeError then raise
459
+ when ArgumentError then raise
460
+ else show_error $!
461
+ end
462
+ end
463
+
464
+ @block = <<-EOT
465
+ handle_error do
466
+ #@block
467
+ end
468
+ EOT
469
+
470
+ eval @blkbegin if @blkbegin
471
+ RbFindX.open args, @params do |f| f.instance_eval @block end
472
+ eval @blkend if @blkend
473
+ end
474
+
data/lib/humansiz.rb ADDED
@@ -0,0 +1,129 @@
1
+ #
2
+ # humansiz.rb -- human readable sizes
3
+ #
4
+ # Numeric class extensions for human readable sizes.
5
+
6
+ =begin rdoc
7
+
8
+ Human readable sizes, times, and modes.
9
+
10
+ Examples:
11
+
12
+ 4096.to_h # => " 4.1kB"
13
+ 4096.to_hib # => " 4.0kiB"
14
+ 1.MB # => 1000000
15
+ 1.MiB # => 1048576
16
+ 1.5.kiB # => 1536.0
17
+
18
+ 1.h # => 3600
19
+ 1.w # => 604800
20
+
21
+ =end
22
+
23
+ class Numeric # sizes in bytes
24
+
25
+ # :stopdoc:
26
+ K = 1000
27
+ M = K * K
28
+ G = K * M
29
+ T = K * G
30
+ # :startdoc:
31
+
32
+ def kB ; self * K ; end
33
+ def MB ; self * M ; end
34
+ def GB ; self * G ; end
35
+ def TB ; self * T ; end
36
+
37
+
38
+ # :stopdoc:
39
+ Kb = 1024
40
+ Mb = Kb * Kb
41
+ Gb = Kb * Mb
42
+ Tb = Kb * Gb
43
+ # :startdoc:
44
+
45
+ def kiB ; self * Kb ; end
46
+ def MiB ; self * Mb ; end
47
+ def GiB ; self * Gb ; end
48
+ def TiB ; self * Tb ; end
49
+
50
+ PREFIXES = " kMGTPEZY".scan /./ # :nodoc:
51
+
52
+ # :call-seq:
53
+ # num.to_h() -> str
54
+ #
55
+ # To human readable with decimal prefixes.
56
+ #
57
+ # 4096.to_h #=> " 4.1kB"
58
+ #
59
+ def to_h
60
+ n = 0
61
+ s = to_f
62
+ while s >= K do s /= K ; n += 1 end
63
+ format = n.zero? ? "%3d " : "%5.1f"
64
+ (format % s) + (PREFIXES[ n]||"?") + "B"
65
+ end
66
+
67
+ # :call-seq:
68
+ # num.to_hib() -> str
69
+ #
70
+ # To human readable with binary prefixes.
71
+ #
72
+ # 4096.to_hib #=> " 4.0kiB"
73
+ #
74
+ def to_hib
75
+ n = 0
76
+ s = to_f
77
+ while s >= Kb do s /= Kb ; n += 1 end
78
+ format = n.zero? ? "%4d " : "%6.1f"
79
+ (format % s) + (PREFIXES[ n]||"?") + "iB"
80
+ end
81
+
82
+ end
83
+
84
+
85
+ class Numeric # time values
86
+ def s ; self ; end
87
+ def m ; s * 60 ; end
88
+ def h ; m * 60 ; end
89
+ def d ; h * 24 ; end
90
+ def w ; d * 7 ; end
91
+ end
92
+
93
+
94
+
95
+ class Time
96
+
97
+ # :call-seq:
98
+ # time.lsish() -> str
99
+ #
100
+ # Build a time string like in <code>ls -l</code>. When the year is
101
+ # the current, show the time. While <code>ls</code> doesn't show
102
+ # the seconds, this will allways include them.
103
+ #
104
+ # Time.now.lsish #=> " 8. Oct 15:15:19"
105
+ # file.stat.mtime.lsish #=> " 1. Apr 2008 "
106
+ #
107
+ def lsish
108
+ strftime "#{PERC_DAY}. %b " +
109
+ (year == Time.now.year ? "%H:%M:%S" : "%Y ")
110
+ end
111
+
112
+ # Windows has no "%e".
113
+ PERC_DAY = Time.now.strftime("%e") =~ /\d/ ? "%e" : "%d" # :nodoc:
114
+
115
+ end
116
+
117
+
118
+ class Integer
119
+ def hex
120
+ to_s 0x10
121
+ end
122
+ def oct
123
+ to_s 010
124
+ end
125
+ def bin
126
+ to_s 0b10
127
+ end
128
+ end
129
+