さらに修正
This commit is contained in:
parent
9ca583442a
commit
16e5867588
1 changed files with 6 additions and 6 deletions
12
wc.rb
12
wc.rb
|
@ -6,7 +6,7 @@ def main
|
||||||
options, filenames = parse_options
|
options, filenames = parse_options
|
||||||
file_stats = collect_file_stats(filenames)
|
file_stats = collect_file_stats(filenames)
|
||||||
total_stats = calculate_total_stats(file_stats)
|
total_stats = calculate_total_stats(file_stats)
|
||||||
max_widths = calculate_max_widths(file_stats + [total_stats])
|
max_widths = calculate_max_widths(total_stats)
|
||||||
print_file_stats(file_stats, max_widths, options)
|
print_file_stats(file_stats, max_widths, options)
|
||||||
print_file_stats([total_stats], max_widths, options) if filenames.size > 1
|
print_file_stats([total_stats], max_widths, options) if filenames.size > 1
|
||||||
end
|
end
|
||||||
|
@ -26,12 +26,12 @@ end
|
||||||
def collect_file_stats(filenames)
|
def collect_file_stats(filenames)
|
||||||
filenames.map do |filename|
|
filenames.map do |filename|
|
||||||
input = filename.empty? ? ARGF.read : File.read(filename)
|
input = filename.empty? ? ARGF.read : File.read(filename)
|
||||||
{ filename: filename, lines: input.lines.count, words: input.split.size, bytes: input.bytesize }
|
{ filenames: filename, lines: input.lines.count, words: input.split.size, bytes: input.bytesize }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def calculate_total_stats(file_stats)
|
def calculate_total_stats(file_stats)
|
||||||
total_stats = { filename: '合計', lines: 0, words: 0, bytes: 0 }
|
total_stats = { filenames: '合計', lines: 0, words: 0, bytes: 0 }
|
||||||
file_stats.each do |stats|
|
file_stats.each do |stats|
|
||||||
total_stats[:lines] += stats[:lines]
|
total_stats[:lines] += stats[:lines]
|
||||||
total_stats[:words] += stats[:words]
|
total_stats[:words] += stats[:words]
|
||||||
|
@ -40,9 +40,9 @@ def calculate_total_stats(file_stats)
|
||||||
total_stats
|
total_stats
|
||||||
end
|
end
|
||||||
|
|
||||||
def calculate_max_widths(all_stats)
|
def calculate_max_widths(total_stats)
|
||||||
%i[lines words bytes].each_with_object({}) do |key, max_widths|
|
%i[lines words bytes].each_with_object({}) do |key, max_widths|
|
||||||
max_widths[key] = all_stats.map { |stats| stats[key].to_s.length }.max
|
max_widths[key] = total_stats[key].to_s.length
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ end
|
||||||
def print_file_stats(file_stats, max_widths, options)
|
def print_file_stats(file_stats, max_widths, options)
|
||||||
file_stats.each do |stats|
|
file_stats.each do |stats|
|
||||||
result = format_result(stats, max_widths, options)
|
result = format_result(stats, max_widths, options)
|
||||||
puts "#{result} #{stats[:filename]}"
|
puts [result, stats[:filenames]].join(' ')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue