This commit is contained in:
Rikuoh Tsujitani 2024-06-09 21:58:55 +09:00
parent c325da4d19
commit 489e1f66ba
Signed by: riq0h
GPG key ID: 010F09DEA298C717

46
ls.rb
View file

@ -31,74 +31,74 @@ 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']
params['l'] ? output_l(filenames) : output(filenames) params['l'] ? output_file_datails(filenames) : output(filenames)
end end
def output(filenames) def output(filenames)
rows = (filenames.size / COLUMNS) rows = filenames.size.ceildiv(COLUMNS)
length_limits = limiter(filenames, rows) pads = pad_created(filenames, rows)
(0...rows).each do |row| (0...rows).each do |row|
COLUMNS.times do |column| COLUMNS.times do |column|
index = row + column * rows 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 end
puts puts
end end
end end
def limiter(filenames, rows) def pad_created(filenames, rows)
filenames.each_slice(rows).map { _1.map(&:length).max } filenames.each_slice(rows).map { _1.map(&:length).max }
end end
def output_l(filenames) def output_file_datails(filenames)
fileblock(filenames) puts total_file_blocks(filenames)
filenames.each do |file| filenames.each do |file|
file_stat = File::Stat.new(file) file_stat = File::Stat.new(file)
print TYPES[file_stat.ftype] print TYPES[file_stat.ftype]
filemod(file_stat) print file_mode(file_stat)
print_ownerinfo(file_stat, filenames) print owner_info(file_stat, filenames)
timestamp(file_stat) print time_stamp(file_stat)
print_symbolic(file) print symbolic(file)
puts puts
end end
end end
def print_ownerinfo(file_stat, filenames) def owner_info(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(filesize(filenames)) " #{file_stat.size}".rjust(max_filename_length(filenames))
end end
def print_symbolic(filenames) def symbolic(filenames)
if File.lstat(filenames).symlink? if File.lstat(filenames).symlink?
print " #{filenames} -> #{File.readlink(filenames)}" " #{filenames} -> #{File.readlink(filenames)}"
else else
print " #{filenames}" " #{filenames}"
end end
end end
def filemod(file_stat) def file_mode(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]
end end
print file_permission.join('') file_permission.join('')
end end
def filesize(filenames) def max_filename_length(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 time_stamp(file_stat)
print file_stat.mtime.strftime('%_m月 %_d %H:%M') file_stat.mtime.strftime('%_m月 %_d %H:%M')
end end
def fileblock(filenames) def total_file_blocks(filenames)
blocks = filenames.map do |file| blocks = filenames.map do |file|
File::Stat.new(file).blocks File::Stat.new(file).blocks
end end
puts "total #{blocks.sum}" "total #{blocks.sum}"
end end
run run