This commit is contained in:
Rikuoh Tsujitani 2024-06-01 09:18:04 +09:00
parent bb1374aca3
commit 57a594c1b2
Signed by: riq0h
GPG key ID: 010F09DEA298C717

70
ls.rb
View file

@ -27,46 +27,58 @@ PERMISSIONS = {
'7' => 'rwx' '7' => 'rwx'
}.freeze }.freeze
def run # def run
listed_filenames = list_filenames # listed_filenames = list_filenames
filenames_matrix = slice_filenames(listed_filenames) # filenames_matrix = slice_filenames(listed_filenames)
filled_filenames = fill_filenames(filenames_matrix) # filled_filenames = fill_filenames(filenames_matrix)
arrange_filenames(filled_filenames) # arrange_filenames(filled_filenames)
end # end
def list_filenames def run
params = ARGV.getopts('a', 'r', 'l') params = ARGV.getopts('a', 'r', 'l')
filenames = params['a'] ? Dir.glob('*', File::FNM_DOTMATCH) : Dir.glob('*') filenames = params['a'] ? Dir.glob('*', File::FNM_DOTMATCH) : Dir.glob('*')
filenames = filenames.reverse if params['r'] filenames = filenames.reverse if params['r']
if params['l'] params['r'] ? output_l(filenames) : output(filenames)
output(filenames)
exit
else
filenames
end
end end
def output(filenames) def output(filenames)
file_blocks(filenames) number_of_row = (filenames.size / COLUMNS.to_f).ceil
filenames.each do |file| max_lengths = make_max_lengths(filenames, number_of_row)
file_stat = File::Stat.new(file)
print TYPES[file_stat.ftype] (0...number_of_row).each do |row|
permission(file_stat) COLUMNS.times do |column|
printer(file_stat, filenames) index = row + column * number_of_row
timestamp(file_stat) print filenames[index].ljust(max_lengths[column] + COLUMNS) if filenames[index]
symbolic(file) end
puts puts
end end
end end
def printer(file_stat, filenames) def make_max_lengths(files, number_of_row)
files.each_slice(number_of_row).to_a.map { _1.map(&:length).max }
end
def output_l(filenames)
fileblocks(filenames)
filenames.each do |file|
file_stat = File::Stat.new(file)
print TYPES[file_stat.ftype]
filemods(file_stat)
prints(file_stat, filenames)
timestamps(file_stat)
symbolics(file)
puts
end
end
def prints(file_stat, filenames)
print " #{file_stat.nlink}" print " #{file_stat.nlink}"
print " #{Etc.getpwuid(file_stat.uid).name}" print " #{Etc.getpwuid(file_stat.uid).name}"
print " #{Etc.getgrgid(file_stat.gid).name}" print " #{Etc.getgrgid(file_stat.gid).name}"
print " #{file_stat.size}".rjust(file_size(filenames)) print " #{file_stat.size}".rjust(filesizes(filenames))
end end
def symbolic(filenames) def symbolics(filenames)
if File.lstat(filenames).symlink? if File.lstat(filenames).symlink?
print " #{filenames} -> #{File.readlink(filenames)}" print " #{filenames} -> #{File.readlink(filenames)}"
else else
@ -74,7 +86,7 @@ def symbolic(filenames)
end end
end end
def permission(file_stat) def filemods(file_stat)
file_count = file_stat.mode.to_s(8).slice(-3, 3) file_count = file_stat.mode.to_s(8).slice(-3, 3)
file_permission = file_count.split('').map do |file| file_permission = file_count.split('').map do |file|
PERMISSIONS[file] PERMISSIONS[file]
@ -82,15 +94,15 @@ def permission(file_stat)
print file_permission.join('') print file_permission.join('')
end end
def file_size(filenames) def filesizes(filenames)
filenames.map { |file| File.size(file) }.max.to_s.length + MARGIN filenames.map { |file| File.size(file) }.max.to_s.length + MARGIN
end end
def timestamp(file_stat) def timestamps(file_stat)
print file_stat.mtime.strftime('%_m %_d %H:%M') print file_stat.mtime.strftime('%_m %_d %H:%M')
end end
def file_blocks(filenames) def fileblocks(filenames)
blocks = filenames.map do |file| blocks = filenames.map do |file|
File::Stat.new(file).blocks File::Stat.new(file).blocks
end end