fjord/ls.rb

45 lines
789 B
Ruby
Raw Normal View History

2024-04-11 21:51:53 +09:00
# frozen_string_literal: true
2024-04-15 22:48:09 +09:00
COLUMNS = 3
files = []
2024-04-11 21:51:53 +09:00
2024-04-15 22:48:09 +09:00
def run(files)
served = serve(files)
sliced = slice(served)
filled = fill(sliced)
arranged(filled)
2024-04-11 21:51:53 +09:00
end
2024-04-15 22:48:09 +09:00
def serve(files)
files.concat(Dir.glob('*'))
2024-04-11 21:51:53 +09:00
end
2024-04-15 22:48:09 +09:00
def slice(served)
element = if (served.size % COLUMNS).zero?
served.size / COLUMNS
else
served.size / COLUMNS + 1
end
served.each_slice(element).to_a
2024-04-11 21:51:53 +09:00
end
2024-04-15 22:48:09 +09:00
def fill(sliced)
array_size = sliced.map(&:size).max
sliced.each do |slice|
slice << '' while slice.size < array_size
2024-04-11 21:51:53 +09:00
end
2024-04-15 22:48:09 +09:00
sliced.transpose
2024-04-11 21:51:53 +09:00
end
2024-04-15 22:48:09 +09:00
def arranged(filled)
element2 = filled.flatten.map(&:size).max
filled.each do |arrange|
arrange.each do |arranged|
print arranged.ljust(element2 + 5)
2024-04-11 21:51:53 +09:00
end
puts
end
end
2024-04-15 22:48:09 +09:00
run(files)