This commit is contained in:
Rikuoh Tsujitani 2024-05-23 09:12:55 +09:00
parent 9a23e19ba2
commit 5d59e49fb1
Signed by: riq0h
GPG key ID: 010F09DEA298C717
2 changed files with 26 additions and 54 deletions

50
ls.rb
View file

@ -6,6 +6,27 @@ require 'etc'
COLUMNS = 3 COLUMNS = 3
MARGIN = 3 MARGIN = 3
TYPES = {
'fifo' => 'p',
'characterSpecial' => 'c',
'directory' => 'd',
'blockSpecial' => 'b',
'file' => '-',
'link' => 'l',
'socket' => 's'
}.freeze
PERMISSIONS = {
'0' => '---',
'1' => '--x',
'2' => '-w-',
'3' => '-wx',
'4' => 'r--',
'5' => 'r-x',
'6' => 'rw-',
'7' => 'rwx'
}.freeze
def run def run
listed_filenames = list_filenames listed_filenames = list_filenames
filenames_matrix = slice_filenames(listed_filenames) filenames_matrix = slice_filenames(listed_filenames)
@ -29,7 +50,7 @@ def ownerinfo(filenames)
file_blocks(filenames) file_blocks(filenames)
filenames.each do |file| filenames.each do |file|
file_stat = File::Stat.new(file) file_stat = File::Stat.new(file)
file_type(file_stat) print " #{TYPES[file_stat.ftype]}"
permission(file_stat) permission(file_stat)
print " #{file_stat.nlink}" print " #{file_stat.nlink}"
print " #{Etc.getpwuid(file_stat.uid).name}" print " #{Etc.getpwuid(file_stat.uid).name}"
@ -50,30 +71,11 @@ def symbolic(filenames)
end end
def permission(file_stat) def permission(file_stat)
(-3..-1).each do |index| file_count = file_stat.mode.to_s(8).slice(-3, 3)
case file_stat.mode.to_s(8)[index] file_permission = file_count.split('').map do |file|
when '0' then print '---' PERMISSIONS[file]
when '1' then print '--x'
when '2' then print '-w-'
when '3' then print '-wx'
when '4' then print 'r--'
when '5' then print 'r-x'
when '6' then print 'rw-'
when '7' then print 'rwx'
end
end end
end print file_permission.join('')
def file_type(file_stat)
{
'fifo' => 'p',
'characterSpecial' => 'c',
'directory' => 'd',
'blockSpecial' => 'b',
'file' => '-',
'link' => 'l',
'socket' => 's'
}[file_stat]
end end
def file_size(filenames) def file_size(filenames)

30
ls2.rb
View file

@ -1,30 +0,0 @@
require 'optparse'
require 'etc'
NUMBER_OF_COLUMNS = 3
COLUMN_MARGIN = 3
SIZE_INDENT = 2
def main
files = Dir.glob('*').sort
output_ls(files)
end
def output_ls(files)
number_of_row = (files.size / NUMBER_OF_COLUMNS.to_f)
max_lengths = make_max_lengths(files, number_of_row)
(0...number_of_row).each do |row|
NUMBER_OF_COLUMNS.times do |column|
index = row + column * number_of_row
print files[index].ljust(max_lengths[column] + COLUMN_MARGIN) if files[index]
end
puts
end
end
def make_max_lengths(files, number_of_row)
files.each_slice(number_of_row).to_a.map { _1.map(&:length).max }
end
main