Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 07.ls_object/command_line_options.rb
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
20 changes: 20 additions & 0 deletions 07.ls_object/file_list_printer.rb
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
67 changes: 67 additions & 0 deletions 07.ls_object/file_metadata.rb
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ファイル名はクラス名と揃えましょう。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正しました。

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
52 changes: 52 additions & 0 deletions 07.ls_object/long_format.rb
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
15 changes: 15 additions & 0 deletions 07.ls_object/ls.rb
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
52 changes: 52 additions & 0 deletions 07.ls_object/multi_column_format.rb
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
1 change: 1 addition & 0 deletions 07.ls_object/test/abc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大きさ、量、字間、行間等を確認するために入れています。この文章はダミーです。文字の大き
1 change: 1 addition & 0 deletions 07.ls_object/test/file_a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ダミーです
2 changes: 2 additions & 0 deletions 07.ls_object/test/file_b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
あなたの名前は何ですか
私の名前は
Empty file added 07.ls_object/test/file_c.txt
Empty file.
Empty file added 07.ls_object/test/file_d.txt
Empty file.
Empty file added 07.ls_object/test/file_e.txt
Empty file.
Empty file added 07.ls_object/test/file_f.txt
Empty file.
Loading