-l
This commit is contained in:
parent
bb1374aca3
commit
57a594c1b2
1 changed files with 41 additions and 29 deletions
70
ls.rb
70
ls.rb
|
@ -27,46 +27,58 @@ PERMISSIONS = {
|
|||
'7' => 'rwx'
|
||||
}.freeze
|
||||
|
||||
def run
|
||||
listed_filenames = list_filenames
|
||||
filenames_matrix = slice_filenames(listed_filenames)
|
||||
filled_filenames = fill_filenames(filenames_matrix)
|
||||
arrange_filenames(filled_filenames)
|
||||
end
|
||||
# def run
|
||||
# listed_filenames = list_filenames
|
||||
# filenames_matrix = slice_filenames(listed_filenames)
|
||||
# filled_filenames = fill_filenames(filenames_matrix)
|
||||
# arrange_filenames(filled_filenames)
|
||||
# end
|
||||
|
||||
def list_filenames
|
||||
def run
|
||||
params = ARGV.getopts('a', 'r', 'l')
|
||||
filenames = params['a'] ? Dir.glob('*', File::FNM_DOTMATCH) : Dir.glob('*')
|
||||
filenames = filenames.reverse if params['r']
|
||||
if params['l']
|
||||
output(filenames)
|
||||
exit
|
||||
else
|
||||
filenames
|
||||
end
|
||||
params['r'] ? output_l(filenames) : output(filenames)
|
||||
end
|
||||
|
||||
def output(filenames)
|
||||
file_blocks(filenames)
|
||||
filenames.each do |file|
|
||||
file_stat = File::Stat.new(file)
|
||||
print TYPES[file_stat.ftype]
|
||||
permission(file_stat)
|
||||
printer(file_stat, filenames)
|
||||
timestamp(file_stat)
|
||||
symbolic(file)
|
||||
number_of_row = (filenames.size / COLUMNS.to_f).ceil
|
||||
max_lengths = make_max_lengths(filenames, number_of_row)
|
||||
|
||||
(0...number_of_row).each do |row|
|
||||
COLUMNS.times do |column|
|
||||
index = row + column * number_of_row
|
||||
print filenames[index].ljust(max_lengths[column] + COLUMNS) if filenames[index]
|
||||
end
|
||||
puts
|
||||
end
|
||||
end
|
||||
|
||||
def printer(file_stat, filenames)
|
||||
def make_max_lengths(files, number_of_row)
|
||||
files.each_slice(number_of_row).to_a.map { _1.map(&:length).max }
|
||||
end
|
||||
|
||||
def output_l(filenames)
|
||||
fileblocks(filenames)
|
||||
filenames.each do |file|
|
||||
file_stat = File::Stat.new(file)
|
||||
print TYPES[file_stat.ftype]
|
||||
filemods(file_stat)
|
||||
prints(file_stat, filenames)
|
||||
timestamps(file_stat)
|
||||
symbolics(file)
|
||||
puts
|
||||
end
|
||||
end
|
||||
|
||||
def prints(file_stat, filenames)
|
||||
print " #{file_stat.nlink}"
|
||||
print " #{Etc.getpwuid(file_stat.uid).name}"
|
||||
print " #{Etc.getgrgid(file_stat.gid).name}"
|
||||
print " #{file_stat.size}".rjust(file_size(filenames))
|
||||
print " #{file_stat.size}".rjust(filesizes(filenames))
|
||||
end
|
||||
|
||||
def symbolic(filenames)
|
||||
def symbolics(filenames)
|
||||
if File.lstat(filenames).symlink?
|
||||
print " #{filenames} -> #{File.readlink(filenames)}"
|
||||
else
|
||||
|
@ -74,7 +86,7 @@ def symbolic(filenames)
|
|||
end
|
||||
end
|
||||
|
||||
def permission(file_stat)
|
||||
def filemods(file_stat)
|
||||
file_count = file_stat.mode.to_s(8).slice(-3, 3)
|
||||
file_permission = file_count.split('').map do |file|
|
||||
PERMISSIONS[file]
|
||||
|
@ -82,15 +94,15 @@ def permission(file_stat)
|
|||
print file_permission.join('')
|
||||
end
|
||||
|
||||
def file_size(filenames)
|
||||
def filesizes(filenames)
|
||||
filenames.map { |file| File.size(file) }.max.to_s.length + MARGIN
|
||||
end
|
||||
|
||||
def timestamp(file_stat)
|
||||
print file_stat.mtime.strftime('%_m %_d %H:%M')
|
||||
def timestamps(file_stat)
|
||||
print file_stat.mtime.strftime('%_m月 %_d %H:%M')
|
||||
end
|
||||
|
||||
def file_blocks(filenames)
|
||||
def fileblocks(filenames)
|
||||
blocks = filenames.map do |file|
|
||||
File::Stat.new(file).blocks
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue