wc
This commit is contained in:
parent
9d33db0136
commit
b712e9bf5f
1 changed files with 61 additions and 43 deletions
90
wc.rb
90
wc.rb
|
@ -2,54 +2,72 @@
|
||||||
|
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
|
|
||||||
total_lines = 0
|
def parse_options
|
||||||
total_words = 0
|
options = {}
|
||||||
total_bytes = 0
|
OptionParser.new do |opts|
|
||||||
|
|
||||||
options = {}
|
|
||||||
OptionParser.new do |opts|
|
|
||||||
opts.on('-l') { options[:lines] = true }
|
opts.on('-l') { options[:lines] = true }
|
||||||
opts.on('-w') { options[:words] = true }
|
opts.on('-w') { options[:words] = true }
|
||||||
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?
|
||||||
input_sources = ARGV.empty? ? [ARGF] : ARGV
|
[options, ARGV.empty? ? [ARGF] : ARGV]
|
||||||
|
end
|
||||||
input_sources.each do |source|
|
|
||||||
lines = 0
|
|
||||||
words = 0
|
|
||||||
bytes = 0
|
|
||||||
|
|
||||||
begin
|
|
||||||
input = source == ARGF ? ARGF.read : File.read(source)
|
|
||||||
|
|
||||||
|
def count_file_stats(input)
|
||||||
|
lines = words = bytes = 0
|
||||||
input.each_line do |line|
|
input.each_line do |line|
|
||||||
lines += 1
|
lines += 1
|
||||||
words += line.split.size
|
words += line.split.size
|
||||||
bytes += line.bytesize
|
bytes += line.bytesize
|
||||||
end
|
end
|
||||||
|
[lines, words, bytes]
|
||||||
|
end
|
||||||
|
|
||||||
total_lines += lines
|
def process_input(source)
|
||||||
total_words += words
|
if source == ARGF || File.exist?(source)
|
||||||
total_bytes += bytes
|
input = source == ARGF ? ARGF.read : File.read(source)
|
||||||
|
count_file_stats(input)
|
||||||
options = { bytes: true, lines: true, words: true } if options.empty?
|
else
|
||||||
result = []
|
|
||||||
result << lines if options[:lines]
|
|
||||||
result << words if options[:words]
|
|
||||||
result << bytes if options[:bytes]
|
|
||||||
|
|
||||||
puts "#{result.join(' ')} #{source == ARGF ? '' : source}"
|
|
||||||
rescue Errno::ENOENT
|
|
||||||
puts "wc: #{source}: そのようなファイルやディレクトリはありません"
|
puts "wc: #{source}: そのようなファイルやディレクトリはありません"
|
||||||
|
exit
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if input_sources.size > 1
|
def update_totals(totals, stats)
|
||||||
total_result = []
|
totals.zip(stats).map { |total, stat| total + stat }
|
||||||
total_result << total_lines if options[:lines]
|
|
||||||
total_result << total_words if options[:words]
|
|
||||||
total_result << total_bytes if options[:bytes]
|
|
||||||
|
|
||||||
puts "#{total_result.join(' ')} 合計"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_max_widths(max_widths, stats)
|
||||||
|
max_widths.zip(stats).map { |max, stat| [max, stat.to_s.length].max }
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_result(stats, max_widths, options)
|
||||||
|
result = []
|
||||||
|
%i[lines words bytes].each_with_index do |key, index|
|
||||||
|
result << stats[index].to_s.rjust(max_widths[index]) if options[key]
|
||||||
|
end
|
||||||
|
result.join(' ')
|
||||||
|
end
|
||||||
|
|
||||||
|
def print_result(result, source)
|
||||||
|
puts "#{result} #{source == ARGF ? '' : source}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def print_total(total_stats, max_widths, options)
|
||||||
|
total_result = format_result(total_stats, max_widths, options)
|
||||||
|
puts "#{total_result} 合計"
|
||||||
|
end
|
||||||
|
|
||||||
|
options, input_sources = parse_options
|
||||||
|
total_stats = [0, 0, 0]
|
||||||
|
max_widths = [0, 0, 0]
|
||||||
|
|
||||||
|
input_sources.each do |source|
|
||||||
|
stats = process_input(source)
|
||||||
|
total_stats = update_totals(total_stats, stats)
|
||||||
|
max_widths = update_max_widths(max_widths, 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
|
||||||
|
|
Loading…
Reference in a new issue