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?
|
|
|
|
[options, ARGV.empty? ? [ARGF] : ARGV]
|
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def count_file_stats(input)
|
|
|
|
lines = words = bytes = 0
|
|
|
|
input.each_line do |line|
|
|
|
|
lines += 1
|
|
|
|
words += line.split.size
|
|
|
|
bytes += line.bytesize
|
|
|
|
end
|
|
|
|
[lines, words, bytes]
|
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def process_input(source)
|
|
|
|
if source == ARGF || File.exist?(source)
|
2024-07-10 14:55:39 +09:00
|
|
|
input = source == ARGF ? ARGF.read : File.read(source)
|
2024-07-12 13:18:19 +09:00
|
|
|
count_file_stats(input)
|
|
|
|
else
|
|
|
|
puts "wc: #{source}: そのようなファイルやディレクトリはありません"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def update_totals(totals, stats)
|
|
|
|
totals.zip(stats).map { |total, stat| total + stat }
|
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def update_max_widths(max_widths, stats)
|
|
|
|
max_widths.zip(stats).map { |max, stat| [max, stat.to_s.length].max }
|
|
|
|
end
|
2024-07-10 14:55:39 +09:00
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def format_result(stats, max_widths, options)
|
|
|
|
result = []
|
|
|
|
%i[lines words bytes].each_with_index do |key, index|
|
|
|
|
result << stats[index].to_s.rjust(max_widths[index]) if options[key]
|
2024-07-10 14:55:39 +09:00
|
|
|
end
|
2024-07-12 13:18:19 +09:00
|
|
|
result.join(' ')
|
2024-07-02 22:44:35 +09:00
|
|
|
end
|
|
|
|
|
2024-07-12 13:18:19 +09:00
|
|
|
def print_result(result, source)
|
|
|
|
puts "#{result} #{source == ARGF ? '' : source}"
|
|
|
|
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-12 13:18:19 +09:00
|
|
|
|
2024-07-15 09:14:09 +09:00
|
|
|
def process_input_sources(input_sources, options)
|
|
|
|
total_stats = [0, 0, 0]
|
|
|
|
max_widths = [0, 0, 0]
|
|
|
|
|
|
|
|
input_sources.each do |source|
|
|
|
|
stats = process_input(source)
|
|
|
|
total_stats = update_totals(total_stats, stats)
|
|
|
|
max_widths = update_max_widths(max_widths, stats)
|
|
|
|
result = format_result(stats, max_widths, options)
|
|
|
|
print_result(result, source)
|
|
|
|
end
|
2024-07-12 13:18:19 +09:00
|
|
|
|
2024-07-15 09:14:09 +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-15 09:14:09 +09:00
|
|
|
options, input_sources = parse_options
|
|
|
|
process_input_sources(input_sources, options)
|