forked from fjordllc/ruby-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
ls コマンドを作るオブジェクト指向版 の課題提出 #11
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
yokomaru
wants to merge
41
commits into
main
Choose a base branch
from
my-ls-object-main
base: main
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.
Open
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
76316a4
first commit
yokomaru db6a683
add: Ls and Test
yokomaru e33b543
add: a and r options
yokomaru 8df64dd
fix: class name ls_file to file_data
yokomaru 31006e3
add: l option and test
yokomaru 9f1202d
fix: remove FileStatus from FileData
yokomaru 0a23a16
fix: display
yokomaru 52a7e6b
add: 一旦commandパターンで作成
yokomaru 56381d8
add: options
yokomaru e9a7794
fix: FileStatus and move directory
yokomaru 65df832
fix: ls.rb
yokomaru 99c65ae
fix: test
yokomaru 1f242e4
fix: apply rubocop error check
yokomaru cec0f05
fix: execute
yokomaru 3905060
add: width
yokomaru 66d2082
fix: format
yokomaru bd46011
add: format class
yokomaru 522334e
fix: delete require and test and line break
yokomaru f25684b
fix: move total_block to long_format
yokomaru 981a9f4
delete: unnecessary attr_reader
yokomaru 48b911e
fix: delete line break
yokomaru 7540509
fix: move build_file_status to file_data from file_status
yokomaru 94d606d
update: argment name
yokomaru 89c8670
fix: render
yokomaru 8bd1de4
update: test
yokomaru f2c5f56
fix: require
yokomaru 589fa08
add: require 'io/console'
yokomaru 534db11
add: instance variable short_format_data
yokomaru 6490225
fix: variable name
yokomaru 998c721
fix: longformat test
yokomaru ea4d055
delete: Option classes
yokomaru ddaeb3c
fix: rename Format to Formatter
yokomaru a99e31f
fix: integrate FileData and FileStatus
yokomaru 433e8ec
delete: test_file.txt
yokomaru ed15658
fix: rename method name at Formatter
yokomaru 645681b
fix: rename method name at LsCommand
yokomaru b1254f5
add: special permission
yokomaru 71ece3b
add: filetype
yokomaru 1dfdf57
fix: test symbolic link
yokomaru 10f613b
fix: make symbolic link
yokomaru 16634c1
fix: special permission
yokomaru 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 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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #!/usr/bin/env ruby | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'io/console' | ||
| require 'optparse' | ||
| require_relative '../lib/ls_command' | ||
|
|
||
| if __FILE__ == $PROGRAM_NAME | ||
| opt = OptionParser.new | ||
|
|
||
| params = { dot_match: false } | ||
| opt.on('-a') { |v| params[:dot_match] = v } | ||
| opt.on('-r') { |v| params[:reverse] = v } | ||
| opt.on('-l') { |v| params[:long_format] = v } | ||
| opt.parse!(ARGV) | ||
| path = ARGV[0] || '.' | ||
| width = IO.console.winsize[1] | ||
|
|
||
| ls = LsCommand.new(path, width:, **params) | ||
| puts ls.formatted_output | ||
| end |
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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #!/usr/bin/env ruby | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'etc' | ||
| require 'pathname' | ||
|
|
||
| class FileData | ||
| attr_reader :name, :file_status | ||
|
|
||
| PERMISSION_START_POSITION = 7 | ||
| PERMISSION_END_POSITION = 9 | ||
|
|
||
| FILE_TYPE = { | ||
| 'fifo' => 'p', | ||
| 'characterSpecial' => 'c', | ||
| 'directory' => 'd', | ||
| 'blockSpecial' => 'b', | ||
| 'file' => '-', | ||
| 'link' => 'l', | ||
| 'socket' => 's' | ||
| }.freeze | ||
|
|
||
| SPECIAL_PERMISSION = { | ||
| 0 => 's', | ||
| 1 => 's', | ||
| 2 => 't' | ||
| }.freeze | ||
|
|
||
| PERMISSION_TYPE = { | ||
| '000' => '---', | ||
| '001' => '--x', | ||
| '010' => '-w-', | ||
| '011' => '-wx', | ||
| '100' => 'r--', | ||
| '101' => 'r-x', | ||
| '110' => 'rw-', | ||
| '111' => 'rwx' | ||
| }.freeze | ||
|
|
||
| def initialize(name, path) | ||
| @name = name | ||
| @full_path = Pathname(File.absolute_path(@name, path)) | ||
| @file_type = File.ftype(@full_path) | ||
| @file_status = build_file_status(File.lstat(@full_path)) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def build_file_status(status) | ||
| { | ||
| type: FILE_TYPE[status.ftype], | ||
| mode: mode(status), | ||
| hardlink_nums: status.nlink.to_s, | ||
| owner_name: Etc.getpwuid(status.uid).name, | ||
| group_name: Etc.getgrgid(status.gid).name, | ||
| bytesize: rdev_or_bytesize(status), | ||
| latest_modify_datetime: status.mtime.strftime('%_m %e %H:%M'), | ||
| filename: file_name(status), | ||
| blocks: status.blocks | ||
| } | ||
| end | ||
|
|
||
| def file_name(status) | ||
| status.symlink? ? "#{@name} -> #{File.readlink(@full_path)}" : @name | ||
| end | ||
|
|
||
| def mode(status) | ||
| mode_binary_numbers = status.mode.to_s(2).rjust(16, '0') | ||
| # SUID、SGID、STICKEYBITの順番で特殊権限をチェック | ||
| mode_binary_numbers[4..6].each_char.with_index.map do |special_permission, i| | ||
| range_start = PERMISSION_START_POSITION + (3 * i) | ||
| range_end = PERMISSION_END_POSITION + (3 * i) | ||
| permission = PERMISSION_TYPE[mode_binary_numbers[range_start..range_end]].dup | ||
| permission[2] = permission[2] == 'x' ? SPECIAL_PERMISSION[i] : SPECIAL_PERMISSION[i].upcase if special_permission == '1' | ||
| permission | ||
| end.join | ||
| end | ||
|
|
||
| def rdev_or_bytesize(status) | ||
| %w[characterSpecial blockSpecial].include?(@file_type) ? format('%#01x', status.rdev.to_s(10)) : status.size.to_s | ||
| end | ||
| end | ||
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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Formatter | ||
| def initialize(files) | ||
| @files = files | ||
| end | ||
|
|
||
| def format; end | ||
| end |
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'formatter' | ||
|
|
||
| class LongFormatter < Formatter | ||
| def initialize(files, path) | ||
| super(files) | ||
| @path = path | ||
| @max_sizes = build_max_sizes | ||
| @total_block = sum_blocks | ||
| @long_format_data = build_long_format_data | ||
| end | ||
|
|
||
| def format | ||
| total = "total #{@total_block}" if File.directory?(@path) || @files.size > 1 | ||
| [total, *@long_format_data].compact.join("\n") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def build_long_format_data | ||
| @files.map { |file| format_row(file.file_status, *@max_sizes) } | ||
| end | ||
|
|
||
| def build_max_sizes | ||
| %i[hardlink_nums owner_name group_name bytesize].map do |key| | ||
| find_max_size(key) | ||
| end | ||
| end | ||
|
|
||
| def find_max_size(key) | ||
| @files.map { |file| file.file_status[key].size }.max | ||
| end | ||
|
|
||
| def format_row(status, max_nlink, max_user, max_group, max_size) | ||
| [ | ||
| "#{status[:type]}#{status[:mode]}", | ||
| " #{status[:hardlink_nums].rjust(max_nlink)}", | ||
| " #{status[:owner_name].ljust(max_user)}", | ||
| " #{status[:group_name].ljust(max_group)}", | ||
| " #{status[:bytesize].rjust(max_size)}", | ||
| " #{status[:latest_modify_datetime]}", | ||
| " #{status[:filename]}" | ||
| ].join | ||
| end | ||
|
|
||
| def sum_blocks | ||
| @files.sum { |file| file.file_status[:blocks] } | ||
| end | ||
| end |
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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'formatter' | ||
|
|
||
| class ShortFormatter < Formatter | ||
| def initialize(files, width) | ||
| super(files) | ||
| @width = width | ||
| @max_file_name = @files.map { |file| File.basename(file.name).size }.max | ||
| @col_count = calculate_col_count | ||
| @row_count = calculate_row_count | ||
| @short_format_data = build_short_format_data | ||
| end | ||
|
|
||
| def format | ||
| @short_format_data.join("\n") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def build_short_format_data | ||
| safe_transpose.map { |row_files| build_short_format_row(row_files) } | ||
| end | ||
|
|
||
| def build_short_format_row(row_files) | ||
| row_files.map do |file| | ||
| basename = file ? File.basename(file.name) : '' | ||
| basename.ljust(@max_file_name + 1) | ||
| end.join.rstrip | ||
| end | ||
|
|
||
| def calculate_col_count | ||
| @width / (@max_file_name + 1) | ||
| end | ||
|
|
||
| def calculate_row_count | ||
| @col_count.zero? ? @files.count : (@files.count.to_f / @col_count).ceil | ||
| end | ||
|
|
||
| def safe_transpose | ||
| nested_file_names = @files.each_slice(@row_count).to_a | ||
| nested_file_names[0].zip(*nested_file_names[1..]) | ||
| end | ||
| end |
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 |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'file/file_data' | ||
| require_relative 'format/long_formatter' | ||
| require_relative 'format/short_formatter' | ||
|
|
||
| class LsCommand | ||
| def initialize(path, width: 80, dot_match: false, reverse: false, long_format: false) | ||
| @path = path | ||
| @width = width | ||
| @dot_match = dot_match | ||
| @reverse = reverse | ||
| @long_format = long_format | ||
| @files = build_dot_match_and_sorted_files | ||
| end | ||
|
|
||
| def formatted_output | ||
| @long_format ? LongFormatter.new(@files, @path).format : ShortFormatter.new(@files, @width).format | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def build_dot_match_and_sorted_files | ||
| files = generate_files | ||
| matched_files = @dot_match ? files : files.filter { |file| !/^\./.match?(file.name) } | ||
| @reverse ? matched_files.sort_by(&:name).reverse : matched_files.sort_by(&:name) | ||
| end | ||
|
|
||
| def generate_files | ||
| if File.directory?(@path) | ||
| Dir.open(@path).entries.map { |name| FileData.new(name, @path) } | ||
| else | ||
| [FileData.new(@path, File.dirname(@path))] | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
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.
こちらと
FileStatusを分ける意義があまり感じられませんでした。ファイルのメタデータを扱うクラスが一つあれば十分かなと思いました。どうでしょうか。