2024-07-23 22:48:32 +09:00
|
|
|
|
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-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-21 09:50:07 +09:00
|
|
|
sources = ARGV.empty? ? ['-'] : ARGV
|
|
|
|
[options, sources]
|
2024-07-12 13:18:19 +09:00
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-20 22:01:46 +09:00
|
|
|
def input_stream_sources(input_sources, options)
|
2024-07-23 22:48:32 +09:00
|
|
|
all_stats = input_sources.map { |source| [source, input_stream(source)] }
|
|
|
|
total_stats = all_stats.reduce({ lines: 0, words: 0, bytes: 0 }) do |total, (_, stats)|
|
|
|
|
update_totals(total, stats)
|
|
|
|
end
|
|
|
|
max_widths = calculate_max_widths(all_stats, total_stats)
|
|
|
|
|
|
|
|
all_stats.each do |source, stats|
|
2024-07-20 22:01:46 +09:00
|
|
|
result = format_result(stats, max_widths, options)
|
|
|
|
print_result(result, source)
|
2024-07-12 13:18:19 +09:00
|
|
|
end
|
2024-07-23 22:48:32 +09:00
|
|
|
|
2024-07-20 22:01:46 +09:00
|
|
|
print_total(total_stats, max_widths, options) if input_sources.size > 1
|
2024-07-12 13:18:19 +09:00
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-23 22:48:32 +09:00
|
|
|
def calculate_max_widths(all_stats, total_stats)
|
|
|
|
max_widths = { lines: 0, words: 0, bytes: 0 }
|
|
|
|
(all_stats.map(&:last) + [total_stats]).each do |stats|
|
|
|
|
max_widths = update_max_widths(max_widths, stats)
|
|
|
|
end
|
|
|
|
max_widths
|
|
|
|
end
|
|
|
|
|
2024-07-20 22:01:46 +09:00
|
|
|
def input_stream(source)
|
2024-07-19 14:57:01 +09:00
|
|
|
if source == '-'
|
|
|
|
count_file_stats(ARGF)
|
|
|
|
else
|
|
|
|
count_file_stats(File.read(source))
|
|
|
|
end
|
2024-07-12 13:18:19 +09:00
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-20 22:01:46 +09:00
|
|
|
def count_file_stats(input)
|
2024-07-20 22:45:14 +09:00
|
|
|
stats = { lines: 0, words: 0, bytes: 0 }
|
2024-07-20 22:01:46 +09:00
|
|
|
input.each_line do |line|
|
2024-07-20 22:45:14 +09:00
|
|
|
stats[:lines] += 1
|
|
|
|
stats[:words] += line.split.size
|
|
|
|
stats[:bytes] += line.bytesize
|
2024-07-20 22:01:46 +09:00
|
|
|
end
|
2024-07-20 22:45:14 +09:00
|
|
|
stats
|
2024-07-20 22:01:46 +09:00
|
|
|
end
|
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def update_totals(totals, stats)
|
2024-07-20 22:45:14 +09:00
|
|
|
totals.merge(stats) { |_key, total, stat| total + stat }
|
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 update_max_widths(max_widths, stats)
|
2024-07-20 22:45:14 +09:00
|
|
|
max_widths.merge(stats) { |_key, max, stat| [max, stat.to_s.length].max }
|
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-20 22:45:14 +09:00
|
|
|
result = {}
|
|
|
|
%i[lines words bytes].each do |key|
|
|
|
|
result[key] = stats[key].to_s.rjust(max_widths[key]) if options[key]
|
2024-07-10 14:55:39 +09:00
|
|
|
end
|
2024-07-20 22:45:14 +09:00
|
|
|
result.values.join(' ')
|
2024-07-02 22:44:35 +09:00
|
|
|
end
|
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def print_result(result, source)
|
2024-07-16 21:57:49 +09:00
|
|
|
puts "#{result} #{source == '-' ? '' : source}"
|
2024-07-12 13:18:19 +09:00
|
|
|
end
|
2024-07-02 22:44:35 +09:00
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def print_total(total_stats, max_widths, options)
|
|
|
|
total_result = format_result(total_stats, max_widths, options)
|
|
|
|
puts "#{total_result} 合計"
|
2024-07-10 14:55:39 +09:00
|
|
|
end
|
2024-07-20 22:45:14 +09:00
|
|
|
|
|
|
|
options, input_sources = parse_options
|
|
|
|
input_stream_sources(input_sources, options)
|