めちゃくちゃ修正した
This commit is contained in:
parent
af7e235665
commit
056d23d6b2
1 changed files with 29 additions and 56 deletions
83
wc.rb
83
wc.rb
|
@ -2,6 +2,15 @@
|
|||
|
||||
require 'optparse'
|
||||
|
||||
def main
|
||||
options, input_sources = parse_options
|
||||
all_stats = collect_all_stats(input_sources)
|
||||
total_stats = calculate_total_stats(all_stats)
|
||||
max_widths = calculate_max_widths(all_stats, total_stats)
|
||||
print_all_stats(all_stats, max_widths, options)
|
||||
print_all_stats([['合計', total_stats]], max_widths, options) if input_sources.size > 1
|
||||
end
|
||||
|
||||
def parse_options
|
||||
options = {}
|
||||
OptionParser.new do |opts|
|
||||
|
@ -10,75 +19,39 @@ def parse_options
|
|||
opts.on('-c') { options[:bytes] = true }
|
||||
end.parse!
|
||||
options = { bytes: true, lines: true, words: true } if options.empty?
|
||||
sources = ARGV.empty? ? ['-'] : ARGV
|
||||
sources = ARGV.empty? ? [''] : ARGV
|
||||
[options, sources]
|
||||
end
|
||||
|
||||
def input_stream_sources(input_sources, options)
|
||||
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)
|
||||
def collect_all_stats(input_sources)
|
||||
input_sources.map do |source|
|
||||
input = source.empty? ? ARGF.read : File.read(source)
|
||||
stats = { lines: input.lines.count, words: input.split.size, bytes: input.bytesize }
|
||||
[source, stats]
|
||||
end
|
||||
max_widths = calculate_max_widths(all_stats, total_stats)
|
||||
|
||||
all_stats.each do |source, stats|
|
||||
result = format_result(stats, max_widths, options)
|
||||
print_result(result, source)
|
||||
end
|
||||
|
||||
print_total(total_stats, max_widths, options) if input_sources.size > 1
|
||||
def calculate_total_stats(all_stats)
|
||||
all_stats.reduce({ lines: 0, words: 0, bytes: 0 }) do |total, (_, stats)|
|
||||
total.merge(stats) { |_, a, b| a + b }
|
||||
end
|
||||
end
|
||||
|
||||
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)
|
||||
(all_stats.map(&:last) + [total_stats]).each_with_object({ lines: 0, words: 0, bytes: 0 }) do |stats, max_widths|
|
||||
max_widths.merge!(stats) { |_, max, stat| [max, stat.to_s.length].max }
|
||||
end
|
||||
max_widths
|
||||
end
|
||||
|
||||
def input_stream(source)
|
||||
if source == '-'
|
||||
count_file_stats(ARGF)
|
||||
else
|
||||
count_file_stats(File.read(source))
|
||||
end
|
||||
end
|
||||
|
||||
def count_file_stats(input)
|
||||
stats = { lines: 0, words: 0, bytes: 0 }
|
||||
input.each_line do |line|
|
||||
stats[:lines] += 1
|
||||
stats[:words] += line.split.size
|
||||
stats[:bytes] += line.bytesize
|
||||
end
|
||||
stats
|
||||
end
|
||||
|
||||
def update_totals(totals, stats)
|
||||
totals.merge(stats) { |_key, total, stat| total + stat }
|
||||
end
|
||||
|
||||
def update_max_widths(max_widths, stats)
|
||||
max_widths.merge(stats) { |_key, max, stat| [max, stat.to_s.length].max }
|
||||
end
|
||||
|
||||
def format_result(stats, max_widths, options)
|
||||
result = {}
|
||||
%i[lines words bytes].each do |key|
|
||||
result[key] = stats[key].to_s.rjust(max_widths[key]) if options[key]
|
||||
end
|
||||
result.values.join(' ')
|
||||
%i[lines words bytes].map { |key| stats[key].to_s.rjust(max_widths[key]) if options[key] }.compact.join(' ')
|
||||
end
|
||||
|
||||
def print_result(result, source)
|
||||
puts "#{result} #{source == '-' ? '' : source}"
|
||||
def print_all_stats(all_stats, max_widths, options)
|
||||
all_stats.each do |source, stats|
|
||||
result = format_result(stats, max_widths, options)
|
||||
puts "#{result} #{source}"
|
||||
end
|
||||
end
|
||||
|
||||
def print_total(total_stats, max_widths, options)
|
||||
total_result = format_result(total_stats, max_widths, options)
|
||||
puts "#{total_result} 合計"
|
||||
end
|
||||
|
||||
options, input_sources = parse_options
|
||||
input_stream_sources(input_sources, options)
|
||||
main
|
||||
|
|
Loading…
Reference in a new issue