-l
This commit is contained in:
parent
9a23e19ba2
commit
5d59e49fb1
2 changed files with 26 additions and 54 deletions
50
ls.rb
50
ls.rb
|
@ -6,6 +6,27 @@ require 'etc'
|
|||
COLUMNS = 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
|
||||
listed_filenames = list_filenames
|
||||
filenames_matrix = slice_filenames(listed_filenames)
|
||||
|
@ -29,7 +50,7 @@ def ownerinfo(filenames)
|
|||
file_blocks(filenames)
|
||||
filenames.each do |file|
|
||||
file_stat = File::Stat.new(file)
|
||||
file_type(file_stat)
|
||||
print " #{TYPES[file_stat.ftype]}"
|
||||
permission(file_stat)
|
||||
print " #{file_stat.nlink}"
|
||||
print " #{Etc.getpwuid(file_stat.uid).name}"
|
||||
|
@ -50,30 +71,11 @@ def symbolic(filenames)
|
|||
end
|
||||
|
||||
def permission(file_stat)
|
||||
(-3..-1).each do |index|
|
||||
case file_stat.mode.to_s(8)[index]
|
||||
when '0' then print '---'
|
||||
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
|
||||
file_count = file_stat.mode.to_s(8).slice(-3, 3)
|
||||
file_permission = file_count.split('').map do |file|
|
||||
PERMISSIONS[file]
|
||||
end
|
||||
end
|
||||
|
||||
def file_type(file_stat)
|
||||
{
|
||||
'fifo' => 'p',
|
||||
'characterSpecial' => 'c',
|
||||
'directory' => 'd',
|
||||
'blockSpecial' => 'b',
|
||||
'file' => '-',
|
||||
'link' => 'l',
|
||||
'socket' => 's'
|
||||
}[file_stat]
|
||||
print file_permission.join('')
|
||||
end
|
||||
|
||||
def file_size(filenames)
|
||||
|
|
30
ls2.rb
30
ls2.rb
|
@ -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
|
Loading…
Reference in a new issue