一部改修
This commit is contained in:
parent
473407048c
commit
9ca583442a
1 changed files with 12 additions and 13 deletions
25
wc.rb
25
wc.rb
|
@ -3,12 +3,12 @@
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
|
|
||||||
def main
|
def main
|
||||||
options, sources = parse_options
|
options, filenames = parse_options
|
||||||
file_stats = collect_file_stats(sources)
|
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(file_stats + [total_stats])
|
||||||
print_file_stats(file_stats, max_widths, options)
|
print_file_stats(file_stats, max_widths, options)
|
||||||
print_file_stats([{ filename: '合計', **total_stats }], max_widths, options) if sources.size > 1
|
print_file_stats([total_stats], max_widths, options) if filenames.size > 1
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_options
|
def parse_options
|
||||||
|
@ -19,19 +19,19 @@ def parse_options
|
||||||
opts.on('-c') { options[:bytes] = true }
|
opts.on('-c') { options[:bytes] = true }
|
||||||
end.parse!
|
end.parse!
|
||||||
options = { bytes: true, lines: true, words: true } if options.empty?
|
options = { bytes: true, lines: true, words: true } if options.empty?
|
||||||
sources = ARGV.empty? ? [''] : ARGV
|
filenames = ARGV.empty? ? [''] : ARGV
|
||||||
[options, sources]
|
[options, filenames]
|
||||||
end
|
end
|
||||||
|
|
||||||
def collect_file_stats(sources)
|
def collect_file_stats(filenames)
|
||||||
sources.map do |source|
|
filenames.map do |filename|
|
||||||
input = source.empty? ? ARGF.read : File.read(source)
|
input = filename.empty? ? ARGF.read : File.read(filename)
|
||||||
{ filename: source, lines: input.lines.count, words: input.split.size, bytes: input.bytesize }
|
{ filename: 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 = { lines: 0, words: 0, bytes: 0 }
|
total_stats = { filename: '合計', 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,8 +40,7 @@ def calculate_total_stats(file_stats)
|
||||||
total_stats
|
total_stats
|
||||||
end
|
end
|
||||||
|
|
||||||
def calculate_max_widths(file_stats, total_stats)
|
def calculate_max_widths(all_stats)
|
||||||
all_stats = file_stats + [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] = all_stats.map { |stats| stats[key].to_s.length }.max
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue