ls -l
This commit is contained in:
parent
b3000625fd
commit
07c9730ed2
1 changed files with 57 additions and 4 deletions
61
ls.rb
61
ls.rb
|
@ -1,8 +1,10 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
|
require 'etc'
|
||||||
|
|
||||||
COLUMNS = 3
|
COLUMNS = 3
|
||||||
|
SIZE_INDENT = 3
|
||||||
|
|
||||||
def run
|
def run
|
||||||
listed_filenames = list_filenames
|
listed_filenames = list_filenames
|
||||||
|
@ -14,8 +16,8 @@ end
|
||||||
def list_filenames
|
def list_filenames
|
||||||
params = ARGV.getopts('a', 'r', 'l')
|
params = ARGV.getopts('a', 'r', 'l')
|
||||||
flags = params['a'] ? File::FNM_DOTMATCH : 0
|
flags = params['a'] ? File::FNM_DOTMATCH : 0
|
||||||
params['l'] ? option_l : 0
|
|
||||||
filenames = Dir.glob('*', flags)
|
filenames = Dir.glob('*', flags)
|
||||||
|
params['l'] ? ownerinfo(filenames) : filenames
|
||||||
if params['r']
|
if params['r']
|
||||||
filenames.reverse
|
filenames.reverse
|
||||||
else
|
else
|
||||||
|
@ -23,9 +25,31 @@ def list_filenames
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def permission
|
def ownerinfo(filenames)
|
||||||
list_filenames.each do |index|
|
filenames.each do |file|
|
||||||
file_stat = File::Stat.new
|
file_stat = File::Stat.new(file)
|
||||||
|
file_type(file_stat)
|
||||||
|
permission(file_stat)
|
||||||
|
print " #{file_stat.nlink}"
|
||||||
|
print " #{Etc.getpwuid(file_stat.uid).name}"
|
||||||
|
print " #{Etc.getgrgid(file_stat.gid).name}"
|
||||||
|
print " #{file_stat.size}".rjust(file_size(filenames))
|
||||||
|
timestamp(file_stat)
|
||||||
|
symbolic(file)
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def symbolic(file)
|
||||||
|
if File.lstat(file).symlink?
|
||||||
|
print " #{file} -> #{File.readlink(file)}"
|
||||||
|
else
|
||||||
|
print " #{file}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def permission(file_stat)
|
||||||
|
(-3..-1).each do |index|
|
||||||
case file_stat.mode.to_s(8)[index]
|
case file_stat.mode.to_s(8)[index]
|
||||||
when '0' then print '---'
|
when '0' then print '---'
|
||||||
when '1' then print '--x'
|
when '1' then print '--x'
|
||||||
|
@ -39,6 +63,35 @@ def permission
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def file_type(file_stat)
|
||||||
|
case file_stat.ftype
|
||||||
|
when 'file'
|
||||||
|
print '-'
|
||||||
|
when 'directory'
|
||||||
|
print 'd'
|
||||||
|
when 'characterSpecial'
|
||||||
|
print 'c'
|
||||||
|
when 'fifo'
|
||||||
|
print 'p'
|
||||||
|
when 'link'
|
||||||
|
print 'l'
|
||||||
|
when 'socket'
|
||||||
|
print 's'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def file_size(filenames)
|
||||||
|
filenames.map { |file| File.size(file) }.max.to_s.length + SIZE_INDENT
|
||||||
|
end
|
||||||
|
|
||||||
|
def timestamp(file_stat)
|
||||||
|
if Time.now - file_stat.mtime >= (60 * 60 * 24 * (365 / 2.0)) || (Time.now - file_stat.mtime).negative?
|
||||||
|
print " #{file_stat.mtime.strftime('%_m %_d %Y')}"
|
||||||
|
else
|
||||||
|
print " #{file_stat.mtime.strftime('%_m %_d %H:%M')}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def slice_filenames(listed_filenames)
|
def slice_filenames(listed_filenames)
|
||||||
columns_size = listed_filenames.size.ceildiv(COLUMNS)
|
columns_size = listed_filenames.size.ceildiv(COLUMNS)
|
||||||
listed_filenames.each_slice(columns_size).to_a
|
listed_filenames.each_slice(columns_size).to_a
|
||||||
|
|
Loading…
Reference in a new issue