rbfind 2.1.1 → 2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ceecae8ade339a4cd276462cda193baa69a5c956ae942fe421861f333913bdad
4
- data.tar.gz: e362ee60d3078d1325512cb78607a2d7f6b9e4b5d45c7eb2f2ca41547c931d43
3
+ metadata.gz: 7bee921838a5b4d93f5483295e8c7e6b263e144e8d52e5ca53a464a222a5d055
4
+ data.tar.gz: 03f0b19f70747dbb152011ecc8f414d99764ad9052c82f9b0b1715939fb7a3dd
5
5
  SHA512:
6
- metadata.gz: 12e0872e576fd9fda282631373c95d8ab8b91781828c07190d466ae1e2829f99a07d4c2dc36ba47298f4ccf27eec8049d4210ddfb7c1bb3a6578871247a77cd7
7
- data.tar.gz: 4c0aada931ba589b63a9578711c2b2f808c1551c66d849679b43afca801dc57fb83cb8ccd11801481c7928ed014493b7b15863625d9842631f431b2a6d12937a
6
+ metadata.gz: 3de28232b6d9b842ced7ff2880e4d80938c194c13c93fd6a69e5265d3f6999276129bc1469eae62a121776222d27836c95cc208ae2d656080875345d342d471e
7
+ data.tar.gz: d979151e81e3eaf660304c2072550f857957e9c08c4da74af67afc5d23e498021b546c33920ba20099fcb2248a2a621eccf51b9732b34212a1273188ec4d6c73
data/bin/rbfind CHANGED
@@ -34,8 +34,8 @@ module RbFind
34
34
  [ %w(--maxdepth -m), :num, "maxium step depth"],
35
35
  [ %w(--follow -y), nil, "follow symbolic links"],
36
36
  [ %w(--nosort -S), nil, "unsorted"],
37
- [ %w(--sort-by -s), :str, "sort expression ('n' is name)"],
38
- [ %w(--reverse -R), :str, "reverse sort"],
37
+ [ %w(--sort-by -s), :str, "sort expression"],
38
+ [ %w(--reverse -R), nil, "reverse the sort"],
39
39
  [ %w(--require -r), :rb, "require library"],
40
40
  [ %w(--puts-path -p), nil, "do 'puts path/cpath' on true block"],
41
41
  [ %w(--ls-l -P), nil, "do 'ls -l' style output on true block"],
@@ -48,14 +48,14 @@ module RbFind
48
48
  [ %w(--binary -b), nil, "grep even binary files"],
49
49
  [ %w(--no-vcs -C), nil, "prune version control dirs (CVS/.svn/.git)"],
50
50
  [ %w(--no-swap -W), nil, "ignore Vim swapfiles"],
51
- [ %w(--skip -k), :lst, "filenames to skip"],
52
- [ %w(--demand -D), :lst, "skip all filenames but these"],
51
+ [ %w(--skip -k), :rgx, "filenames to skip"],
52
+ [ %w(--demand -D), :rgx, "skip all filenames but these"],
53
53
  [ %w(--ext -e), :lst, "skip all filename extensions but these"],
54
54
  [ %w(--visible -I), nil, "skip all hidden (starting with .dot)"],
55
55
  [ %w(--nodirs -N), nil, "skip directories"],
56
56
  [ %w(--begin -B), :blk, "eval block before begin"],
57
57
  [ %w(--end -E), :blk, "eval block after end"],
58
- [ %w(--file -f), :blk, "read block expression from file"],
58
+ [ %w(--file -f), :nam, "read block expression from file"],
59
59
  [ %w(--encoding -K), :str, "encoding extern[:intern] (same as ruby -E)"],
60
60
  ]
61
61
 
@@ -142,7 +142,7 @@ module RbFind
142
142
  @binary or @block = "not binary? and (#@block)"
143
143
 
144
144
  else
145
- @block ||= if @args.last and not (File.exists? @args.last) then
145
+ @block ||= if @args.last and not (File.lstat @args.last rescue false) then
146
146
  @args.pop.dup
147
147
  else
148
148
  @puts ||= true
@@ -483,6 +483,20 @@ class NilClass
483
483
  end
484
484
  end
485
485
 
