右寄せの処理を修正
This commit is contained in:
parent
d3fbc051bd
commit
8197b50a57
1 changed files with 17 additions and 8 deletions
25
wc.rb
25
wc.rb
|
@ -1,3 +1,4 @@
|
|||
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'optparse'
|
||||
|
@ -15,20 +16,28 @@ def parse_options
|
|||
end
|
||||
|
||||
def input_stream_sources(input_sources, options)
|
||||
total_stats = { lines: 0, words: 0, bytes: 0 }
|
||||
max_widths = { lines: 0, words: 0, bytes: 0 }
|
||||
|
||||
input_sources.each do |source|
|
||||
stats = input_stream(source)
|
||||
total_stats = update_totals(total_stats, stats)
|
||||
max_widths = update_max_widths(max_widths, stats)
|
||||
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|
|
||||
result = format_result(stats, max_widths, options)
|
||||
print_result(result, source)
|
||||
end
|
||||
|
||||
|
||||
print_total(total_stats, max_widths, options) if input_sources.size > 1
|
||||
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)
|
||||
end
|
||||
max_widths
|
||||
end
|
||||
|
||||
def input_stream(source)
|
||||
if source == '-'
|
||||
count_file_stats(ARGF)
|
||||
|
|
Loading…
Reference in a new issue