This commit is contained in:
Rikuoh Tsujitani 2024-04-24 22:37:28 +09:00
parent 3c65a01570
commit a6fb0f99ba
Signed by: riq0h
GPG key ID: 010F09DEA298C717

37
ls.rb
View file

@ -1,40 +1,39 @@
# frozen_string_literal: true # frozen_string_literal: true
COLUMNS = 3 COLUMNS = 3
files = []
def run(files) def run
served_files = serve_files(files) listed_filenames = list_filenames
sliced_files = slice_files(served_files) filenames_matrix = slice_filenames(listed_filenames)
filled_files = fill_files(sliced_files) filled_filenames = fill_filenames(filenames_matrix)
arrange_files(filled_files) arrange_filenames(filled_filenames)
end end
def serve_files(_files) def list_filenames
Dir.glob('*') Dir.glob('*')
end end
def slice_files(served_files) def slice_filenames(listed_filenames)
columns_size = served_files.size.ceildiv(COLUMNS) columns_size = listed_filenames.size.ceildiv(COLUMNS)
served_files.each_slice(columns_size).to_a listed_filenames.each_slice(columns_size).to_a
end end
def fill_files(sliced_files) def fill_filenames(filenames_matrix)
array_size = sliced_files.map(&:size).max array_size = filenames_matrix.map(&:size).max
sliced_files.each do |slice| filenames_matrix.each do |slice|
slice << '' while slice.size < array_size slice << '' while slice.size < array_size
end end
sliced_files.transpose filenames_matrix.transpose
end end
def arrange_files(filled_files) def arrange_filenames(filled_filenames)
string_count = filled_files.flatten.map(&:size).max filenames_count = filled_filenames.flatten.map(&:size).max
filled_files.each do |arrange| filled_filenames.each do |arrange|
arrange.each do |arranged| arrange.each do |arranged|
print arranged.ljust(string_count + 5) print arranged.ljust(filenames_count + 5)
end end
puts puts
end end
end end
run(files) run