486
+ class TrueClass
487
+ def <=> oth
488
+ oth == true ? 0 : oth == false ? 1 : nil
489
+ end
490
+ include Enumerable
491
+ end
492
+
493
+ class FalseClass
494
+ def <=> oth
495
+ oth == false ? 0 : oth == true ? -1 : nil
496
+ end
497
+ include Enumerable
498
+ end
499
+
486
500
  class Proc
487
501
  def to_s
488
502
  "#<%s:0x%08x>" % [ self.class, object_id]
data/lib/rbfind.rb CHANGED
@@ -8,7 +8,7 @@ require "rbfind/csv"
8
8
 
9
9
  module RbFind
10
10
 
11
- VERSION = "2.1.1".freeze
11
+ VERSION = "2.5".freeze
12
12
 
13
13
  =begin rdoc
14
14
 
@@ -241,7 +241,7 @@ Reverse sort:
241
241
 
242
242
  Sort without case sensitivity and preceding dot:
243
243
 
244
- s = proc { |x| x =~ /^\.?/ ; $'.downcase }
244
+ s = proc { name =~ /^\.?/ ; $'.downcase }
245
245
  RbFind.run sort: s do
246
246
  puts path
247
247
  end
@@ -270,36 +270,28 @@ Sort without case sensitivity and preceding dot:
270
270
 
271
271
  private
272
272
 
273
+ Params = Struct.new :max_depth, :depth_first, :follow,
274
+ :sort, :reverse, :error, :block
275
+
273
276
  def initialize max_depth: nil, depth_first: nil, follow: nil,
274
277
  sort: true, reverse: false, error: nil, &block
275
- @max_depth = max_depth
276
- @depth_first = depth_first
277
- @follow = follow
278
- @sort = sort_parser sort, reverse
279
- @error = error
280
- @block = block
281
-
282
- ostat = $stdout.stat
283
- @ostat = ostat if ostat.file?
284
-
285
- @wd, @start, @count = Dir.getwd, Time.now, 0
278
+ @params = Params.new max_depth, depth_first, follow,
279
+ (sort_parser sort), reverse, error, block
280
+ @start, @count, @depth = Time.now, 0, 0
286
281
  end
287
282
 
288
- def sort_parser st, rev
289
- r = case st
290
- when Proc then proc { |l| l.sort_by! &st }
291
- when nil, false, nil, "" then proc { }
292
- else proc { |l| l.sort! }
283
+ def sort_parser st
284
+ case st
285
+ when Proc then proc { |l| l.sort_by! { |e| e.instance_eval &st } }
286
+ when String then proc { |l| l.sort_by! { |e| e.instance_eval st } }
287
+ when nil, false then proc { }
288
+ else proc { |l| l.sort_by! { |e| e.name } }
293
289
  end
294
- rev ? proc { |l| r.call l ; l.reverse! } : r
295
290
  end
296
291
 
297
292
  public
298
293
 
299
- attr_reader :wd, :start, :count, :depth
300
-
301
294
  def run *args
302
- @levels, @depth = [], 0
303
295
  args.flatten!
304
296
  args.compact!
305
297
  if args.empty? then
@@ -307,94 +299,69 @@ Sort without case sensitivity and preceding dot:
307
299
  else
308
300
  args.each { |base|
309
301
  handle_error do
310
- File.exists? base or raise "`#{base}` doesn't exist."
311
- visit_depth base
302
+ File.lstat base rescue raise "`#{base}` doesn't exist."
303
+ e = Entry.new base, self
304
+ enter e
312
305
  end
313
306
  }
314
307
  end
315
- ensure
316
- @levels = @depth = nil
317
308
  end
318
309
 
310
+ attr_reader :start, :count
311
+ attr_reader :depth
312
+ attr_reader :current
313
+
319
314
  private
320
315
 
321
- def join_path
322
- (File.join @levels).freeze
316
+ def enter elem
317
+ c_, @current = @current, elem
318
+ @count += 1
319
+ visit_depth
320
+ ensure
321
+ @current = c_
323
322
  end
324
323
 
325
- def visit filename
326
- @depth += 1
327
- visit_depth filename
328
- ensure
329
- @depth -= 1
324
+ def visit_dir dir
325
+ return if @params.max_depth and @params.max_depth == @depth
326
+ list = (Dir.new dir).children
327
+ list = list.map { |f| Entry.new f, self }
328
+ @params.sort.call list
329
+ list.reverse! if @params.reverse
330
+ begin
331
+ @depth += 1
332
+ list.each { |e| enter e }
333
+ ensure
334
+ @depth -= 1
335
+ end
330
336
  end
331
337
 
332
- def visit_depth filename
333
- @levels.push filename.dup.freeze
334
- p_, @path = @path, join_path
335
- if @depth_first then
338
+ def visit_depth
339
+ if @params.depth_first then
336
340
  enter_dir
337
- call_block or raise "#{self.class}: prune doesn't work with :depth_first."
341
+ call_block
338
342
  else
339
343
  call_block and enter_dir
340
344
  end
341
- @count += 1
342
- ensure
343
- @path = p_
344
- @levels.pop
345
345
  end
346
346
 
347
347
  def enter_dir
348
- return unless File.directory? @path
349
- if File.symlink? @path then
350
- return unless @follow and handle_error do
351
- d = @path.dup
352
- while d != Dir::CUR_DIR do
353
- d, = File.split d
354
- raise "cyclic recursion in #@path" if File.identical? d, @path
355
- end
356
- true
357
- end
358
- end
348
+ return unless @current.stat.directory? || (@params.follow &&
349
+ @current.symlink? && @current.rstat.directory?)
359
350
  handle_error do
360
- visit_dir @path
351
+ @current.cyclic? and
352
+ raise "Cyclic recursion in #{@current.path}"
353
+ visit_dir @current.path
361
354
  end
362
355
  end
363
356
 
364
- def visit_dir dir
365
- return if @max_depth and @max_depth == @depth
366
- list = (Dir.new dir).children
367
- @sort.call list
368
- list.each { |f| visit f }
369
- ensure
370
- end
371
-
372
357
  def call_block
373
- e = Entry.new @levels.last, @path, self
374
358
  handle_error do
375
- $_, $. = e.name, count
376
359
  begin
377
- e.instance_eval &@block
360
+ $_, $. = @current.name, @count
361
+ @current.instance_eval &@params.block
378
362
  rescue Done
379
363
  end
380
- if !(e.name.equal? @levels.last) && e.name != @levels.last then
381
- if e.name then
382
- e.name == (File.basename e.name) or
383
- raise "#{self.class}: rename to `#{e.name}' may not be a path."
384
- e.name.freeze
385
- @levels.pop
386
- @levels.push e.name
387
- p, @path = @path, join_path
388
- File.rename p, @path
389
- else
390
- if e.dir? then
391
- Dir.rmdir @path
392
- else
393
- File.unlink @path
394
- end
395
- end
396
- end
397
- true
364
+ @current.path
398
365
  end
399
366
  rescue Prune
400
367
  end
@@ -402,9 +369,9 @@ Sort without case sensitivity and preceding dot:
402
369
  def handle_error
403
370
  yield
404
371
  rescue
405
- case @error
406
- when Proc then @error.call
407
- when String then instance_eval @error
372
+ case @params.error
373
+ when Proc then @params.error.call
374
+ when String then instance_eval @params.error
408
375
  else raise
409
376
  end
410
377
  nil
@@ -417,21 +384,35 @@ Sort without case sensitivity and preceding dot:
417
384
 
418
385
  attr_reader :path, :name
419
386
 
420
- def initialize name, path, walk
421
- @name, @path, @walk = name, path, walk
387
+ def initialize filename, walk
388
+ @walk = walk
389
+ @prev, @name = walk.current, filename.dup.freeze
390
+ @path = join_path @name
391
+ end
392
+
393
+ protected
394
+ attr_reader :prev
395
+ private
396
+ def join_path name
397
+ @prev ? (File.join @prev.path, name).freeze : name
398
+ end
399
+ def reset
400
+ @fullpath = @stat = @rstat = @ostat = @colors = nil
422
401
  end
402
+ public
423
403
 
404
+ def count ; @walk.count ; end
424
405
  def depth ; @walk.depth ; end
425
406
  def now ; @walk.start ; end
426
407
 
427
- def fullpath ; @fullpath ||= File.absolute_path @path, @walk.wd ; end
408
+ def fullpath ; @fullpath ||= File.absolute_path @path ; end
428
409
 
429
410
  def stat ; @stat ||= File.lstat @path ; end
430
411
  def rstat ; @rstat ||= File.stat @path ; end
431
412
 
432
413
 
433
414
  private
434
- def append_slash s ; (File.directory? s) ? (File.join s, "") : s ; end
415
+ def append_slash s ; directory? ? (File.join s, "") : s ; end
435
416
  public
436
417
 
437
418
  def path! ; append_slash path ; end
@@ -461,7 +442,23 @@ Sort without case sensitivity and preceding dot:
461
442
  end
462
443
  public
463
444
 
464
- def dir? ; stat.directory? ; end
445
+ def directory? ; stat.directory? ; end
446
+ alias dir? directory?
447
+
448
+ def symlink? ; stat.symlink? ; end
449
+
450
+ def cyclic?
451
+ e = self
452
+ loop do
453
+ e = e.prev
454
+ e or break
455
+ if File.identical? e.path, @path then
456
+ return true
457
+ end
458
+ end
459
+ false
460
+ end
461
+
465
462
 
466
463
  def aage ; @walk.start - stat.atime ; end
467
464
  def mage ; @walk.start - stat.mtime ; end
@@ -516,10 +513,10 @@ Sort without case sensitivity and preceding dot:
516
513
  end
517
514
 
518
515
 
519
- def readlink ; File.readlink @path if stat.symlink? ; end
516
+ def readlink ; File.readlink @path if symlink? ; end
520
517
 
521
518
  def broken_link?
522
- return unless stat.symlink?
519
+ return unless symlink?
523
520
  rstat
524
521
  false
525
522
  rescue
@@ -529,7 +526,7 @@ Sort without case sensitivity and preceding dot:
529
526
 
530
527
  ARROW = " -> "
531
528
  def arrow
532
- ARROW + (File.readlink @path) if stat.symlink?
529
+ ARROW + (File.readlink @path) if symlink?
533
530
  end
534
531
 
535
532
 
@@ -552,7 +549,11 @@ Sort without case sensitivity and preceding dot:
552
549
  # Check whether a directory contains an entry.
553
550
  #
554
551
  def contains? name
555
- File.exists? File.join @path, name
552
+ p = File.join @path, name
553
+ File.lstat p
554
+ true
555
+ rescue
556
+ false
556
557
  end
557
558
 
558
559
  # :call-seq:
@@ -580,7 +581,8 @@ Sort without case sensitivity and preceding dot:
580
581
  # nothing will be done.
581
582
  #
582
583
  def open &block
583
- @ostat and @ostat.identical? @path and
584
+ @ostat ||= $stdout.stat
585
+ @ostat.identical? @path and
584
586
  raise "Refusing to open output file."
585
587
  File.open @path, &block if file?
586
588
  end
@@ -690,10 +692,27 @@ Sort without case sensitivity and preceding dot:
690
692
  include Csv
691
693
 
692
694
 
693
- def rename newname ; @name = newname ; end
695
+ def rename newname
696
+ @name = newname
697
+ newname == (File.basename newname) or
698
+ raise "Rename to `#{newname}' may not be a path."
699
+ p = join_path newname
700
+ (File.exist? p) and raise "Rename to `#{p}` would overwrite."
701
+ File.rename @path, p
702
+ @name, @path = newname.dup.freeze, p
703
+ reset
704
+ end
694
705
  alias mv rename
695
706
 
696
- def rm ; @name = nil ; end
707
+ def rm
708
+ if directory? then
709
+ Dir.rmdir @path
710
+ else
711
+ File.unlink @path
712
+ end
713
+ @name = @path = nil
714
+ reset
715
+ end
697
716
 
698
717
 
699
718
  def cname ; color name ; end
@@ -83,6 +83,16 @@ class Numeric # sizes in bytes
83
83
  end
84
84
 
85
85
 
86
+ class Integer
87
+ def to_g
88
+ s = to_s
89
+ l = []
90
+ while (t = s.slice! /\d{1,3}\z/) do l.unshift t end
91
+ l.join "_"
92
+ end
93
+ end
94
+
95
+
86
96
  class Numeric
87
97
  "smhdw".each_char { |c|
88
98
  define_method c do Time.to_sec self, c end
data/lib/rbfind/table.rb CHANGED
@@ -9,18 +9,28 @@ module RbFind
9
9
 
10
10
  def initialize *heads
11
11
  heads.flatten!
12
- @heads = heads
12
+ @heads = heads.map { |h|
13
+ a = case h
14
+ when />\z/ then +1
15
+ when /\^\z/ then 0
16
+ when /<?\z/ then -1
17
+ end
18
+ [ $`, a]
19
+ }
13
20
  @rows = []
14
21
  end
15
22
 
16
- def spawn
17
- self.class.new *@heads
23
+ attr_reader :heads
24
+ protected :heads
25
+ def initialize_copy oth
26
+ @heads = oth.heads
27
+ @rows = []
18
28
  end
19
29
 
20
30
  def add *row
21
31
  row.flatten!
22
32
  n = @heads.size
23
- row[ 0, n] = row[ 0, n].map { |r| r.to_s }
33
+ row.map! { |r| break if n.zero? ; n -= 1 ; r.to_s }
24
34
  @rows.push row
25
35
  end
26
36
 
@@ -32,20 +42,24 @@ module RbFind
32
42
  @rows.empty?
33
43
  end
34
44
 
35
- def output head: false
45
+ def output head: false, ifempty: nil
46
+ if empty? and ifempty then
47
+ puts ifempty
48
+ return
49
+ end
36
50
  make_lines head: head do |l| puts l end
37
51
  end
38
52
 
39
53
  def make_lines head: false
40
54
  rs = @rows
41
- rs.unshift heads_plain if head
55
+ rs.unshift @heads.map { |(h,a)| h } if head
42
56
  w = calc_widths
43
57
  rs.each { |r|
44
- j = (w.zip @heads, r).map { |v,h,c|
45
- case h
46
- when />\z/ then c.rjust v
47
- when /\^\z/ then c.center v
48
- when /<?\z/ then c.ljust v
58
+ j = (w.zip @heads, r).map { |v,(_,a),c|
59
+ case a
60
+ when -1 then c.ljust v
61
+ when 0 then c.center v
62
+ when +1 then c.rjust v
49
63
  end
50
64
  }
51
65
  l = j.join " "
@@ -59,14 +73,14 @@ module RbFind
59
73
  @html = ""
60
74
  tag :table, table, nl: 2 do
61
75
  tag :tr, row, nl: 1 do
62
- (@heads.zip heads_plain).each { |h,c|
63
- tag :td, c.downcase, align: (html_align h) do @html << c end
76
+ @heads.each { |(h,a)|
77
+ tag :td, h.downcase, align: a do @html << h end
64
78
  }
65
79
  end
66
80
  @rows.each { |r|
67
81
  tag :tr, table, nl: 1 do
68
- (@heads.zip heads_plain, r).each { |h,g,c|
69
- tag :td, g.downcase, align: (html_align h) do @html << c end
82
+ (@heads.zip r).each { |(g,a),c|
83
+ tag :td, g.downcase, align: a do @html << c end
70
84
  }
71
85
  end
72
86
  }
@@ -86,13 +100,9 @@ module RbFind
86
100
  w
87
101
  end
88
102
 
89
- def heads_plain
90
- @heads.map { |h| h.sub /\W\z/, "" }
91
- end
92
-
93
103
  def tag name, cls, nl: 0, align: nil
94
104
  @html << "<#{name}"
95
- @html << " style=\"text-align: " << align << ";\"" if align
105
+ @html << " style=\"text-align: " << (html_align align) << ";\"" if align
96
106
  @html << " class=\"" << cls << "\"" if cls
97
107
  @html << ">"
98
108
  @html << $/ if nl > 1
@@ -101,11 +111,11 @@ module RbFind
101
111
  @html << $/ if nl > 0
102
112
  end
103
113
 
104
- def html_align h
105
- case h
106
- when />/ then "right"
107
- when /\^/ then "center"
108
- when /<?/ then "left"
114
+ def html_align a
115
+ case a
116
+ when -1 then "left"
117
+ when 0 then "center"
118
+ when +1 then "right"
109
119
  end
110
120
  end
111
121
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbfind
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: '2.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bertram Scharpf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-22 00:00:00.000000000 Z
11
+ date: 2021-02-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  A replacement for the standard UNIX command find.
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  version: '0'
55
55
  requirements:
56
56
  - just Ruby
57
- rubygems_version: 3.0.6
57
+ rubygems_version: 3.0.8
58
58
  signing_key:
59
59
  specification_version: 4
60
60
  summary: Ruby replacement for the standard Unix find tool