Skip to content

Instantly share code, notes, and snippets.

@opamp
Created July 27, 2011 07:38
Show Gist options
  • Save opamp/1108871 to your computer and use it in GitHub Desktop.
Save opamp/1108871 to your computer and use it in GitHub Desktop.
easy ls command (Practice)
#!/usr/bin/ruby -Ku
class Pdir
def initialize(dirnameArray,optionArray)
@AllPrintDirPATH = dirnameArray
@Alloptions = optionArray
@diro = Array.new
dirnameArray.each do |x|
if File.exists?(x) == true && File.directory?(x) == true then
@diro << Dir.open(x)
else
print "Arguments Error\n"
exit 1
end
end
end
def printhide? # a オプションがあればtrue
@Alloptions.each do |oc|
if oc == 'a' then
return true
end
end
return false
end
def printl?
@Alloptions.each do |oc|
if oc == "l" then
return true
end
end
return false
end
def l_printDir
i = 0
@diro.each do |printd|
if @diro.size != 1 then
if i != 0 then
print "\n"
end
print @AllPrintDirPATH[i] + ":\n"
end
i += 1
a = printd.entries
a.each do |filen|
if File.file?(filen) == true then
print "-"
else
print "d"
end
sbuf = File.stat(filen)
smod = sbuf.mode.to_s(8)
print smod[(smod.size - 3)..smod.size - 1] + " " + sbuf.uid.to_s + " " + sbuf.gid.to_s + " " + sbuf.size.to_s + " " + sbuf.atime.to_s + " "
if filen =~ /\./ && self.printhide? == false
next
else
puts filen
end
end
end
return true
end #def end
def defaultPrintDir() #lsの規則にしたがって指定ディレクトリのファイルを表示する
if self.printl? == true then
return self.l_printDir
end
i = 0
@diro.each do |printd|
if @diro.size != 1 then
if i != 0 then
print "\n"
end
print @AllPrintDirPATH[i] + ":\n"
end
i += 1
a = printd.entries
a.each do |pr|
if pr =~ /^\./ && self.printhide? == false
next
else
puts pr
end
end
end
return true
end #defaultPrintDir end
end #class end
goptions = Array.new
options = Array.new
targetdirs = Array.new
ARGV.each do |arg|
if arg =~ /^-.+/ then
goptions << arg
else
targetdirs << arg
end
end
if targetdirs.size == 0 then
targetdirs << '.'
end
goptions.each do |option_str|
1.upto(option_str.size - 1) do |oo|
options << option_str[oo]
end
end
pdirobj = Pdir.new(targetdirs,options)
pdirobj.defaultPrintDir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment