This commit is contained in:
Rikuoh Tsujitani 2024-06-06 11:07:44 +09:00
parent 57a594c1b2
commit 4d5404ea3b
Signed by: riq0h
GPG key ID: 010F09DEA298C717

71
ls.rb
View file

@ -27,58 +27,50 @@ PERMISSIONS = {
'7' => 'rwx' '7' => 'rwx'
}.freeze }.freeze
# def run
# listed_filenames = list_filenames
# filenames_matrix = slice_filenames(listed_filenames)
# filled_filenames = fill_filenames(filenames_matrix)
# arrange_filenames(filled_filenames)
# end
def run 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['r'] ? output_l(filenames) : output(filenames) params['l'] ? output_l(filenames) : output(filenames)
end end
def output(filenames) def output(filenames)
number_of_row = (filenames.size / COLUMNS.to_f).ceil rows = (filenames.size / COLUMNS)
max_lengths = make_max_lengths(filenames, number_of_row) length_limits = init_limit(filenames, rows)
(0...rows).each do |row|
(0...number_of_row).each do |row|
COLUMNS.times do |column| COLUMNS.times do |column|
index = row + column * number_of_row index = row + column * rows
print filenames[index].ljust(max_lengths[column] + COLUMNS) if filenames[index] print filenames[index].ljust(length_limits[column] + COLUMNS) if filenames[index]
end end
puts puts
end end
end end
def make_max_lengths(files, number_of_row) def init_limit(filenames, rows)
files.each_slice(number_of_row).to_a.map { _1.map(&:length).max } filenames.each_slice(rows).map { _1.map(&:length).max }
end end
def output_l(filenames) def output_l(filenames)
fileblocks(filenames) fileblock(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]
filemods(file_stat) filemod(file_stat)
prints(file_stat, filenames) print_ownerinfo(file_stat, filenames)
timestamps(file_stat) timestamp(file_stat)
symbolics(file) print_symbolic(file)
puts puts
end end
end end
def prints(file_stat, filenames) def print_ownerinfo(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(filesizes(filenames)) print " #{file_stat.size}".rjust(filesize(filenames))
end end
def symbolics(filenames) def print_symbolic(filenames)
if File.lstat(filenames).symlink? if File.lstat(filenames).symlink?
print " #{filenames} -> #{File.readlink(filenames)}" print " #{filenames} -> #{File.readlink(filenames)}"
else else
@ -86,7 +78,7 @@ def symbolics(filenames)
end end
end end
def filemods(file_stat) def filemod(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]
@ -94,42 +86,19 @@ def filemods(file_stat)
print file_permission.join('') print file_permission.join('')
end end
def filesizes(filenames) def filesize(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 timestamps(file_stat) def timestamp(file_stat)
print file_stat.mtime.strftime('%_m月 %_d %H:%M') print file_stat.mtime.strftime('%_m月 %_d %H:%M')
end end
def fileblocks(filenames) def fileblock(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}" puts "total #{blocks.sum}"
end end
def slice_filenames(listed_filenames)
columns_size = listed_filenames.size.ceildiv(COLUMNS)
listed_filenames.each_slice(columns_size).to_a
end
def fill_filenames(filenames_matrix)
array_size = filenames_matrix.map(&:size).max
filenames_matrix.each do |slice|
slice << '' while slice.size < array_size
end
filenames_matrix.transpose
end
def arrange_filenames(filled_filenames)
filenames_count = filled_filenames.flatten.map(&:size).max
filled_filenames.each do |arrange|
arrange.each do |arranged|
print arranged.ljust(filenames_count + 5)
end
puts
end
end
run run