class RbFind::Entry
Constants
- ARROW
- DEFAULT_COLORS
Attributes
Public Class Methods
Source
# File lib/rbfind.rb, line 825 def colored arg, num colors col_str "\e[#{@colors[num]}m#{arg}\e[m" end
Source
# File lib/rbfind.rb, line 831 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
Source
# File lib/rbfind.rb, line 413 def initialize filename, walk @walk = walk @prev, @name = walk.current, filename.dup.freeze @path = join_path @name end
Private Class Methods
Source
# File lib/rbfind.rb, line 861 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
Public Instance Methods
Source
# File lib/rbfind.rb, line 566 def arrow ARROW + (File.readlink @path) if symlink? end
Source
# File lib/rbfind.rb, line 691 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
Test whether the first n
blocks contain null characters.
Source
# File lib/rbfind.rb, line 503 def birthage ; @walk.start - birthtime ; end
Source
# File lib/rbfind.rb, line 498 def birthtime stat.birthtime rescue NotImplementedError File.birthtime @path end
Please consider thoroughly whether you really need this. bugs.ruby-lang.org/issues/21205#change-112494
Source
# File lib/rbfind.rb, line 556 def broken_link? return unless symlink? rstat false rescue true end
Source
# File lib/rbfind.rb, line 775 def color arg color_stat arg, stat end
Source
# File lib/rbfind.rb, line 589 def contains? name p = File.join @path, name File.lstat p true rescue false end
Check whether a directory contains an entry.
Source
# File lib/rbfind.rb, line 761 def creadlink l = readlink if l then s = rstat rescue nil color_stat l, s end end
Source
# File lib/rbfind.rb, line 478 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
Source
# File lib/rbfind.rb, line 449 def dirname File.basename File.dirname fullpath end
Source
# File lib/rbfind.rb, line 578 def empty? Dir.open @path do |d| d.each_child { |f| return false } end true rescue Errno::ENOTDIR end
Look up if the directory is empty. If the object is not a directory or not accessible, nil
is returned.
Source
# File lib/rbfind.rb, line 603 def entries Dir.open @path do |d| d.children end rescue Errno::ENOTDIR end
Return all entries in an array. If the object is not a directory, nil
is returned.
Source
# File lib/rbfind.rb, line 513 def filesize stat.file? or return if block_given? then yield stat.size else stat.size end end
Returns the files size. When the object is not a regular file, nil will be returned or the block will not be called.
Source
# File lib/rbfind.rb, line 436 def fullpath ; @fullpath ||= File.absolute_path @path ; end
Source
# File lib/rbfind.rb, line 671 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
Source
# File lib/rbfind.rb, line 548 def group! g = stat.gid g == Process.gid ? "." : (get_group g) end
Source
# File lib/rbfind.rb, line 657 def lines block_given? or return lines do end r = false open { |file| n = 0 file.each_line { |l| l.chomp! n += 1 r ||= true if yield l, n } r } end
Yield line by line together with the line number i
.
Source
# File lib/rbfind.rb, line 492 def mage ; @walk.start - stat.mtime ; end
Source
# File lib/rbfind.rb, line 621 def open &block @ostat ||= $stdout.stat @ostat.identical? @path and raise "Refusing to open output file." File.open @path, &block if file? end
Open the file for reading. If the object is not a regular file, nothing will be done.
Source
# File lib/rbfind.rb, line 636 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
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.
Source
# File lib/rbfind.rb, line 554 def readlink ; File.readlink @path if symlink? ; end
Source
# File lib/rbfind.rb, line 732 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
Source
# File lib/rbfind.rb, line 744 def rm if directory? then Dir.rmdir @path else File.unlink @path end @name = @path = nil reset end
Source
# File lib/rbfind.rb, line 538 def user! u = stat.uid u == Process.uid ? "." : (get_user u) end
Source
# File lib/rbfind.rb, line 610 def vcs? %w(CVS .svn .git .hg .fslckout).include? name end
Source
# File lib/rbfind.rb, line 708 def vimswap? if name =~ /\A(\..+)?\.sw[a-z]\z/i then mark = read 5 mark == "b0VIM" end end
Source
# File lib/rbfind.rb, line 455 def without_ext ; name[ /^(.+?)(?:\.[^.]+)?$/, 1 ].to_s ; end
Private Instance Methods
Source
# File lib/rbfind.rb, line 443 def append_slash s ; directory? ? (File.join s, "") : s ; end
Source
# File lib/rbfind.rb, line 812 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
Source
# File lib/rbfind.rb, line 782 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
Source
# File lib/rbfind.rb, line 523 def etc Etc rescue NameError require "etc" and retry raise end
Source
# File lib/rbfind.rb, line 530 def get_group g ; (etc.getgrgid g).name rescue g.to_s ; end
Source
# File lib/rbfind.rb, line 529 def get_user u ; (etc.getpwuid u).name rescue u.to_s ; end
Source
# File lib/rbfind.rb, line 424 def join_path name @prev ? (File.join @prev.path, name).freeze : name end
Source
# File lib/rbfind.rb, line 464 def method_missing sym, *args, &block if stat.respond_to? sym then stat.send sym, *args, &block else super end end
Source
# File lib/rbfind.rb, line 427 def reset @fullpath = @stat = @rstat = @ostat = @colors = nil end