Class: RbFind::Entry
- Inherits:
-
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
Returns a new instance of Entry.
414
415
416
417
418
|
# File 'lib/rbfind.rb', line 414
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
465
466
467
468
469
470
471
|
# File 'lib/rbfind.rb', line 465
def method_missing sym, *args, &block
if stat.respond_to? sym then
stat.send sym, *args, &block
else
super
end
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
412
413
414
|
# File 'lib/rbfind.rb', line 412
def name
@name
end
|
#path ⇒ Object
Returns the value of attribute path.
412
413
414
|
# File 'lib/rbfind.rb', line 412
def path
@path
end
|
Class Method Details
.colored(arg, num) ⇒ Object
Also known as:
coloured
826
827
828
829
|
# File 'lib/rbfind.rb', line 826
def colored arg, num
colors col_str
"\e[#{@colors[num]}m#{arg}\e[m"
end
|
.colors(str) ⇒ Object
Also known as:
colours
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
|
# File 'lib/rbfind.rb', line 832
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
#aage ⇒ Object
492
|
# File 'lib/rbfind.rb', line 492
def aage ; @walk.start - stat.atime ; end
|
#arrow ⇒ Object
567
568
569
|
# File 'lib/rbfind.rb', line 567
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.
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
# File 'lib/rbfind.rb', line 692
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
|
#birthage ⇒ Object
Also known as:
bage
504
|
# File 'lib/rbfind.rb', line 504
def birthage ; @walk.start - birthtime ; end
|
#birthtime ⇒ Object
499
500
501
502
503
|
# File 'lib/rbfind.rb', line 499
def birthtime
stat.birthtime
rescue NotImplementedError
File.birthtime @path
end
|
#broken_link? ⇒ Boolean
Also known as:
broken?
557
558
559
560
561
562
563
|
# File 'lib/rbfind.rb', line 557
def broken_link?
return unless symlink?
rstat
false
rescue
true
end
|
#cage ⇒ Object
495
|
# File 'lib/rbfind.rb', line 495
def cage ; @walk.start - stat.ctime ; end
|
#carrow ⇒ Object
770
771
772
773
|
# File 'lib/rbfind.rb', line 770
def carrow
r = creadlink
ARROW + r if r
end
|
#cfullpath ⇒ Object
758
|
# File 'lib/rbfind.rb', line 758
def cfullpath ; color fullpath ; end
|
#cfullpath! ⇒ Object
760
|
# File 'lib/rbfind.rb', line 760
def cfullpath! ; color fullpath! ; end
|
#cname ⇒ Object
756
|
# File 'lib/rbfind.rb', line 756
def cname ; color name ; end
|
#color(arg) ⇒ Object
Also known as:
colour
776
777
778
|
# File 'lib/rbfind.rb', line 776
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.
590
591
592
593
594
595
596
|
# File 'lib/rbfind.rb', line 590
def contains? name
p = File.join @path, name
File.lstat p
true
rescue
false
end
|
#count ⇒ Object
433
|
# File 'lib/rbfind.rb', line 433
def count ; @walk.count ; end
|
#cpath ⇒ Object
757
|
# File 'lib/rbfind.rb', line 757
def cpath ; color path ; end
|
#cpath! ⇒ Object
759
|
# File 'lib/rbfind.rb', line 759
def cpath! ; color path! ; end
|
#creadlink ⇒ Object
762
763
764
765
766
767
768
|
# File 'lib/rbfind.rb', line 762
def creadlink
l = readlink
if l then
s = rstat rescue nil
color_stat l, s
end
end
|
#cyclic? ⇒ Boolean
479
480
481
482
483
484
485
486
487
488
489
|
# File 'lib/rbfind.rb', line 479
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 ⇒ Object
434
|
# File 'lib/rbfind.rb', line 434
def depth ; @walk.depth ; end
|
#directory? ⇒ Boolean
Also known as:
dir?
474
|
# File 'lib/rbfind.rb', line 474
def directory? ; stat.directory? ; end
|
#dirname ⇒ Object
450
451
452
|
# File 'lib/rbfind.rb', line 450
def dirname
File.basename File.dirname fullpath
end
|
#done ⇒ Object
Also known as:
done!
718
|
# File 'lib/rbfind.rb', line 718
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.
579
580
581
582
583
|
# File 'lib/rbfind.rb', line 579
def empty?
Dir.open @path do |d| d.each_child { |f| return false } end
true
rescue Errno::ENOTDIR
end
|
#entries ⇒ Object
Also known as:
children
:call-seq:
entries() -> ary
Return all entries in an array. If the object is not a directory, nil
is returned.
604
605
606
607
|
# File 'lib/rbfind.rb', line 604
def entries
Dir.open @path do |d| d.children end
rescue Errno::ENOTDIR
end
|
#ext ⇒ Object
455
|
# File 'lib/rbfind.rb', line 455
def ext ; File.extname name ; end
|
#filesize ⇒ Object
: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.
514
515
516
517
518
519
520
521
|
# File 'lib/rbfind.rb', line 514
def filesize
stat.file? or return
if block_given? then
yield stat.size
else
stat.size
end
end
|
#fullpath ⇒ Object
437
|
# File 'lib/rbfind.rb', line 437
def fullpath ; @fullpath ||= File.absolute_path @path ; end
|
#fullpath! ⇒ Object
448
|
# File 'lib/rbfind.rb', line 448
def fullpath! ; append_slash fullpath ; end
|
#grep(re, color = nil) ⇒ Object
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
# File 'lib/rbfind.rb', line 672
def grep re, color = nil
case color
when /\A\d+(?:;\d+)*\z/, nil, false then
when true then color = "31;1" 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 ⇒ Object
545
546
547
|
# File 'lib/rbfind.rb', line 545
def group
get_group stat.gid
end
|
#group! ⇒ Object
549
550
551
552
|
# File 'lib/rbfind.rb', line 549
def group!
g = stat.gid
g == Process.gid ? "." : (get_group g)
end
|
#hidden? ⇒ Boolean
458
|
# File 'lib/rbfind.rb', line 458
def hidden? ; name =~ /^\./ ; end
|
#inspect ⇒ Object
420
|
# File 'lib/rbfind.rb', line 420
def inspect ; "<##{self.class} #@path>" ; end
|
#lines ⇒ Object
:call-seq:
lines { |l,i| ... } -> nil
Yield line by line together with the line number i
.
658
659
660
661
662
663
664
665
666
667
668
669
670
|
# File 'lib/rbfind.rb', line 658
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
|
#mage ⇒ Object
Also known as:
age
493
|
# File 'lib/rbfind.rb', line 493
def mage ; @walk.start - stat.mtime ; end
|
#mode ⇒ Object
462
|
# File 'lib/rbfind.rb', line 462
def mode ; stat.mode ; end
|
#novcs ⇒ Object
Also known as:
no_vcs
724
725
726
|
# File 'lib/rbfind.rb', line 724
def novcs
prune if vcs?
end
|
#now ⇒ Object
435
|
# File 'lib/rbfind.rb', line 435
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.
622
623
624
625
626
627
|
# File 'lib/rbfind.rb', line 622
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
447
|
# File 'lib/rbfind.rb', line 447
def path! ; append_slash path ; end
|
#prune ⇒ Object
Also known as:
prune!
721
|
# File 'lib/rbfind.rb', line 721
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.
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
|
# File 'lib/rbfind.rb', line 637
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
|
#readlink ⇒ Object
555
|
# File 'lib/rbfind.rb', line 555
def readlink ; File.readlink @path if symlink? ; end
|
#rename(newname) ⇒ Object
Also known as:
mv
733
734
735
736
737
738
739
740
741
742
|
# File 'lib/rbfind.rb', line 733
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
|
#rm ⇒ Object
745
746
747
748
749
750
751
752
753
|
# File 'lib/rbfind.rb', line 745
def rm
if directory? then
Dir.rmdir @path
else
File.unlink @path
end
@name = @path = nil
reset
end
|
#rstat ⇒ Object
440
|
# File 'lib/rbfind.rb', line 440
def rstat ; @rstat ||= File.stat @path ; end
|
#stat ⇒ Object
439
|
# File 'lib/rbfind.rb', line 439
def stat ; @stat ||= File.lstat @path ; end
|
#symlink? ⇒ Boolean
477
|
# File 'lib/rbfind.rb', line 477
def symlink? ; stat.symlink? ; end
|
#user ⇒ Object
Also known as:
owner
534
535
536
|
# File 'lib/rbfind.rb', line 534
def user
get_user stat.uid
end
|
#user! ⇒ Object
Also known as:
owner!
539
540
541
542
|
# File 'lib/rbfind.rb', line 539
def user!
u = stat.uid
u == Process.uid ? "." : (get_user u)
end
|
#vcs? ⇒ Boolean
611
612
613
|
# File 'lib/rbfind.rb', line 611
def vcs?
%w(CVS .svn .git .hg .fslckout).include? name
end
|
#vimswap? ⇒ Boolean
709
710
711
712
713
714
|
# File 'lib/rbfind.rb', line 709
def vimswap?
if name =~ /\A(\..+)?\.sw[a-z]\z/i then
mark = read 5
mark == "b0VIM"
end
end
|
#visible? ⇒ Boolean
459
|
# File 'lib/rbfind.rb', line 459
def visible? ; not hidden? ; end
|
#without_ext ⇒ Object
456
|
# File 'lib/rbfind.rb', line 456
def without_ext ; name[ /^(.+?)(?:\.[^.]+)?$/, 1 ].to_s ; end
|