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