さすがにこれで完成ということにしたい
This commit is contained in:
parent
d24ae97dd9
commit
66768a11ed
1 changed files with 22 additions and 20 deletions
42
wc.rb
42
wc.rb
|
@ -4,12 +4,12 @@ require 'optparse'
|
||||||
|
|
||||||
def main
|
def main
|
||||||
options, filenames = parse_options
|
options, filenames = parse_options
|
||||||
file_details = collect_file_stats(filenames)
|
file_stats = collect_file_stats(filenames)
|
||||||
total_stats = calculate_total_stats(file_details)
|
total_stat = calculate_total_stat(file_stats)
|
||||||
max_widths = calculate_max_widths(total_stats)
|
max_widths = calculate_max_widths(total_stat)
|
||||||
|
|
||||||
file_details.each { |file_stats| print_stats(file_stats, max_widths, options) }
|
file_stats << total_stat if filenames.size > 1
|
||||||
print_stats(total_stats, max_widths, options) if filenames.size > 1
|
file_stats.each { |file_stat| puts format_result(file_stat, max_widths, options).join(' ') }
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_options
|
def parse_options
|
||||||
|
@ -27,32 +27,34 @@ end
|
||||||
def collect_file_stats(filenames)
|
def collect_file_stats(filenames)
|
||||||
filenames.map do |filename|
|
filenames.map do |filename|
|
||||||
text = filename.empty? ? ARGF.read : File.read(filename)
|
text = filename.empty? ? ARGF.read : File.read(filename)
|
||||||
{ filename: filename, lines: text.lines.count, words: text.split.size, bytes: text.bytesize }
|
{
|
||||||
|
filename: filename,
|
||||||
|
lines: text.lines.count,
|
||||||
|
words: text.split.size,
|
||||||
|
bytes: text.bytesize
|
||||||
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def calculate_total_stats(file_details)
|
def calculate_total_stat(file_stats)
|
||||||
total_stats = { filename: '合計', lines: 0, words: 0, bytes: 0 }
|
total_stat = { filename: '合計', lines: 0, words: 0, bytes: 0 }
|
||||||
file_details.each do |stats|
|
file_stats.each do |stats|
|
||||||
total_stats[:lines] += stats[:lines]
|
total_stat[:lines] += stats[:lines]
|
||||||
total_stats[:words] += stats[:words]
|
total_stat[:words] += stats[:words]
|
||||||
total_stats[:bytes] += stats[:bytes]
|
total_stat[:bytes] += stats[:bytes]
|
||||||
end
|
end
|
||||||
total_stats
|
total_stat
|
||||||
end
|
end
|
||||||
|
|
||||||
def calculate_max_widths(total_stats)
|
def calculate_max_widths(total_stat)
|
||||||
%i[lines words bytes].to_h { |key| [key, total_stats[key].to_s.length] }
|
%i[lines words bytes].to_h { |key| [key, total_stat[key].to_s.length] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_result(stats, max_widths, options)
|
def format_result(stats, max_widths, options)
|
||||||
%i[lines words bytes].filter_map do |key|
|
result = %i[lines words bytes].filter_map do |key|
|
||||||
stats[key].to_s.rjust(max_widths[key]) if options[key]
|
stats[key].to_s.rjust(max_widths[key]) if options[key]
|
||||||
end
|
end
|
||||||
end
|
result << stats[:filename]
|
||||||
|
|
||||||
def print_stats(stats, max_widths, options)
|
|
||||||
puts (format_result(stats, max_widths, options) << stats[:filename]).join(' ')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
main
|
main
|
||||||
|
|
Loading…
Reference in a new issue