From 865fcd9949e8aed3c05afb515167bf2e8006c5f5 Mon Sep 17 00:00:00 2001 From: matsuyama Date: Thu, 11 Apr 2024 23:23:41 +0900 Subject: [PATCH 01/14] =?UTF-8?q?Ls=E3=82=B3=E3=83=9E=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=83=97=E3=83=AD=E3=82=B0=E3=83=A9=E3=83=A0=E3=82=92=E4=BD=9C?= =?UTF-8?q?=E6=88=90=E3=80=82=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E7=84=A1=E3=81=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/basic_format.rb | 40 +++++++++++++++++++++++++ 07.ls_object/file_info.rb | 57 ++++++++++++++++++++++++++++++++++++ 07.ls_object/ls.rb | 6 ++++ 07.ls_object/ls_command.rb | 24 +++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 07.ls_object/basic_format.rb create mode 100644 07.ls_object/file_info.rb create mode 100644 07.ls_object/ls.rb create mode 100644 07.ls_object/ls_command.rb diff --git a/07.ls_object/basic_format.rb b/07.ls_object/basic_format.rb new file mode 100644 index 0000000000..6e4ff53291 --- /dev/null +++ b/07.ls_object/basic_format.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require_relative 'file_info' + +class BasicFormat + COLUMN_NUM = 3 + + def initialize(file_paths) + @files = file_paths.map { |file_path| FileInfo.new(file_path) } + end + + def format_lines + files_grouped_into_lines.map do |line| + line.map do |file| + file_name = file.base_name + file_name.ljust(max_length) + end.join(' ') + end + end + + private + + def files_grouped_into_lines + total_line_count = (@files.size.to_f / COLUMN_NUM).ceil + files_sliced = @files.each_slice(total_line_count).to_a + + lines = Array.new(total_line_count) { [] } + lines.each_with_index do |line, index| + files_sliced.each do |files| + line << files[index] if files[index] + end + end + + lines + end + + def max_length + @files.map { |file| file.base_name.size }.max + end +end diff --git a/07.ls_object/file_info.rb b/07.ls_object/file_info.rb new file mode 100644 index 0000000000..d7c9ea3a5a --- /dev/null +++ b/07.ls_object/file_info.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +class FileInfo + def initialize(file_path) + @file_path = file_path + end + + def base_name + File.basename(@file_path) + end + + def type + file_lstat = File.lstat(@file_path) + if file_lstat.directory? + 'd' + elsif file_lstat.symlink? + 'l' + else + '-' + end + end + + def mode + file_stat.mode.to_s(8).slice(-3, 3).chars.to_a.map { |n| PERMISSIONS[n] }.join + end + + def link_num + file_stat.nlink + end + + def size + file_stat.size + end + + def user + Etc.getpwuid(file_stat.uid).name + end + + def group + Etc.getgrgid(file_stat.gid).name + end + + def last_access_time + file_stat.mtime.strftime('%_m %_d %H:%M') + end + + def symlink? + mode_num = File.lstat(@file_path).mode.to_s(8) + mode_num[0, 2] == '12' + end + + private + + def file_stat + File::Stat.new(@file_path) + end +end diff --git a/07.ls_object/ls.rb b/07.ls_object/ls.rb new file mode 100644 index 0000000000..ae81b32dd7 --- /dev/null +++ b/07.ls_object/ls.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +require_relative 'ls_command' + +path = ARGV[0] || '.' +LsCommand.new(path).display diff --git a/07.ls_object/ls_command.rb b/07.ls_object/ls_command.rb new file mode 100644 index 0000000000..de7f2baa6c --- /dev/null +++ b/07.ls_object/ls_command.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require_relative 'basic_format' + +class LsCommand + def initialize(path) + @path = path + end + + def display + puts formatter.format_lines + end + + private + + def file_paths + file_pattern = File.join(@path, '*') + Dir.glob(file_pattern) + end + + def formatter + BasicFormat.new(file_paths) + end +end From e3b933b3504a6686fa2075dc4ade0f83ea74b5aa Mon Sep 17 00:00:00 2001 From: matsuyama Date: Fri, 12 Apr 2024 00:35:29 +0900 Subject: [PATCH 02/14] =?UTF-8?q?=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3a=E3=81=A8=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3r=E3=81=AE=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/ls.rb | 10 +++++++++- 07.ls_object/ls_command.rb | 6 ++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/07.ls_object/ls.rb b/07.ls_object/ls.rb index ae81b32dd7..594e1348eb 100644 --- a/07.ls_object/ls.rb +++ b/07.ls_object/ls.rb @@ -1,6 +1,14 @@ # frozen_string_literal: true require_relative 'ls_command' +require 'optparse' + +opt = OptionParser.new +options = {} +opt.on('-a') { |v| options[:a] = v } +opt.on('-r') { |v| options[:r] = v } +opt.on('-l') { |v| options[:l] = v } +opt.parse!(ARGV) path = ARGV[0] || '.' -LsCommand.new(path).display +LsCommand.new(path, options).display diff --git a/07.ls_object/ls_command.rb b/07.ls_object/ls_command.rb index de7f2baa6c..4be281bfd4 100644 --- a/07.ls_object/ls_command.rb +++ b/07.ls_object/ls_command.rb @@ -3,8 +3,9 @@ require_relative 'basic_format' class LsCommand - def initialize(path) + def initialize(path, options) @path = path + @options = options end def display @@ -15,7 +16,8 @@ def display def file_paths file_pattern = File.join(@path, '*') - Dir.glob(file_pattern) + file_paths = @options[:a] ? Dir.glob(file_pattern, File::FNM_DOTMATCH) : Dir.glob(file_pattern) + @options[:r] ? file_paths.reverse : file_paths end def formatter From c76d38915a4a95dd8f4728314170e3e9c3d5cde9 Mon Sep 17 00:00:00 2001 From: matsuyama Date: Fri, 12 Apr 2024 00:51:56 +0900 Subject: [PATCH 03/14] =?UTF-8?q?Etc=E3=83=A9=E3=82=A4=E3=83=96=E3=83=A9?= =?UTF-8?q?=E3=83=AA=E3=82=92=E5=B0=8E=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/file_info.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/07.ls_object/file_info.rb b/07.ls_object/file_info.rb index d7c9ea3a5a..c25a9c257b 100644 --- a/07.ls_object/file_info.rb +++ b/07.ls_object/file_info.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require 'etc' + class FileInfo def initialize(file_path) @file_path = file_path From 5b250ee4d1400bb28abfc3c05853870216865677 Mon Sep 17 00:00:00 2001 From: matsuyama Date: Fri, 12 Apr 2024 00:56:53 +0900 Subject: [PATCH 04/14] =?UTF-8?q?=E5=AE=9A=E6=95=B0=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E3=81=97=E3=81=A6=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=83=91=E3=83=BC=E3=83=9F=E3=83=83=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E3=81=8C=E9=81=A9=E5=88=87=E3=81=AB=E8=A1=A8=E7=A4=BA=E3=81=95?= =?UTF-8?q?=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/file_info.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/07.ls_object/file_info.rb b/07.ls_object/file_info.rb index c25a9c257b..eebcc319cd 100644 --- a/07.ls_object/file_info.rb +++ b/07.ls_object/file_info.rb @@ -2,6 +2,9 @@ require 'etc' +PERMISSIONS = { '0' => '---', '1' => '--x', '2' => '-w-', '3' => '-wx', + '4' => 'r--', '5' => 'r-x', '6' => 'rw-', '7' => 'rwx' }.freeze + class FileInfo def initialize(file_path) @file_path = file_path From 80b5a96fe676eae08df4437b039c6f8321b4bae0 Mon Sep 17 00:00:00 2001 From: matsuyama Date: Sat, 13 Apr 2024 03:16:57 +0900 Subject: [PATCH 05/14] =?UTF-8?q?=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3l=E3=81=AE=E6=A9=9F=E8=83=BD=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/detailed_format.rb | 27 +++++++++++++++++++++++++++ 07.ls_object/ls_command.rb | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 07.ls_object/detailed_format.rb diff --git a/07.ls_object/detailed_format.rb b/07.ls_object/detailed_format.rb new file mode 100644 index 0000000000..e7f680e94b --- /dev/null +++ b/07.ls_object/detailed_format.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class DetailedFormat < BasicFormat + def format_lines + maxes = max_lengths + @files.map do |file| + "#{file.type}#{file.mode} "\ + "#{file.link_num.to_s.rjust(maxes[:nlink])} "\ + "#{file.user.rjust(maxes[:user])} #{file.group.to_s.rjust(maxes[:group])} "\ + "#{file.size.to_s.rjust(maxes[:size])} "\ + "#{file.last_access_time} #{file.base_name}"\ + "#{" -> #{File.readlink(file.base_name)}" if file.symlink?}" + end + end + + private + + def max_lengths + max_lengths = {} + max_lengths[:size] = @files.map { |file| file.size.to_s.length }.max + max_lengths[:nlink] = @files.map { |file| file.link_num.to_s.length }.max + max_lengths[:user] = @files.map { |file| file.user.length }.max + max_lengths[:group] = @files.map { |file| file.group.length }.max + + max_lengths + end +end diff --git a/07.ls_object/ls_command.rb b/07.ls_object/ls_command.rb index 4be281bfd4..065bbea6dc 100644 --- a/07.ls_object/ls_command.rb +++ b/07.ls_object/ls_command.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative 'basic_format' +require_relative 'detailed_format' class LsCommand def initialize(path, options) @@ -21,6 +22,6 @@ def file_paths end def formatter - BasicFormat.new(file_paths) + @options[:l] ? DetailedFormat.new(file_paths) : BasicFormat.new(file_paths) end end From 841866cf8230c621349010405d9c5bac80ce60cc Mon Sep 17 00:00:00 2001 From: matsuyama Date: Sat, 13 Apr 2024 03:25:07 +0900 Subject: [PATCH 06/14] =?UTF-8?q?=E3=82=B7=E3=83=B3=E3=83=9C=E3=83=AA?= =?UTF-8?q?=E3=83=83=E3=82=AF=E3=83=AA=E3=83=B3=E3=82=AF=E3=81=AE=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E3=82=92=E4=BF=AE=E6=AD=A3=E3=81=97=E3=80=81=E3=82=AB?= =?UTF-8?q?=E3=83=AC=E3=83=B3=E3=83=88=E3=83=87=E3=82=A3=E3=83=AC=E3=82=AF?= =?UTF-8?q?=E3=83=88=E3=83=AA=E4=BB=A5=E5=A4=96=E3=81=AE=E3=83=87=E3=82=A3?= =?UTF-8?q?=E3=83=AC=E3=82=AF=E3=83=88=E3=83=AA=E3=81=A7=E3=82=82=E6=AD=A3?= =?UTF-8?q?=E3=81=97=E3=81=8F=E8=A1=A8=E7=A4=BA=E3=81=95=E3=82=8C=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/detailed_format.rb | 2 +- 07.ls_object/file_info.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/07.ls_object/detailed_format.rb b/07.ls_object/detailed_format.rb index e7f680e94b..b53c92a7cf 100644 --- a/07.ls_object/detailed_format.rb +++ b/07.ls_object/detailed_format.rb @@ -9,7 +9,7 @@ def format_lines "#{file.user.rjust(maxes[:user])} #{file.group.to_s.rjust(maxes[:group])} "\ "#{file.size.to_s.rjust(maxes[:size])} "\ "#{file.last_access_time} #{file.base_name}"\ - "#{" -> #{File.readlink(file.base_name)}" if file.symlink?}" + "#{" -> #{File.readlink(file.file_path)}" if file.symlink?}" end end diff --git a/07.ls_object/file_info.rb b/07.ls_object/file_info.rb index eebcc319cd..2fc7e60d8a 100644 --- a/07.ls_object/file_info.rb +++ b/07.ls_object/file_info.rb @@ -6,6 +6,8 @@ '4' => 'r--', '5' => 'r-x', '6' => 'rw-', '7' => 'rwx' }.freeze class FileInfo + attr_reader :file_path + def initialize(file_path) @file_path = file_path end From 8d86419716bcce8c013d54ccb57292bac9ee9045 Mon Sep 17 00:00:00 2001 From: matsuyama Date: Mon, 15 Apr 2024 01:56:42 +0900 Subject: [PATCH 07/14] =?UTF-8?q?=E3=82=AA=E3=83=97=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3L=E3=81=A7=E5=90=88=E8=A8=88=E3=83=96=E3=83=AD?= =?UTF-8?q?=E3=83=83=E3=82=AF=E6=95=B0=E3=81=8C=E8=A1=A8=E7=A4=BA=E3=81=8C?= =?UTF-8?q?=E3=81=95=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E5=A4=89=E6=9B=B4?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/detailed_format.rb | 4 ++++ 07.ls_object/file_info.rb | 4 ++++ 07.ls_object/ls_command.rb | 1 + 3 files changed, 9 insertions(+) diff --git a/07.ls_object/detailed_format.rb b/07.ls_object/detailed_format.rb index b53c92a7cf..12dcd18060 100644 --- a/07.ls_object/detailed_format.rb +++ b/07.ls_object/detailed_format.rb @@ -13,6 +13,10 @@ def format_lines end end + def calculate_total_blocks + @files.sum(&:blocks) + end + private def max_lengths diff --git a/07.ls_object/file_info.rb b/07.ls_object/file_info.rb index 2fc7e60d8a..3a1f15f8ef 100644 --- a/07.ls_object/file_info.rb +++ b/07.ls_object/file_info.rb @@ -56,6 +56,10 @@ def symlink? mode_num[0, 2] == '12' end + def blocks + file_stat.blocks + end + private def file_stat diff --git a/07.ls_object/ls_command.rb b/07.ls_object/ls_command.rb index 065bbea6dc..4f04b258bf 100644 --- a/07.ls_object/ls_command.rb +++ b/07.ls_object/ls_command.rb @@ -10,6 +10,7 @@ def initialize(path, options) end def display + puts "total #{formatter.calculate_total_blocks}" if @options[:l] puts formatter.format_lines end From 082aadf1e8477ce664ce02805644c66d67614ebb Mon Sep 17 00:00:00 2001 From: matsuyama Date: Tue, 7 May 2024 18:07:56 +0900 Subject: [PATCH 08/14] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=81=AE=E3=82=B0=E3=83=AB=E3=83=BC=E3=83=97=E5=8C=96=E3=83=AD?= =?UTF-8?q?=E3=82=B8=E3=83=83=E3=82=AF=E3=82=92=E6=94=B9=E8=89=AF=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=81=9F=E3=82=81N.times.map=20=E3=82=92=E4=BD=BF?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/basic_format.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/07.ls_object/basic_format.rb b/07.ls_object/basic_format.rb index 6e4ff53291..435b5046ec 100644 --- a/07.ls_object/basic_format.rb +++ b/07.ls_object/basic_format.rb @@ -23,13 +23,12 @@ def format_lines def files_grouped_into_lines total_line_count = (@files.size.to_f / COLUMN_NUM).ceil files_sliced = @files.each_slice(total_line_count).to_a - - lines = Array.new(total_line_count) { [] } - lines.each_with_index do |line, index| - files_sliced.each do |files| - line << files[index] if files[index] - end - end + + lines = total_line_count.times.map { |index| + files_sliced.map do |files| + files[index] + end.compact + } lines end From df666f95d9df807bf79b978e92dda56c7c668440 Mon Sep 17 00:00:00 2001 From: matsuyama Date: Tue, 7 May 2024 21:57:57 +0900 Subject: [PATCH 09/14] =?UTF-8?q?=E5=A4=89=E6=95=B0=E3=82=92=E4=BB=8B?= =?UTF-8?q?=E3=81=95=E3=81=9Amax=5Flengths=E3=82=92=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E5=8F=82=E7=85=A7=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/detailed_format.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/07.ls_object/detailed_format.rb b/07.ls_object/detailed_format.rb index 12dcd18060..39e2562b81 100644 --- a/07.ls_object/detailed_format.rb +++ b/07.ls_object/detailed_format.rb @@ -2,12 +2,11 @@ class DetailedFormat < BasicFormat def format_lines - maxes = max_lengths @files.map do |file| "#{file.type}#{file.mode} "\ - "#{file.link_num.to_s.rjust(maxes[:nlink])} "\ - "#{file.user.rjust(maxes[:user])} #{file.group.to_s.rjust(maxes[:group])} "\ - "#{file.size.to_s.rjust(maxes[:size])} "\ + "#{file.link_num.to_s.rjust(max_lengths[:nlink])} "\ + "#{file.user.rjust(max_lengths[:user])} #{file.group.to_s.rjust(max_lengths[:group])} "\ + "#{file.size.to_s.rjust(max_lengths[:size])} "\ "#{file.last_access_time} #{file.base_name}"\ "#{" -> #{File.readlink(file.file_path)}" if file.symlink?}" end From 804bebc833c94255545aa5841a682a97030ca741 Mon Sep 17 00:00:00 2001 From: matsuyama Date: Wed, 8 May 2024 03:09:15 +0900 Subject: [PATCH 10/14] =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E8=A9=B3=E7=B4=B0=E6=83=85=E5=A0=B1=E3=81=AE=E3=83=95=E3=82=A9?= =?UTF-8?q?=E3=83=BC=E3=83=9E=E3=83=83=E3=83=88=E5=87=A6=E7=90=86=E3=82=92?= =?UTF-8?q?FileInfo=E3=82=AF=E3=83=A9=E3=82=B9=E3=81=AB=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/detailed_format.rb | 7 +------ 07.ls_object/file_info.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/07.ls_object/detailed_format.rb b/07.ls_object/detailed_format.rb index 39e2562b81..05f269ffb2 100644 --- a/07.ls_object/detailed_format.rb +++ b/07.ls_object/detailed_format.rb @@ -3,12 +3,7 @@ class DetailedFormat < BasicFormat def format_lines @files.map do |file| - "#{file.type}#{file.mode} "\ - "#{file.link_num.to_s.rjust(max_lengths[:nlink])} "\ - "#{file.user.rjust(max_lengths[:user])} #{file.group.to_s.rjust(max_lengths[:group])} "\ - "#{file.size.to_s.rjust(max_lengths[:size])} "\ - "#{file.last_access_time} #{file.base_name}"\ - "#{" -> #{File.readlink(file.file_path)}" if file.symlink?}" + file.full_info_format(max_lengths) end end diff --git a/07.ls_object/file_info.rb b/07.ls_object/file_info.rb index 3a1f15f8ef..188bd6fd29 100644 --- a/07.ls_object/file_info.rb +++ b/07.ls_object/file_info.rb @@ -60,6 +60,15 @@ def blocks file_stat.blocks end + def full_info_format(max_lengths) + "#{type}#{mode} "\ + "#{link_num.to_s.rjust(max_lengths[:nlink])} "\ + "#{user.rjust(max_lengths[:user])} #{group.to_s.rjust(max_lengths[:group])} "\ + "#{size.to_s.rjust(max_lengths[:size])} "\ + "#{last_access_time} #{base_name}"\ + "#{" -> #{File.readlink(file_path)}" if symlink?}" + end + private def file_stat From a6375b66f04dd56cdd4b5c875b126a26ad1b3cb8 Mon Sep 17 00:00:00 2001 From: matsuyama Date: Thu, 9 May 2024 17:55:29 +0900 Subject: [PATCH 11/14] =?UTF-8?q?=E3=82=AF=E3=83=A9=E3=82=B9=E5=90=8D?= =?UTF-8?q?=E3=82=92BasicFormat=E3=81=8B=E3=82=89ShortFormat=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=81=97=E3=80=81BasicFormat=E3=82=92?= =?UTF-8?q?=E3=82=A4=E3=83=B3=E3=82=BF=E3=83=BC=E3=83=95=E3=82=A7=E3=83=BC?= =?UTF-8?q?=E3=82=B9=E3=81=A8=E3=81=97=E3=81=A6=E5=AE=9A=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/basic_format.rb | 28 +------------------------- 07.ls_object/detailed_format.rb | 2 ++ 07.ls_object/ls_command.rb | 4 ++-- 07.ls_object/short_format.rb | 35 +++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 29 deletions(-) create mode 100644 07.ls_object/short_format.rb diff --git a/07.ls_object/basic_format.rb b/07.ls_object/basic_format.rb index 435b5046ec..02d007f368 100644 --- a/07.ls_object/basic_format.rb +++ b/07.ls_object/basic_format.rb @@ -3,37 +3,11 @@ require_relative 'file_info' class BasicFormat - COLUMN_NUM = 3 - def initialize(file_paths) @files = file_paths.map { |file_path| FileInfo.new(file_path) } end def format_lines - files_grouped_into_lines.map do |line| - line.map do |file| - file_name = file.base_name - file_name.ljust(max_length) - end.join(' ') - end - end - - private - - def files_grouped_into_lines - total_line_count = (@files.size.to_f / COLUMN_NUM).ceil - files_sliced = @files.each_slice(total_line_count).to_a - - lines = total_line_count.times.map { |index| - files_sliced.map do |files| - files[index] - end.compact - } - - lines - end - - def max_length - @files.map { |file| file.base_name.size }.max + NotImplementedError end end diff --git a/07.ls_object/detailed_format.rb b/07.ls_object/detailed_format.rb index 05f269ffb2..b48c4dfd04 100644 --- a/07.ls_object/detailed_format.rb +++ b/07.ls_object/detailed_format.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative 'basic_format' + class DetailedFormat < BasicFormat def format_lines @files.map do |file| diff --git a/07.ls_object/ls_command.rb b/07.ls_object/ls_command.rb index 4f04b258bf..447b6f22ea 100644 --- a/07.ls_object/ls_command.rb +++ b/07.ls_object/ls_command.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require_relative 'basic_format' +require_relative 'short_format' require_relative 'detailed_format' class LsCommand @@ -23,6 +23,6 @@ def file_paths end def formatter - @options[:l] ? DetailedFormat.new(file_paths) : BasicFormat.new(file_paths) + @options[:l] ? DetailedFormat.new(file_paths) : ShortFormat.new(file_paths) end end diff --git a/07.ls_object/short_format.rb b/07.ls_object/short_format.rb new file mode 100644 index 0000000000..7c8aaa37f7 --- /dev/null +++ b/07.ls_object/short_format.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require_relative 'basic_format' + +class ShortFormat < BasicFormat + COLUMN_NUM = 3 + + def format_lines + files_grouped_into_lines.map do |line| + line.map do |file| + file_name = file.base_name + file_name.ljust(max_length) + end.join(' ') + end + end + + private + + def files_grouped_into_lines + total_line_count = (@files.size.to_f / COLUMN_NUM).ceil + files_sliced = @files.each_slice(total_line_count).to_a + + lines = total_line_count.times.map { |index| + files_sliced.map do |files| + files[index] + end.compact + } + + lines + end + + def max_length + @files.map { |file| file.base_name.size }.max + end +end From 367d4f530061bb69377b7f245a7dd769683044b0 Mon Sep 17 00:00:00 2001 From: matsuyama Date: Thu, 9 May 2024 17:59:00 +0900 Subject: [PATCH 12/14] =?UTF-8?q?full=5Finfo=5Fformat=E3=83=A1=E3=82=BD?= =?UTF-8?q?=E3=83=83=E3=83=89=E3=81=AE=E3=82=A4=E3=83=B3=E3=83=87=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/file_info.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/07.ls_object/file_info.rb b/07.ls_object/file_info.rb index 188bd6fd29..f0a95c9fe8 100644 --- a/07.ls_object/file_info.rb +++ b/07.ls_object/file_info.rb @@ -61,12 +61,12 @@ def blocks end def full_info_format(max_lengths) - "#{type}#{mode} "\ - "#{link_num.to_s.rjust(max_lengths[:nlink])} "\ - "#{user.rjust(max_lengths[:user])} #{group.to_s.rjust(max_lengths[:group])} "\ - "#{size.to_s.rjust(max_lengths[:size])} "\ - "#{last_access_time} #{base_name}"\ - "#{" -> #{File.readlink(file_path)}" if symlink?}" + "#{type}#{mode} "\ + "#{link_num.to_s.rjust(max_lengths[:nlink])} "\ + "#{user.rjust(max_lengths[:user])} #{group.to_s.rjust(max_lengths[:group])} "\ + "#{size.to_s.rjust(max_lengths[:size])} "\ + "#{last_access_time} #{base_name}"\ + "#{" -> #{File.readlink(file_path)}" if symlink?}" end private From 98584043980acbd19da6eaef3a16e67d0b014b4e Mon Sep 17 00:00:00 2001 From: matsuyama Date: Thu, 9 May 2024 18:17:19 +0900 Subject: [PATCH 13/14] =?UTF-8?q?files=5Fgrouped=5Finto=5Flines=20?= =?UTF-8?q?=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=81=AE=E4=B8=8D=E8=A6=81?= =?UTF-8?q?=E3=81=AA=E4=BB=A3=E5=85=A5=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/short_format.rb | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/07.ls_object/short_format.rb b/07.ls_object/short_format.rb index 7c8aaa37f7..d7912d2e34 100644 --- a/07.ls_object/short_format.rb +++ b/07.ls_object/short_format.rb @@ -20,13 +20,9 @@ def files_grouped_into_lines total_line_count = (@files.size.to_f / COLUMN_NUM).ceil files_sliced = @files.each_slice(total_line_count).to_a - lines = total_line_count.times.map { |index| - files_sliced.map do |files| - files[index] - end.compact - } - - lines + total_line_count.times.map do |index| + files_sliced.map { |files| files[index] }.compact + end end def max_length From e8e7b332c437232bb19b571b3d51c54ef5962b9e Mon Sep 17 00:00:00 2001 From: matsuyama Date: Thu, 9 May 2024 18:30:38 +0900 Subject: [PATCH 14/14] =?UTF-8?q?FIleInfo=E3=82=AF=E3=83=A9=E3=82=B9?= =?UTF-8?q?=E3=81=8B=E3=82=89=20attr=5Freader=20:file=5Fpath=20=E3=82=92?= =?UTF-8?q?=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07.ls_object/file_info.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/07.ls_object/file_info.rb b/07.ls_object/file_info.rb index f0a95c9fe8..d78e813228 100644 --- a/07.ls_object/file_info.rb +++ b/07.ls_object/file_info.rb @@ -6,8 +6,6 @@ '4' => 'r--', '5' => 'r-x', '6' => 'rw-', '7' => 'rwx' }.freeze class FileInfo - attr_reader :file_path - def initialize(file_path) @file_path = file_path end @@ -66,7 +64,7 @@ def full_info_format(max_lengths) "#{user.rjust(max_lengths[:user])} #{group.to_s.rjust(max_lengths[:group])} "\ "#{size.to_s.rjust(max_lengths[:size])} "\ "#{last_access_time} #{base_name}"\ - "#{" -> #{File.readlink(file_path)}" if symlink?}" + "#{" -> #{File.readlink(@file_path)}" if symlink?}" end private