forked from fjordllc/ruby-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
lsコマンドをオブジェクト指向で作成しました #12
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
s-tone-gs
wants to merge
17
commits into
main
Choose a base branch
from
ls_object
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
17 commits
Select commit
Hold shift + click to select a range
348d712
オブジェクト指向でlsコマンドを作成
s-tone-gs af023c0
テストコードを作成
s-tone-gs b5ae272
メソッドの挙動と命名があっていなかったため修正
s-tone-gs c78d1df
要素の幅とし要素間の空白の幅も含まれていたので分離
s-tone-gs 7add53d
lオプションの役割と命名にずれがあったため修正
s-tone-gs b28b6d4
親クラスのlsが具体的な値を持つのが不自然であったため修正
s-tone-gs 81cc5bf
モジュールで切り出していたメソッドがlsクラスに包括されると判断してリファクタリング
s-tone-gs 50704a0
命名の具体度を上げて修正
s-tone-gs 5ccd7e7
クラス構成を再考
s-tone-gs b71f8f5
typoを修正
s-tone-gs 97ebac8
クラスごとにファイルを分割
s-tone-gs 30c62c4
クラスの役割を見直し
s-tone-gs cf1429c
余計な空白を削除
s-tone-gs 1a45ee7
出力クラスの命名を修正
s-tone-gs e054253
配列の順番に依存しないように修正
s-tone-gs f2bb7fa
ファイル名とクラス名を一致させるよう修正
s-tone-gs 1967c3d
インスタンスの命名をクラスと一致させるように修正
s-tone-gs 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,25 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'optparse' | ||
|
|
||
| class CommandLineOptions | ||
| def initialize | ||
| opt = OptionParser.new | ||
| opt.on('-a') { |v| @all = v } | ||
| opt.on('-r') { |v| @reverse = v } | ||
| opt.on('-l') { |v| @long_format = v } | ||
| opt.parse(ARGV) | ||
| end | ||
|
|
||
| def show_all? | ||
| @all.nil? ? false : @all | ||
| end | ||
|
|
||
| def show_long_format? | ||
| @long_format.nil? ? false : @long_format | ||
| end | ||
|
|
||
| def show_reverse? | ||
| @reverse.nil? ? false : @reverse | ||
| 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,20 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'multi_column_format' | ||
| require_relative 'long_format' | ||
|
|
||
| class FileListPrinter | ||
| def self.run(files, long_format) | ||
| file_list_printer = new(files, long_format) | ||
| puts file_list_printer.generate | ||
| end | ||
|
|
||
| def initialize(files, long_format) | ||
| @files = files | ||
| @long_format = long_format | ||
| end | ||
|
|
||
| def generate | ||
| @long_format ? LongFormat.generate(@files) : MultiColumnFormat.generate(@files) | ||
| 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,67 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'etc' | ||
|
|
||
| class FileMetadata | ||
| FILE_TYPES = { | ||
| '04' => 'd', | ||
| '10' => '-', | ||
| '12' => 'l' | ||
| }.freeze | ||
| PERMISSIONS = { | ||
| '7' => 'rwx', | ||
| '6' => 'rw-', | ||
| '5' => 'r-x', | ||
| '4' => 'r--', | ||
| '3' => '-wx', | ||
| '2' => '-w-', | ||
| '1' => '--x', | ||
| '0' => '---' | ||
| }.freeze | ||
|
|
||
| private_constant :FILE_TYPES, :PERMISSIONS | ||
| attr_reader :name | ||
|
|
||
| def self.get_files(all, reverse) | ||
| flags = all ? File::FNM_DOTMATCH : 0 | ||
| files = Dir.glob('*', flags).map { |file_name| new(file_name) } | ||
| asc_order_files = files.sort_by(&:name) | ||
| reverse ? asc_order_files.reverse : asc_order_files | ||
| end | ||
|
|
||
| def initialize(name) | ||
| @name = name | ||
| @stat = ::File.stat(name) | ||
| end | ||
|
|
||
| def str_mode | ||
| int_mode = @stat.mode.to_s(8).rjust(6, '0') | ||
| FILE_TYPES[int_mode[0..1]] + int_mode[3..5].chars.map { |mode| PERMISSIONS[mode] }.join | ||
| end | ||
|
|
||
| def nlink | ||
| @stat.nlink | ||
| end | ||
|
|
||
| def owner | ||
| uid = @stat.uid | ||
| Etc.getpwuid(uid) | ||
| end | ||
|
|
||
| def group | ||
| gid = @stat.gid | ||
| Etc.getgrgid(gid) | ||
| end | ||
|
|
||
| def size | ||
| @stat.size.to_s | ||
| end | ||
|
|
||
| def blocks | ||
| @stat.blocks | ||
| end | ||
|
|
||
| def mtime | ||
| @stat.mtime.strftime('%b %d %H:%M').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,52 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class LongFormat | ||
| def self.generate(files) | ||
| long_format = new(files) | ||
| column = files.map do |file| | ||
| long_format.generate_content(file) | ||
| end.join("\n") | ||
|
|
||
| [ | ||
| "total #{long_format.total_block_size}", | ||
| column | ||
| ].join("\n") | ||
| end | ||
|
|
||
| def initialize(files) | ||
| @files = files | ||
| end | ||
|
|
||
| def total_block_size | ||
| # rubyは1ブロックを512バイト、Linuxは1ブロックを1024で計算しているため、2で割っている | ||
| @files.map { |file| file.blocks.div(2) }.sum | ||
| end | ||
|
|
||
| def generate_content(file) | ||
| content_widths = calc_widths | ||
| [ | ||
| file.str_mode, | ||
| file.nlink.to_s.rjust(content_widths[:nlink]), | ||
| file.owner.name.rjust(content_widths[:owner]), | ||
| file.group.name.rjust(content_widths[:group]), | ||
| file.size.rjust(content_widths[:size]), | ||
| file.mtime.rjust(content_widths[:mtime]), | ||
| file.name | ||
| ].join(' ') | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def calc_widths | ||
| widths = Hash.new { |hash, key| hash[key] = [] } | ||
| @files.each do |f| | ||
| widths[:owner].push(f.owner.name.length) | ||
| widths[:group].push(f.group.name.length) | ||
| widths[:nlink].push(f.nlink.to_s.length) | ||
| widths[:size].push(f.size.length) | ||
| widths[:mtime].push(f.mtime.length) | ||
| widths[:name].push(f.name.length) | ||
| end | ||
| widths.transform_values(&:max) | ||
| 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,15 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative 'command_line_options' | ||
| require_relative 'file_metadata' | ||
| require_relative 'file_list_printer' | ||
|
|
||
| class Ls | ||
| def self.run | ||
| options = CommandLineOptions.new | ||
| files = FileMetadata.get_files(options.show_all?, options.show_reverse?) | ||
| FileListPrinter.run(files, options.show_long_format?) | ||
| end | ||
| end | ||
|
|
||
| Ls.run |
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,52 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class MultiColumnFormat | ||
| COLUMN_COUNT = 3 | ||
|
|
||
| def self.generate(files) | ||
| multi_column_format = new(files) | ||
| matrixed_files = multi_column_format.build_matrix | ||
| multi_column_format.generate_rows(matrixed_files) | ||
| end | ||
|
|
||
| def initialize(files) | ||
| @files = files | ||
| end | ||
|
|
||
| def build_matrix | ||
| (0...row_count).map do |i| | ||
| index_to_row_value = i | ||
| (0...COLUMN_COUNT).map do | ||
| index_to_target = index_to_row_value | ||
| index_to_row_value += row_count | ||
| @files[index_to_target] | ||
| end.compact | ||
| end | ||
| end | ||
|
|
||
| def generate_rows(matrixed_files) | ||
| matrixed_files.map do |files| | ||
| generate_row(files) | ||
| end.join("\n") | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def name_width | ||
| @files.map { |file| file.name.length }.max | ||
| end | ||
|
|
||
| def row_count | ||
| @files.count.ceildiv(COLUMN_COUNT) | ||
| end | ||
|
|
||
| def generate_row(files) | ||
| files.map do |file| | ||
| generate_content(file) | ||
| end.join(' ') | ||
| end | ||
|
|
||
| def generate_content(file) | ||
| file.name.ljust(name_width) | ||
| 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 @@ | ||
| この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大き |
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 @@ | ||
| ダミーです |
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,2 @@ | ||
| あなたの名前は何ですか | ||
| 私の名前は |
Empty file.
Empty file.
Empty file.
Empty file.
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.
ファイル名はクラス名と揃えましょう。
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.
修正しました。