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