class RbFind::Entry

Constants

ARROW
DEFAULT_COLORS

Attributes

name[R]
path[R]
prev[R]

Public Class Methods

new(filename, walk) click to toggle source
# File lib/rbfind.rb, line 414
def initialize filename, walk
  @walk = walk
  @prev, @name = walk.current, filename.dup.freeze
  @path = join_path @name
end

Private Class Methods

col_str() click to toggle source
# File lib/rbfind.rb, line 851
def col_str
  ENV[ "RBFIND_COLORS"] || ENV[ "RBFIND_COLOURS"] || ENV[ "LS_COLORS"] || (
    env = DEFAULT_COLORS.dup
    els = ENV[ "LSCOLORS"]
    if els then
      env[ 2*2, els.length] = els
    end
    env
  )
end
colored(arg, num) click to toggle source
# File lib/rbfind.rb, line 815
def colored arg, num
  colors col_str
  "\e[#{@colors[num]}m#{arg}\e[m"
end
Also aliased as: coloured
colors(str) click to toggle source
# File lib/rbfind.rb, line 821
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
Also aliased as: colours
coloured(arg, num)
Alias for: colored
colours(str)
Alias for: colors

Public Instance Methods

aage() click to toggle source
# File lib/rbfind.rb, line 490
def aage ; @walk.start - stat.atime ; end
age()
Alias for: mage
arrow() click to toggle source
# File lib/rbfind.rb, line 555
def arrow
  ARROW + (File.readlink @path) if symlink?
end
Alias for: binary?
binary?( n = 1) → true or false click to toggle source

Test whether the first n blocks contain null characters.

# File lib/rbfind.rb, line 681
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
Also aliased as: bin?
broken?()
Alias for: broken_link?
cage() click to toggle source
# File lib/rbfind.rb, line 492
def cage ; @walk.start - stat.ctime ; end
carrow() click to toggle source
# File lib/rbfind.rb, line 759
def carrow
  r = creadlink
  ARROW + r if r
end
cfullpath() click to toggle source
# File lib/rbfind.rb, line 747
def cfullpath  ; color fullpath  ; end
cfullpath!() click to toggle source
# File lib/rbfind.rb, line 749
def cfullpath! ; color fullpath! ; end
Alias for: entries
cname() click to toggle source
# File lib/rbfind.rb, line 745
def cname      ; color name      ; end
color(arg) click to toggle source
# File lib/rbfind.rb, line 765
def color arg
  color_stat arg, stat
end
Also aliased as: colour
colour(arg)
Alias for: color
contains?( name) → true or false click to toggle source

Check whether a directory contains an entry.

# File lib/rbfind.rb, line 578
def contains? name
  p = File.join @path, name
  File.lstat p
  true
rescue
  false
end
count() click to toggle source
# File lib/rbfind.rb, line 431
def count ; @walk.count ; end
cpath() click to toggle source
# File lib/rbfind.rb, line 746
def cpath      ; color path      ; end
cpath!() click to toggle source
# File lib/rbfind.rb, line 748
def cpath!     ; color path!     ; end
cyclic?() click to toggle source
# File lib/rbfind.rb, line 477
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
depth() click to toggle source
# File lib/rbfind.rb, line 432
def depth ; @walk.depth ; end
dir?()
Alias for: directory?
directory?() click to toggle source
# File lib/rbfind.rb, line 472
def directory? ; stat.directory? ; end
Also aliased as: dir?
dirname() click to toggle source
# File lib/rbfind.rb, line 448
def dirname
  File.basename File.dirname fullpath
end
done() click to toggle source
# File lib/rbfind.rb, line 707
def done  ; raise Done  ; end
Also aliased as: done!
done!()
Alias for: done
empty?() → true or false click to toggle source

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

# File lib/rbfind.rb, line 567
def empty?
  (Dir.new @path).each_child { |f| return false }
  true
rescue Errno::ENOTDIR
end
entries() → ary click to toggle source

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

# File lib/rbfind.rb, line 592
def entries
  (Dir.new @path).children
rescue Errno::ENOTDIR
end
Also aliased as: children
ext() click to toggle source
# File lib/rbfind.rb, line 453
def ext         ; File.extname name ; end
filesize → nil or int click to toggle source
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.

# File lib/rbfind.rb, line 502
def filesize
  stat.file? or return
  if block_given? then
    yield stat.size
  else
    stat.size
  end
end
fullpath() click to toggle source
# File lib/rbfind.rb, line 435
def fullpath ; @fullpath ||= File.absolute_path @path ; end
fullpath!() click to toggle source
# File lib/rbfind.rb, line 446
def fullpath! ; append_slash fullpath ; end
grep(re, color = nil) click to toggle source
# File lib/rbfind.rb, line 661
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
group() click to toggle source
# File lib/rbfind.rb, line 533
def group
  get_group stat.gid
end
group!() click to toggle source
# File lib/rbfind.rb, line 537
def group!
  g = stat.gid
  g == Process.gid ? "." : (get_group g)
end
hidden?() click to toggle source
# File lib/rbfind.rb, line 456
def hidden?  ; name =~ /^\./ ; end
lines { |l,i| ... } → nil click to toggle source

Yield line by line together with the line number i.

# File lib/rbfind.rb, line 646
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
mage() click to toggle source
# File lib/rbfind.rb, line 491
def mage ; @walk.start - stat.mtime ; end
Also aliased as: age
mode() click to toggle source
# File lib/rbfind.rb, line 460
def mode ; stat.mode ; end
mv(newname)
Alias for: rename
no_vcs()
Alias for: novcs
novcs() click to toggle source
# File lib/rbfind.rb, line 713
def novcs
  prune if vcs?
end
Also aliased as: no_vcs
now() click to toggle source
# File lib/rbfind.rb, line 433
def now   ; @walk.start ; end
open() { |h| ... } → obj click to toggle source

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

# File lib/rbfind.rb, line 610
def open &block
  @ostat ||= $stdout.stat
  @ostat.identical? @path and
    raise "Refusing to open output file."
  File.open @path, &block if file?
end
owner()
Alias for: user
owner!()
Alias for: user!
path!() click to toggle source
# File lib/rbfind.rb, line 445
def path!     ; append_slash path     ; end
prune() click to toggle source
# File lib/rbfind.rb, line 710
def prune ; raise Prune ; end
Also aliased as: prune!
prune!()
Alias for: prune
read( n = nil) → str or nil click to toggle source
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.

# File lib/rbfind.rb, line 625
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
rename(newname) click to toggle source
# File lib/rbfind.rb, line 722
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
Also aliased as: mv
rm() click to toggle source
# File lib/rbfind.rb, line 734
def rm
  if directory? then
    Dir.rmdir @path
  else
    File.unlink @path
  end
  @name = @path = nil
  reset
end
rstat() click to toggle source
# File lib/rbfind.rb, line 438
def rstat ; @rstat ||= File.stat  @path ; end
stat() click to toggle source
# File lib/rbfind.rb, line 437
def stat  ; @stat  ||= File.lstat @path ; end
user() click to toggle source
# File lib/rbfind.rb, line 522
def user
  get_user stat.uid
end
Also aliased as: owner
user!() click to toggle source
# File lib/rbfind.rb, line 527
def user!
  u = stat.uid
  u == Process.uid ? "."  : (get_user u)
end
Also aliased as: owner!
vcs?() click to toggle source
# File lib/rbfind.rb, line 599
def vcs?
  %w(CVS .svn .git .hg .fslckout).include? name
end
vimswap?() click to toggle source
# File lib/rbfind.rb, line 698
def vimswap?
  if name =~ /\A(\..+)?\.sw[a-z]\z/i then
    mark = read 5
    mark == "b0VIM"
  end
end
visible?() click to toggle source
# File lib/rbfind.rb, line 457
def visible? ; not hidden?   ; end
without_ext() click to toggle source
# File lib/rbfind.rb, line 454
def without_ext ; name[ /^(.+?)(?:\.[^.]+)?$/, 1 ].to_s ; end

Private Instance Methods

append_slash(s ;) click to toggle source
# File lib/rbfind.rb, line 442
def append_slash s ; directory? ? (File.join s, "") : s ; end
col_type() click to toggle source
# File lib/rbfind.rb, line 802
def col_type
  # Overwrite this to define custom colors
  # Example:
  #   case ext
  #     when ".png", /\.jpe?g$/, /\.tiff?$/ then 15
  #     when /\.tar\.(gz|bz2)$/             then 16
  #   end
end
color_stat(arg, s) click to toggle source
# File lib/rbfind.rb, line 772
def color_stat arg, s
  m = s.mode if s
  code = case m && m >> 12
    when 001 then                        5
    when 002 then                        8
    when 004 then
      if (m & 0002).nonzero? then
        if (m & 01000).nonzero? then    11
        else                            12
        end
      else                               2
      end
    when 006 then                        7
    when 010 then
      if (m & 0111).nonzero? then
        if    (m & 04000).nonzero? then  9
        elsif (m & 02000).nonzero? then 10
        else                             6
        end
      else                               col_type or 0
      end
    when 012 then                        3
    when 014 then                        4
    when 016 then                       13
    when nil then                        1
    else                                14
  end
  self.class.colored arg, code
end
etc() click to toggle source
# File lib/rbfind.rb, line 512
def etc
  Etc
rescue NameError
  require "etc" and retry
  raise
end
get_group(g ;) click to toggle source
# File lib/rbfind.rb, line 519
def get_group g ; (etc.getgrgid g).name rescue g.to_s ; end
get_user(u ;) click to toggle source
# File lib/rbfind.rb, line 518
def get_user  u ; (etc.getpwuid u).name rescue u.to_s ; end
join_path(name) click to toggle source
# File lib/rbfind.rb, line 423
def join_path name
  @prev ? (File.join @prev.path, name).freeze : name
end
method_missing(sym, *args, &block) click to toggle source
Calls superclass method
# File lib/rbfind.rb, line 463
def method_missing sym, *args, &block
  if stat.respond_to? sym then
    stat.send sym, *args, &block
  else
    super
  end
end
reset() click to toggle source
# File lib/rbfind.rb, line 426
def reset
  @fullpath = @stat = @rstat = @ostat = @colors = nil
end