fjord/ls2.rb

31 lines
638 B
Ruby
Raw Normal View History

2024-05-21 22:32:10 +09:00
require 'optparse'
require 'etc'
NUMBER_OF_COLUMNS = 3
COLUMN_MARGIN = 3
SIZE_INDENT = 2
def main
files = Dir.glob('*').sort
output_ls(files)
end
def output_ls(files)
number_of_row = (files.size / NUMBER_OF_COLUMNS.to_f)
max_lengths = make_max_lengths(files, number_of_row)
(0...number_of_row).each do |row|
NUMBER_OF_COLUMNS.times do |column|
index = row + column * number_of_row
print files[index].ljust(max_lengths[column] + COLUMN_MARGIN) if files[index]
end
puts
end
end
def make_max_lengths(files, number_of_row)
files.each_slice(number_of_row).to_a.map { _1.map(&:length).max }
end
main