forked from fjordllc/ruby-practices
-
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
Open
blacktoad30
wants to merge
10
commits into
my-wc
Choose a base branch
from
my-wc-fix--simplify
base: my-wc
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+97
−217
Open
wcコマンドの修正 #10
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cb2cdc9
fix: Merge into one file
blacktoad30 85c5b63
refactor: word_count
blacktoad30 a00bce0
Merge branch 'my-wc-fix--simplify-word_count' into my-wc-fix--simplify
blacktoad30 3fb9b6a
fix: 引数解析の結果をなるべくいじらない
blacktoad30 1cd1c23
Merge branch 'my-wc-fix--simplify-parse_args' into my-wc-fix--simplify
blacktoad30 957298f
refactor: Make some code easier to understand
blacktoad30 b024333
refactor: Use ARGV
blacktoad30 e9ade88
refactor: Use `String` for counting
blacktoad30 e1ef5fe
fix: Treat '-' as filename
blacktoad30 1dee3a8
fix: Do not use `Kernel.#format` to format output
blacktoad30 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,102 @@ | ||
| #!/usr/bin/env ruby | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative './lib/wc_methods' | ||
| require 'optparse' | ||
|
|
||
| errno = main(ARGV) | ||
| OPTION_STRING = 'lwc' | ||
| DEFAULT_OPTION_CHARS = OPTION_STRING.chars.freeze | ||
|
|
||
| exit(errno) | ||
| def main | ||
| options, paths = parse_commandline_options | ||
|
|
||
| option_chars = extract_option_chars(options) | ||
| counts = paths.empty? ? [count_newline_word_bytesize] : paths.map { count_newline_word_bytesize(_1) } | ||
|
|
||
| print_counts(counts, option_chars) | ||
|
|
||
| 0 | ||
| end | ||
|
|
||
| def parse_commandline_options | ||
| options = OptionParser.getopts(ARGV, OPTION_STRING) | ||
|
|
||
| [options, ARGV] | ||
| end | ||
|
|
||
| def extract_option_chars(options) | ||
| option_chars = options.filter_map { |opt, bool| opt if bool } | ||
|
|
||
| # `wc [file ...]` == `wc -lwc [file ...]` | ||
| option_chars.empty? ? DEFAULT_OPTION_CHARS : option_chars | ||
| end | ||
|
|
||
| def count_newline_word_bytesize(path = '') | ||
| buf = path.empty? ? $stdin.set_encoding('ASCII-8BIT').read : File.open(path, encoding: 'ASCII-8BIT', &:read) | ||
| count = {} | ||
|
|
||
| count['l'] = count_newline(buf) | ||
| count['w'] = count_word(buf) | ||
| count['c'] = count_bytesize(buf) | ||
|
|
||
| { path:, count: } | ||
| end | ||
|
|
||
| def count_newline(buf) | ||
| buf.count("\n") | ||
| end | ||
|
|
||
| def count_word(buf) | ||
| num = 0 | ||
| buf.split { num += 1 if _1.match?(/[[:graph:]]/) } | ||
| num | ||
| end | ||
|
|
||
| def count_bytesize(buf) | ||
| buf.bytesize | ||
| end | ||
|
|
||
| def print_counts(counts, option_chars) | ||
| digit = calc_digit(counts, option_chars) | ||
| need_with_path = !stdin?(counts) | ||
|
|
||
| counts << total_counts(counts) if counts.size >= 2 | ||
|
|
||
| counts.each do |result| | ||
| counts = result[:count].values_at(*option_chars).map { _1.to_s.rjust(digit) } | ||
|
|
||
| counts << result[:path] if need_with_path | ||
|
|
||
| puts counts.join(' ') | ||
| end | ||
| end | ||
|
|
||
| def calc_digit(counts, option_chars) | ||
| need_padding = option_chars.size >= 2 || counts.size >= 2 | ||
|
|
||
| return 1 unless need_padding | ||
| return 7 if stdin?(counts) | ||
|
|
||
| counts.sum { _1[:count]['c'] }.to_s.size | ||
| end | ||
|
|
||
| def stdin?(counts) | ||
| counts.size == 1 && counts.first[:path].empty? | ||
| end | ||
|
|
||
| def total_counts(counts) | ||
| init_value_for_total = DEFAULT_OPTION_CHARS.to_h { [_1, 0] } | ||
|
|
||
| count_total = counts.each_with_object(init_value_for_total) do |result, total| | ||
| DEFAULT_OPTION_CHARS.each do |option| | ||
| total[option] += result[:count][option] | ||
| end | ||
| end | ||
|
|
||
| { path: 'total', count: count_total } | ||
| end | ||
|
|
||
| if __FILE__ == $PROGRAM_NAME | ||
| errno = main | ||
|
|
||
| exit(errno) | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
AVAILABLE_OPTION_CHARSとかですかね。下のDEFAULT_OPTION_CHARSと同じOPTION_CHARSだとわかるほうがよいかなと思います。