-l
This commit is contained in:
parent
57a594c1b2
commit
4d5404ea3b
1 changed files with 20 additions and 51 deletions
71
ls.rb
71
ls.rb
|
@ -27,58 +27,50 @@ 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
|
||||
params = ARGV.getopts('a', 'r', 'l')
|
||||
filenames = params['a'] ? Dir.glob('*', File::FNM_DOTMATCH) : Dir.glob('*')
|
||||
filenames = filenames.reverse if params['r']
|
||||
params['r'] ? output_l(filenames) : output(filenames)
|
||||
params['l'] ? output_l(filenames) : output(filenames)
|
||||
end
|
||||
|
||||
def output(filenames)
|
||||
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|
|
||||
rows = (filenames.size / COLUMNS)
|
||||
length_limits = init_limit(filenames, rows)
|
||||
(0...rows).each do |row|
|
||||
COLUMNS.times do |column|
|
||||
index = row + column * number_of_row
|
||||
print filenames[index].ljust(max_lengths[column] + COLUMNS) if filenames[index]
|
||||
index = row + column * rows
|
||||
print filenames[index].ljust(length_limits[column] + COLUMNS) if filenames[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 }
|
||||
def init_limit(filenames, rows)
|
||||
filenames.each_slice(rows).map { _1.map(&:length).max }
|
||||
end
|
||||
|
||||
def output_l(filenames)
|
||||
fileblocks(filenames)
|
||||
fileblock(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)
|
||||
filemod(file_stat)
|
||||
print_ownerinfo(file_stat, filenames)
|
||||
timestamp(file_stat)
|
||||
print_symbolic(file)
|
||||
puts
|
||||
end
|
||||
end
|
||||
|
||||
def prints(file_stat, filenames)
|
||||
def print_ownerinfo(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(filesizes(filenames))
|
||||
print " #{file_stat.size}".rjust(filesize(filenames))
|
||||
end
|
||||
|
||||
def symbolics(filenames)
|
||||
def print_symbolic(filenames)
|
||||
if File.lstat(filenames).symlink?
|
||||
print " #{filenames} -> #{File.readlink(filenames)}"
|
||||
else
|
||||
|
@ -86,7 +78,7 @@ def symbolics(filenames)
|
|||
end
|
||||
end
|
||||
|
||||
def filemods(file_stat)
|
||||
def filemod(file_stat)
|
||||
file_count = file_stat.mode.to_s(8).slice(-3, 3)
|
||||
file_permission = file_count.split('').map do |file|
|
||||
PERMISSIONS[file]
|
||||
|
@ -94,42 +86,19 @@ def filemods(file_stat)
|
|||
print file_permission.join('')
|
||||
end
|
||||
|
||||
def filesizes(filenames)
|
||||
def filesize(filenames)
|
||||
filenames.map { |file| File.size(file) }.max.to_s.length + MARGIN
|
||||
end
|
||||
|
||||
def timestamps(file_stat)
|
||||
def timestamp(file_stat)
|
||||
print file_stat.mtime.strftime('%_m月 %_d %H:%M')
|
||||
end
|
||||
|
||||
def fileblocks(filenames)
|
||||
def fileblock(filenames)
|
||||
blocks = filenames.map do |file|
|
||||
File::Stat.new(file).blocks
|
||||
end
|
||||
puts "total #{blocks.sum}"
|
||||
end
|
||||
|
||||
def slice_filenames(listed_filenames)
|
||||
columns_size = listed_filenames.size.ceildiv(COLUMNS)
|
||||
listed_filenames.each_slice(columns_size).to_a
|
||||
end
|
||||
|
||||
def fill_filenames(filenames_matrix)
|
||||
array_size = filenames_matrix.map(&:size).max
|
||||
filenames_matrix.each do |slice|
|
||||
slice << '' while slice.size < array_size
|
||||
end
|
||||
filenames_matrix.transpose
|
||||
end
|
||||
|
||||
def arrange_filenames(filled_filenames)
|
||||
filenames_count = filled_filenames.flatten.map(&:size).max
|
||||
filled_filenames.each do |arrange|
|
||||
arrange.each do |arranged|
|
||||
print arranged.ljust(filenames_count + 5)
|
||||
end
|
||||
puts
|
||||
end
|
||||
end
|
||||
|
||||
run
|
||||
|
|
Loading…
Reference in a new issue