This commit is contained in:
Rikuoh Tsujitani 2024-04-23 07:45:29 +09:00
parent 6645a15a7b
commit 3c65a01570
Signed by: riq0h
GPG key ID: 010F09DEA298C717

38
ls.rb
View file

@ -4,38 +4,34 @@ COLUMNS = 3
files = [] files = []
def run(files) def run(files)
served = serve(files) served_files = serve_files(files)
sliced = slice(served) sliced_files = slice_files(served_files)
filled = fill(sliced) filled_files = fill_files(sliced_files)
arranged(filled) arrange_files(filled_files)
end end
def serve(files) def serve_files(_files)
files.concat(Dir.glob('*')) Dir.glob('*')
end end
def slice(served) def slice_files(served_files)
element = if (served.size % COLUMNS).zero? columns_size = served_files.size.ceildiv(COLUMNS)
served.size / COLUMNS served_files.each_slice(columns_size).to_a
else
served.size / COLUMNS + 1
end
served.each_slice(element).to_a
end end
def fill(sliced) def fill_files(sliced_files)
array_size = sliced.map(&:size).max array_size = sliced_files.map(&:size).max
sliced.each do |slice| sliced_files.each do |slice|
slice << '' while slice.size < array_size slice << '' while slice.size < array_size
end end
sliced.transpose sliced_files.transpose
end end
def arranged(filled) def arrange_files(filled_files)
element2 = filled.flatten.map(&:size).max string_count = filled_files.flatten.map(&:size).max
filled.each do |arrange| filled_files.each do |arrange|
arrange.each do |arranged| arrange.each do |arranged|
print arranged.ljust(element2 + 5) print arranged.ljust(string_count + 5)
end end
puts puts
end end