-l
This commit is contained in:
parent
c325da4d19
commit
489e1f66ba
1 changed files with 23 additions and 23 deletions
46
ls.rb
46
ls.rb
|
@ -31,74 +31,74 @@ def run
|
|||
params = ARGV.getopts('a', 'r', 'l')
|
||||
filenames = params['a'] ? Dir.glob('*', File::FNM_DOTMATCH) : Dir.glob('*')
|
||||
filenames = filenames.reverse if params['r']
|
||||
params['l'] ? output_l(filenames) : output(filenames)
|
||||
params['l'] ? output_file_datails(filenames) : output(filenames)
|
||||
end
|
||||
|
||||
def output(filenames)
|
||||
rows = (filenames.size / COLUMNS)
|
||||
length_limits = limiter(filenames, rows)
|
||||
rows = filenames.size.ceildiv(COLUMNS)
|
||||
pads = pad_created(filenames, rows)
|
||||
(0...rows).each do |row|
|
||||
COLUMNS.times do |column|
|
||||
index = row + column * rows
|
||||
print filenames[index].ljust(length_limits[column] + COLUMNS) if filenames[index]
|
||||
print filenames[index].ljust(pads[column] + COLUMNS) if filenames[index]
|
||||
end
|
||||
puts
|
||||
end
|
||||
end
|
||||
|
||||
def limiter(filenames, rows)
|
||||
def pad_created(filenames, rows)
|
||||
filenames.each_slice(rows).map { _1.map(&:length).max }
|
||||
end
|
||||
|
||||
def output_l(filenames)
|
||||
fileblock(filenames)
|
||||
def output_file_datails(filenames)
|
||||
puts total_file_blocks(filenames)
|
||||
filenames.each do |file|
|
||||
file_stat = File::Stat.new(file)
|
||||
print TYPES[file_stat.ftype]
|
||||
filemod(file_stat)
|
||||
print_ownerinfo(file_stat, filenames)
|
||||
timestamp(file_stat)
|
||||
print_symbolic(file)
|
||||
print file_mode(file_stat)
|
||||
print owner_info(file_stat, filenames)
|
||||
print time_stamp(file_stat)
|
||||
print symbolic(file)
|
||||
puts
|
||||
end
|
||||
end
|
||||
|
||||
def print_ownerinfo(file_stat, filenames)
|
||||
def owner_info(file_stat, filenames)
|
||||
print " #{file_stat.nlink}"
|
||||
print " #{Etc.getpwuid(file_stat.uid).name}"
|
||||
print " #{Etc.getgrgid(file_stat.gid).name}"
|
||||
print " #{file_stat.size}".rjust(filesize(filenames))
|
||||
" #{file_stat.size}".rjust(max_filename_length(filenames))
|
||||
end
|
||||
|
||||
def print_symbolic(filenames)
|
||||
def symbolic(filenames)
|
||||
if File.lstat(filenames).symlink?
|
||||
print " #{filenames} -> #{File.readlink(filenames)}"
|
||||
" #{filenames} -> #{File.readlink(filenames)}"
|
||||
else
|
||||
print " #{filenames}"
|
||||
" #{filenames}"
|
||||
end
|
||||
end
|
||||
|
||||
def filemod(file_stat)
|
||||
def file_mode(file_stat)
|
||||
file_count = file_stat.mode.to_s(8).slice(-3, 3)
|
||||
file_permission = file_count.split('').map do |file|
|
||||
PERMISSIONS[file]
|
||||
end
|
||||
print file_permission.join('')
|
||||
file_permission.join('')
|
||||
end
|
||||
|
||||
def filesize(filenames)
|
||||
def max_filename_length(filenames)
|
||||
filenames.map { |file| File.size(file) }.max.to_s.length + MARGIN
|
||||
end
|
||||
|
||||
def timestamp(file_stat)
|
||||
print file_stat.mtime.strftime('%_m月 %_d %H:%M')
|
||||
def time_stamp(file_stat)
|
||||
file_stat.mtime.strftime('%_m月 %_d %H:%M')
|
||||
end
|
||||
|
||||
def fileblock(filenames)
|
||||
def total_file_blocks(filenames)
|
||||
blocks = filenames.map do |file|
|
||||
File::Stat.new(file).blocks
|
||||
end
|
||||
puts "total #{blocks.sum}"
|
||||
"total #{blocks.sum}"
|
||||
end
|
||||
|
||||
run
|
||||
|
|
Loading…
Reference in a new issue