Class: RbFind::Entry

Inherits:
Object
  • Object
show all
Includes:
Csv
Defined in:
lib/rbfind.rb

Constant Summary collapse

ARROW =
" -> "

Constants included from Csv

Csv::COLON

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Csv

#colsep, #csv, outfile, putl, #spcsep, #tabsep

Constructor Details

#initialize(filename, walk) ⇒ Entry



387
388
389
390
391
# File 'lib/rbfind.rb', line 387

def initialize filename, walk
  @walk = walk
  @prev, @name = walk.current, filename.dup.freeze
  @path = join_path @name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)



436
437
438
439
440
441
442
# File 'lib/rbfind.rb', line 436

def method_missing sym, *args, &block
  if stat.respond_to? sym then
    stat.send sym, *args, &block
  else
    super
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



385
386
387
# File 'lib/rbfind.rb', line 385

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



385
386
387
# File 'lib/rbfind.rb', line 385

def path
  @path
end

Class Method Details

.colored(arg, num) ⇒ Object Also known as: coloured



788
789
790
791
# File 'lib/rbfind.rb', line 788

def colored arg, num
  colors col_str
  "\e[#{@colors[num]}m#{arg}\e[m"
end

.colors(str) ⇒ Object Also known as: colours



794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
# File 'lib/rbfind.rb', line 794

def colors str
  @colors ||= if str =~ /:/ then
    h = {}
    (str.split ":").each { |a|
      t, c = a.split "="
      h[  t] = c
    }
    %w(rs or di ln so pi ex bd cd su sg tw ow - -).map { |t| h[ t] }
  else
    cols = []
    str.scan /(.)(.)/i do
      fg, bg = $~.captures.map { |x| x.downcase.ord - ?a.ord }
      a = []
      case fg
        when 0..7 then a.push 30 + fg
      end
      a.push 1 if $1 == $1.upcase
      case bg
        when 0..7 then a.push 40 + bg
      end
      e = a.join ";"
      cols.push e
    end
    cols
  end
end

Instance Method Details

#aageObject



463
# File 'lib/rbfind.rb', line 463

def aage ; @walk.start - stat.atime ; end

#arrowObject



528
529
530
# File 'lib/rbfind.rb', line 528

def arrow
  ARROW + (File.readlink @path) if symlink?
end

#binary?(n = 1) ⇒ Boolean Also known as: bin?

:call-seq:

binary?( n = 1)   -> true or false

Test whether the first n blocks contain null characters.



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/rbfind.rb', line 654

def binary? n = 1
  bs = stat.blksize
  open { |file|
    loop do
      if n then
        break if n <= 0
        n -= 1
      end
      b = file.read bs
      b or break
      return true if b[ "\0"]
    end
  }
  false
end

#broken_link?Boolean Also known as: broken?



518
519
520
521
522
523
524
# File 'lib/rbfind.rb', line 518

def broken_link?
  return unless symlink?
  rstat
  false
rescue
  true
end

#cageObject



465
# File 'lib/rbfind.rb', line 465

def cage ; @walk.start - stat.ctime ; end

#carrowObject



732
733
734
735
# File 'lib/rbfind.rb', line 732

def carrow
  r = creadlink
  ARROW + r if r
end

#cfullpathObject



720
# File 'lib/rbfind.rb', line 720

def cfullpath  ; color fullpath  ; end

#cfullpath!Object



722
# File 'lib/rbfind.rb', line 722

def cfullpath! ; color fullpath! ; end

#cnameObject



718
# File 'lib/rbfind.rb', line 718

def cname      ; color name      ; end

#color(arg) ⇒ Object Also known as: colour



738
739
740
# File 'lib/rbfind.rb', line 738

def color arg
  color_stat arg, stat
end

#contains?(name) ⇒ Boolean

:call-seq:

contains?( name)     -> true or false

Check whether a directory contains an entry.



551
552
553
554
555
556
557
# File 'lib/rbfind.rb', line 551

def contains? name
  p = File.join @path, name
  File.lstat p
  true
rescue
  false
end

#countObject



404
# File 'lib/rbfind.rb', line 404

def count ; @walk.count ; end

#cpathObject



719
# File 'lib/rbfind.rb', line 719

def cpath      ; color path      ; end

#cpath!Object



721
# File 'lib/rbfind.rb', line 721

def cpath!     ; color path!     ; end


724
725
726
727
728
729
730
# File 'lib/rbfind.rb', line 724

def creadlink
  l = readlink
  if l then
    s = rstat rescue nil
    color_stat l, s
  end
end

#cyclic?Boolean



450
451
452
453
454
455
456
457
458
459
460
# File 'lib/rbfind.rb', line 450

def cyclic?
  e = self
  loop do
    e = e.prev
    e or break
    if File.identical? e.path, @path then
      return true
    end
  end
  false
end

#depthObject



405
# File 'lib/rbfind.rb', line 405

def depth ; @walk.depth ; end

#directory?Boolean Also known as: dir?



445
# File 'lib/rbfind.rb', line 445

def directory? ; stat.directory? ; end

#dirnameObject



421
422
423
# File 'lib/rbfind.rb', line 421

def dirname
  File.basename File.dirname fullpath
end

#doneObject Also known as: done!

Raises:



680
# File 'lib/rbfind.rb', line 680

def done  ; raise Done  ; end

#empty?Boolean

:call-seq:

empty?()     -> true or false

Look up if the directory is empty. If the object is not a directory or not accessible, nil is returned.



540
541
542
543
544
# File 'lib/rbfind.rb', line 540

def empty?
  (Dir.new @path).each_child { |f| return false }
  true
rescue Errno::ENOTDIR
end

#entriesObject Also known as: children

:call-seq:

entries()     -> ary

Return all entries in an array. If the object is not a directory, nil is returned.



565
566
567
568
# File 'lib/rbfind.rb', line 565

def entries
  (Dir.new @path).children
rescue Errno::ENOTDIR
end

#extObject



426
# File 'lib/rbfind.rb', line 426

def ext         ; File.extname name ; end

#filesizeObject

:call-seq:

filesize => nil or int
filesize { |size| ... } => obj

Returns the files size. When the object is not a regular file, nil will be returned or the block will not be called.



475
476
477
478
479
480
481
482
# File 'lib/rbfind.rb', line 475

def filesize
  stat.file? or return
  if block_given? then
    yield stat.size
  else
    stat.size
  end
end

#fullpathObject



408
# File 'lib/rbfind.rb', line 408

def fullpath ; @fullpath ||= File.absolute_path @path ; end

#fullpath!Object



419
# File 'lib/rbfind.rb', line 419

def fullpath! ; append_slash fullpath ; end

#grep(re, color = nil) ⇒ Object



634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/rbfind.rb', line 634

def grep re, color = nil
  case color
    when /\A\d+(?:;\d+)*\z/, nil, false then
    when true then color = "31;1"  # red
    else           raise "Illegal color spec: #{color}"
  end
  lines { |l,i|
    l.scrub!
    l =~ re or next
    color and l = "#$`\e[#{color}m#$&\e[m#$'"
    colsep @path, i, l
    true
  }
end

#groupObject



506
507
508
# File 'lib/rbfind.rb', line 506

def group
  get_group stat.gid
end

#group!Object



510
511
512
513
# File 'lib/rbfind.rb', line 510

def group!
  g = stat.gid
  g == Process.gid ? "." : (get_group g)
end

#hidden?Boolean



429
# File 'lib/rbfind.rb', line 429

def hidden?  ; name =~ /^\./ ; end

#linesObject

:call-seq:

lines { |l,i| ... }    -> nil

Yield line by line together with the line number i.



619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/rbfind.rb', line 619

def lines
  block_given? or return lines do end
  r = false
  open { |file|
    n = 0
    file.each_line { |l|
      l.chomp!
      n += 1
      $_, $. = l, n
      r ||= true if yield l, n
    }
    r
  }
