2024-07-02 22:44:35 +09:00
|
|
|
# frozen_string_literal: true
|
2024-07-01 09:32:50 +09:00
|
|
|
|
2024-07-02 22:44:35 +09:00
|
|
|
require 'optparse'
|
2024-07-01 09:32:50 +09:00
|
|
|
|
2024-07-25 08:33:11 +09:00
|
|
|
def main
|
2024-07-29 18:00:15 +09:00
|
|
|
options, filenames = parse_options
|
2024-07-30 14:30:09 +09:00
|
|
|
file_details = collect_file_stats(filenames)
|
|
|
|
total_stats = calculate_total_stats(file_details)
|
2024-07-29 22:05:22 +09:00
|
|
|
max_widths = calculate_max_widths(total_stats)
|
2024-07-30 14:30:09 +09:00
|
|
|
|
|
|
|
file_details.each { |file_stats| print_stats(file_stats, max_widths, options) }
|
|
|
|
print_stats(total_stats, max_widths, options) if filenames.size > 1
|
2024-07-25 08:33:11 +09:00
|
|
|
end
|
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def parse_options
|
|
|
|
options = {}
|
|
|
|
OptionParser.new do |opts|
|
|
|
|
opts.on('-l') { options[:lines] = true }
|
|
|
|
opts.on('-w') { options[:words] = true }
|
|
|
|
opts.on('-c') { options[:bytes] = true }
|
|
|
|
end.parse!
|
|
|
|
options = { bytes: true, lines: true, words: true } if options.empty?
|
2024-07-29 18:00:15 +09:00
|
|
|
filenames = ARGV.empty? ? [''] : ARGV
|
|
|
|
[options, filenames]
|
2024-07-12 13:18:19 +09:00
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-29 18:00:15 +09:00
|
|
|
def collect_file_stats(filenames)
|
|
|
|
filenames.map do |filename|
|
2024-07-30 14:30:09 +09:00
|
|
|
text = filename.empty? ? ARGF.read : File.read(filename)
|
|
|
|
{ filename: filename, lines: text.lines.count, words: text.split.size, bytes: text.bytesize }
|
2024-07-23 22:48:32 +09:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-30 14:30:09 +09:00
|
|
|
def calculate_total_stats(file_details)
|
|
|
|
total_stats = { filename: '合計', lines: 0, words: 0, bytes: 0 }
|
|
|
|
file_details.each do |stats|
|
2024-07-28 20:53:15 +09:00
|
|
|
total_stats[:lines] += stats[:lines]
|
|
|
|
total_stats[:words] += stats[:words]
|
|
|
|
total_stats[:bytes] += stats[:bytes]
|
2024-07-19 14:57:01 +09:00
|
|
|
end
|
2024-07-28 20:53:15 +09:00
|
|
|
total_stats
|
2024-07-12 13:18:19 +09:00
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-29 22:05:22 +09:00
|
|
|
def calculate_max_widths(total_stats)
|
2024-07-30 14:30:09 +09:00
|
|
|
%i[lines words bytes].to_h { |key| [key, total_stats[key].to_s.length] }
|
2024-07-12 13:18:19 +09:00
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def format_result(stats, max_widths, options)
|
2024-07-28 20:53:15 +09:00
|
|
|
%i[lines words bytes].filter_map do |key|
|
|
|
|
stats[key].to_s.rjust(max_widths[key]) if options[key]
|
2024-07-30 14:30:09 +09:00
|
|
|
end
|
2024-07-12 13:18:19 +09:00
|
|
|
end
|
2024-07-02 22:44:35 +09:00
|
|
|
|
2024-07-30 14:30:09 +09:00
|
|
|
def print_stats(stats, max_widths, options)
|
|
|
|
puts (format_result(stats, max_widths, options) << stats[:filename]).join(' ')
|
2024-07-10 14:55:39 +09:00
|
|
|
end
|
2024-07-20 22:45:14 +09:00
|
|
|
|
2024-07-25 08:33:11 +09:00
|
|
|
main
|