-
Notifications
You must be signed in to change notification settings - Fork 0
wcコマンドを作成 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
wcコマンドを作成 #10
Changes from all commits
65c31b5
f16378f
5fd8ba8
581b8fc
1ee4ccd
77ea30e
44beccb
c0ac463
0eed6c8
90934e9
ea6443d
3d09423
0506db2
07680e4
f38807f
c0dd417
18cd3cf
e1fceb9
bc05889
740c96f
1ac3773
d31e9b1
e11ccf9
fa413ca
200be15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||||||
| # frozen_string_literal: true | ||||||||||
|
|
||||||||||
| require 'optparse' | ||||||||||
|
|
||||||||||
| def main | ||||||||||
| column_names, paths = column_names_and_paths | ||||||||||
| statistics_matrix = | ||||||||||
| if paths.empty? | ||||||||||
| [build_statistics_row($stdin.read)] | ||||||||||
| else | ||||||||||
| build_statistics_matrix(paths) | ||||||||||
| end | ||||||||||
| width = calculate_output_width(statistics_matrix, column_names) | ||||||||||
| statistics_matrix.each do |statistics| | ||||||||||
| render(statistics, column_names, width) | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def column_names_and_paths | ||||||||||
| line = false | ||||||||||
| word = false | ||||||||||
| size = false | ||||||||||
| opt = OptionParser.new | ||||||||||
| opt.on('-l') { |v| line = v } | ||||||||||
| opt.on('-w') { |v| word = v } | ||||||||||
| opt.on('-c') { |v| size = v } | ||||||||||
| paths = opt.parse(ARGV) | ||||||||||
| column_names = [line, word, size].none? ? %i[line_count word_count size] : { line_count: line, word_count: word, size: size }.select { |_, v| v }.keys | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ちょっと横に長すぎる気がしますね。改行した方が読みやすいかも。
Suggested change
|
||||||||||
| [column_names, paths] | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def build_statistics_row(input, name = nil) | ||||||||||
| { | ||||||||||
| line_count: input.lines.count, | ||||||||||
| word_count: input.split.count, | ||||||||||
| size: input.size, | ||||||||||
| name: name | ||||||||||
| } | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def build_statistics_matrix(paths) | ||||||||||
| statistics_matrix = paths.map do |path| | ||||||||||
| build_statistics_row(File.read(path), path) | ||||||||||
| end | ||||||||||
| statistics_matrix << calculate_total(statistics_matrix) if paths.size > 1 | ||||||||||
| statistics_matrix | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def calculate_total(statistics_matrix) | ||||||||||
| line_count_sum = 0 | ||||||||||
| word_count_sum = 0 | ||||||||||
| size_sum = 0 | ||||||||||
| statistics_matrix.each do |statistics| | ||||||||||
| line_count_sum += statistics[:line_count] | ||||||||||
| word_count_sum += statistics[:word_count] | ||||||||||
| size_sum += statistics[:size] | ||||||||||
| end | ||||||||||
| { line_count: line_count_sum, word_count: word_count_sum, size: size_sum, name: 'total' } | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def calculate_output_width(statistics_matrix, column_names) | ||||||||||
| widths = statistics_matrix.map do |statistics| | ||||||||||
| column_names.map do |column_name| | ||||||||||
| statistics[column_name].to_s.length | ||||||||||
| end | ||||||||||
| end | ||||||||||
| widths.flatten.max | ||||||||||
| end | ||||||||||
|
|
||||||||||
| def render(statistics, column_names, width) | ||||||||||
| filterd_statistics = column_names.map do |column_name| | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. filterd -> filtered ですね。 ここでやってるのは見た目の調整だと思うので、formatted とかの方が良さそうです |
||||||||||
| statistics[column_name].to_s.rjust(width) | ||||||||||
| end | ||||||||||
| puts [*filterd_statistics, statistics[:name]].join(' ') | ||||||||||
| end | ||||||||||
|
|
||||||||||
| main | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ARGVを直接記述しました