end

#mageObject Also known as: age



464
# File 'lib/rbfind.rb', line 464

def mage ; @walk.start - stat.mtime ; end

#modeObject



433
# File 'lib/rbfind.rb', line 433

def mode ; stat.mode ; end

#novcsObject Also known as: no_vcs



686
687
688
# File 'lib/rbfind.rb', line 686

def novcs
  prune if vcs?
end

#nowObject



406
# File 'lib/rbfind.rb', line 406

def now   ; @walk.start ; end

#open(&block) ⇒ Object

:call-seq:

open() { |h| ... }    -> obj

Open the file for reading. If the object is not a regular file, nothing will be done.



583
584
585
586
587
588
# File 'lib/rbfind.rb', line 583

def open &block
  @ostat ||= $stdout.stat
  @ostat.identical? @path and
    raise "Refusing to open output file."
  File.open @path, &block if file?
end

#path!Object



418
# File 'lib/rbfind.rb', line 418

def path!     ; append_slash path     ; end

#pruneObject Also known as: prune!

Raises:



683
# File 'lib/rbfind.rb', line 683

def prune ; raise Prune ; end

#read(n = nil) ⇒ Object

:call-seq:

read( n = nil)               -> str or nil
read( n = nil) { |b| ... }   -> nil

Read the first n bytes or return nil for others than regular files. nil reads to end of file. If a block is given, chonks of n bytes (or all) will be yielded.



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/rbfind.rb', line 598

def read n = nil
  open { |o|
    if block_given? then
      if n then
        while (r = o.read n) do
          yield r
        end
      else
        yield o.read
      end
    else
      o.read n
    end
  }
end


516
# File 'lib/rbfind.rb', line 516

def readlink ; File.readlink @path if symlink? ; end

#rename(newname) ⇒ Object Also known as: mv



695
696
697
698
699
700
701
702
703
704
# File 'lib/rbfind.rb', line 695

def rename newname
  @name = newname
  newname == (File.basename newname) or
    raise "Rename to `#{newname}' may not be a path."
  p = join_path newname
  (File.exist? p) and raise "Rename to `#{p}` would overwrite."
  File.rename @path, p
  @name, @path = newname.dup.freeze, p
  reset
end

#rmObject



707
708
709
710
711
712
713
714
715
# File 'lib/rbfind.rb', line 707

def rm
  if directory? then
    Dir.rmdir @path
  else
    File.unlink @path
  end
  @name = @path = nil
  reset
end

#rstatObject



411
# File 'lib/rbfind.rb', line 411

def rstat ; @rstat ||= File.stat  @path ; end

#statObject



410
# File 'lib/rbfind.rb', line 410

def stat  ; @stat  ||= File.lstat @path ; end

#symlink?Boolean



448
# File 'lib/rbfind.rb', line 448

def symlink? ; stat.symlink? ; end

#userObject Also known as: owner



495
496
497
# File 'lib/rbfind.rb', line 495

def user
  get_user stat.uid
end

#user!Object Also known as: owner!



500
501
502
503
# File 'lib/rbfind.rb', line 500

def user!
  u = stat.uid
  u == Process.uid ? "."  : (get_user u)
end

#vcs?Boolean



572
573
574
# File 'lib/rbfind.rb', line 572

def vcs?
  %w(CVS .svn .git .hg .fslckout).include? name
end

#vimswap?Boolean



671
672
673
674
675
676
# File 'lib/rbfind.rb', line 671

def vimswap?
  if name =~ /\A(\..+)?\.sw[a-z]\z/i then
    mark = read 5
    mark == "b0VIM"
  end
end

#visible?Boolean



430
# File 'lib/rbfind.rb', line 430

def visible? ; not hidden?   ; end

#without_extObject



427
# File 'lib/rbfind.rb', line 427

def without_ext ; name[ /^(.+?)(?:\.[^.]+)?$/, 1 ].to_s ; end