From 5bf0054a13b019a7e788fa7a3bf0f344125fe151 Mon Sep 17 00:00:00 2001 From: Yatsu0720 Date: Sun, 14 Aug 2022 23:53:19 +0900 Subject: [PATCH] =?UTF-8?q?-r,-a,-l=20=E3=81=AE=E3=82=AA=E3=83=97=E3=82=B7?= =?UTF-8?q?=E3=83=A7=E3=83=B3=E3=81=AB=E5=AF=BE=E5=BF=9C=E3=82=84=E3=82=8A?= =?UTF-8?q?=E7=9B=B4=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05.ls/ls.rb | 33 +++++++++------- 05.ls/test.rb | 105 -------------------------------------------------- 2 files changed, 20 insertions(+), 118 deletions(-) delete mode 100644 05.ls/test.rb diff --git a/05.ls/ls.rb b/05.ls/ls.rb index b2fe8f0931..66448c0379 100755 --- a/05.ls/ls.rb +++ b/05.ls/ls.rb @@ -1,4 +1,3 @@ -#!/usr/bin/env ruby # frozen_string_literal: true require 'optparse' @@ -9,22 +8,20 @@ FILE_STAT_CONVERSION = { '10' => '-', '04' => 'd', '12' => 'l' }.freeze def main - l_option = false - + params = ARGV.getopts('arl') opts = OptionParser.new - opts.on('-l') { l_option = true } opts.parse!(ARGV) || '.' ls_dir = ARGV[0] || '.' - file_stats = make_file_stats(ls_dir) - if l_option + file_stats = make_file_stats(ls_dir, params) + if params['l'] print_l_option_files(file_stats) else - print_no_option_files(file_stats) + print_not_l_option_files(file_stats) end end -def print_no_option_files(file_stats) +def print_not_l_option_files(file_stats) file_names = file_stats.map { |file_stat| file_stat[:file_name] } max_word_of_characters = file_names.max_by(&:length).length max_row = file_names.size / MAX_COLUMN + 1 @@ -49,7 +46,7 @@ def print_l_option_files(file_stats) print file_stat[:user].ljust(max_lengths[:user] + 2) print file_stat[:group].ljust(max_lengths[:group] + 2) print file_stat[:file_size].to_s.rjust(max_lengths[:file_size]) - print file_stat[:time].strftime(' %b %e %R ') + print file_stat[:time].strftime(' %_m %e %R ') puts file_stat[:file_name] end end @@ -74,8 +71,8 @@ def convert_file_stat_chars(file_stat) file_type + owner_permission + group_permission + other_permission end -def make_file_stats(ls_dir) - files = fetch_files(ls_dir) +def make_file_stats(ls_dir, params) + files = fetch_files(ls_dir, params) files.map do |file| file_stat = File.stat(file) file_stats = {} @@ -91,8 +88,18 @@ def make_file_stats(ls_dir) end end -def fetch_files(ls_dir) - Dir.glob("#{ls_dir}/*").sort +def fetch_files(ls_dir, params) + files = if params['a'] + Dir.glob("#{ls_dir}/*", File::FNM_DOTMATCH).sort + else + Dir.glob("#{ls_dir}/*").sort + end + + if params['r'] + files.reverse + else + files + end end main diff --git a/05.ls/test.rb b/05.ls/test.rb deleted file mode 100644 index 66448c0379..0000000000 --- a/05.ls/test.rb +++ /dev/null @@ -1,105 +0,0 @@ -# frozen_string_literal: true - -require 'optparse' -require 'etc' - -MAX_COLUMN = 3 -PERMISSION_CONVERSION = { '0' => '---', '1' => '--x', '2' => '-w-', '3' => '-wx', '4' => 'r--', '5' => 'r-x', '6' => 'rw-', '7' => 'rwx' }.freeze -FILE_STAT_CONVERSION = { '10' => '-', '04' => 'd', '12' => 'l' }.freeze - -def main - params = ARGV.getopts('arl') - opts = OptionParser.new - opts.parse!(ARGV) || '.' - ls_dir = ARGV[0] || '.' - - file_stats = make_file_stats(ls_dir, params) - if params['l'] - print_l_option_files(file_stats) - else - print_not_l_option_files(file_stats) - end -end - -def print_not_l_option_files(file_stats) - file_names = file_stats.map { |file_stat| file_stat[:file_name] } - max_word_of_characters = file_names.max_by(&:length).length - max_row = file_names.size / MAX_COLUMN + 1 - - separated_list = file_names.each_slice(max_row).to_a - - formatted_list = separated_list.map do |file_name_list| - file_name_list.values_at(0..max_row - 1).map do |file_name| - file_name.to_s.ljust(5 + max_word_of_characters) - end - end - puts formatted_list.transpose.map(&:join) -end - -def print_l_option_files(file_stats) - max_lengths = calculate_max_length(file_stats) - total_block_size = file_stats.sum { |block_size| block_size[:blocks_size] } - puts "total #{total_block_size}" - file_stats.each do |file_stat| - print file_stat[:permission] - print "#{file_stat[:hard_link].to_s.rjust(max_lengths[:hard_link] + 1)} " - print file_stat[:user].ljust(max_lengths[:user] + 2) - print file_stat[:group].ljust(max_lengths[:group] + 2) - print file_stat[:file_size].to_s.rjust(max_lengths[:file_size]) - print file_stat[:time].strftime(' %_m %e %R ') - puts file_stat[:file_name] - end -end - -def calculate_max_length(files_stats) - max_lengths = {} - files_stats.each do |hash| - max_lengths[:hard_link] = [max_lengths[:hard_link].to_i, hash[:hard_link].to_s.size].max - max_lengths[:user] = [max_lengths[:user].to_i, hash[:user].size].max - max_lengths[:group] = [max_lengths[:group].to_i, hash[:group].size].max - max_lengths[:file_size] = [max_lengths[:file_size].to_i, hash[:file_size].to_s.size].max - end - max_lengths -end - -def convert_file_stat_chars(file_stat) - file_type = FILE_STAT_CONVERSION[file_stat.mode.to_s(8).rjust(6, '0')[0, 2]] - converted_file_stat = file_stat.mode.to_s(8).rjust(6, '0') - owner_permission = PERMISSION_CONVERSION[converted_file_stat[3]] - group_permission = PERMISSION_CONVERSION[converted_file_stat[4]] - other_permission = PERMISSION_CONVERSION[converted_file_stat[5]] - file_type + owner_permission + group_permission + other_permission -end - -def make_file_stats(ls_dir, params) - files = fetch_files(ls_dir, params) - files.map do |file| - file_stat = File.stat(file) - file_stats = {} - file_stats[:blocks_size] = file_stat.blocks - file_stats[:permission] = convert_file_stat_chars(file_stat) - file_stats[:hard_link] = file_stat.nlink - file_stats[:user] = Etc.getpwuid(file_stat.uid).name - file_stats[:group] = Etc.getgrgid(file_stat.gid).name - file_stats[:file_size] = file_stat.size - file_stats[:time] = file_stat.mtime - file_stats[:file_name] = File.basename(file) - file_stats - end -end - -def fetch_files(ls_dir, params) - files = if params['a'] - Dir.glob("#{ls_dir}/*", File::FNM_DOTMATCH).sort - else - Dir.glob("#{ls_dir}/*").sort - end - - if params['r'] - files.reverse - else - files - end -end - -main