fjord/ls.rb

107 lines
2.2 KiB
Ruby
Raw Normal View History

2024-04-11 21:51:53 +09:00
# frozen_string_literal: true
2024-04-26 08:55:27 +09:00
require 'optparse'
2024-05-11 10:59:59 +09:00
require 'etc'
2024-04-26 08:55:27 +09:00
2024-04-15 22:48:09 +09:00
COLUMNS = 3
2024-05-21 09:41:20 +09:00
MARGIN = 3
2024-04-11 21:51:53 +09:00
2024-05-23 09:12:55 +09:00
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
2024-06-01 09:18:04 +09:00
def run
2024-05-07 21:56:32 +09:00
params = ARGV.getopts('a', 'r', 'l')
2024-05-18 11:56:26 +09:00
filenames = params['a'] ? Dir.glob('*', File::FNM_DOTMATCH) : Dir.glob('*')
filenames = filenames.reverse if params['r']
2024-06-09 21:58:55 +09:00
params['l'] ? output_file_datails(filenames) : output(filenames)
2024-05-07 21:56:32 +09:00
end
2024-05-24 14:18:08 +09:00
def output(filenames)
2024-06-09 21:58:55 +09:00
rows = filenames.size.ceildiv(COLUMNS)
pads = pad_created(filenames, rows)
2024-06-06 11:07:44 +09:00
(0...rows).each do |row|
2024-06-01 09:18:04 +09:00
COLUMNS.times do |column|
2024-06-06 11:07:44 +09:00
index = row + column * rows
2024-06-09 21:58:55 +09:00
print filenames[index].ljust(pads[column] + COLUMNS) if filenames[index]
2024-06-01 09:18:04 +09:00
end
puts
end
end
2024-06-09 21:58:55 +09:00
def pad_created(filenames, rows)
2024-06-06 11:07:44 +09:00
filenames.each_slice(rows).map { _1.map(&:length).max }
2024-06-01 09:18:04 +09:00
end
2024-06-09 21:58:55 +09:00
def output_file_datails(filenames)
puts total_file_blocks(filenames)
2024-05-11 10:59:59 +09:00
filenames.each do |file|
file_stat = File::Stat.new(file)
2024-05-24 14:18:08 +09:00
print TYPES[file_stat.ftype]
2024-06-09 21:58:55 +09:00
print file_mode(file_stat)
print owner_info(file_stat, filenames)
print time_stamp(file_stat)
print symbolic(file)
2024-05-11 10:59:59 +09:00
puts
end
end
2024-06-09 21:58:55 +09:00
def owner_info(file_stat, filenames)
2024-06-12 10:45:58 +09:00
[
file_stat.nlink.to_s.prepend(' '),
Etc.getpwuid(file_stat.uid).name,
Etc.getgrgid(file_stat.gid).name,
file_stat.size.to_s.rjust(max_filename_length(filenames))
].join(' ')
2024-05-24 14:18:08 +09:00
end
2024-06-09 21:58:55 +09:00
def symbolic(filenames)
2024-05-16 09:15:11 +09:00
if File.lstat(filenames).symlink?
2024-06-09 21:58:55 +09:00
" #{filenames} -> #{File.readlink(filenames)}"
2024-05-11 10:59:59 +09:00
else
2024-06-09 21:58:55 +09:00
" #{filenames}"
2024-05-11 10:59:59 +09:00
end
end
2024-06-09 21:58:55 +09:00
def file_mode(file_stat)
2024-05-23 09:12:55 +09:00
file_count = file_stat.mode.to_s(8).slice(-3, 3)
file_permission = file_count.split('').map do |file|
PERMISSIONS[file]
2024-05-05 21:40:28 +09:00
end
2024-06-09 21:58:55 +09:00
file_permission.join('')
2024-05-11 10:59:59 +09:00
end
2024-06-09 21:58:55 +09:00
def max_filename_length(filenames)
2024-05-21 09:41:20 +09:00
filenames.map { |file| File.size(file) }.max.to_s.length + MARGIN
2024-05-11 10:59:59 +09:00
end
2024-06-09 21:58:55 +09:00
def time_stamp(file_stat)
file_stat.mtime.strftime('%_m月 %_d %H:%M')
2024-05-11 10:59:59 +09:00
end
2024-06-09 21:58:55 +09:00
def total_file_blocks(filenames)
2024-05-16 09:15:11 +09:00
blocks = filenames.map do |file|
File::Stat.new(file).blocks
end
2024-06-09 21:58:55 +09:00
"total #{blocks.sum}"
2024-05-16 09:15:11 +09:00
end
2024-04-24 22:37:28 +09:00
run