Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruby-ls solution #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions samples/part1/bin/ruby-ls
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/env ruby

## FIXME: Replace this code with a pure Ruby clone of the ls utility
system("ls", *ARGV)
require 'optparse'
require 'etc'
require_relative '../lib/ruby-ls'

RubyLS::Application.new(ARGV).run
2 changes: 2 additions & 0 deletions samples/part1/lib/ruby-ls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require_relative "ruby-ls/application"
require_relative "ruby-ls/display"
30 changes: 30 additions & 0 deletions samples/part1/lib/ruby-ls/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module RubyLS
class Application
def initialize(argv)
@params, @dirs = parse_options(argv)
@display = RubyLS::Display.new(@params)
end

def run
@display.render(@dirs)
end

def parse_options(argv)
params = {}
parser = OptionParser.new

parser.on("-l") { params[:display_format] = :long }
parser.on("-a") { params[:show_hidden] = true }

begin
dirs = parser.parse(argv)
rescue OptionParser::InvalidOption
$stderr.puts "ls: illegal option -- #{argv[0][1]}"
$stderr.puts "usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]"
exit(1)
end

[params, dirs]
end
end
end
110 changes: 110 additions & 0 deletions samples/part1/lib/ruby-ls/display.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
module RubyLS
class Display
attr_reader :display_format, :show_hidden, :glob, :files

def initialize(params)
@display_format = params[:display_format]
@show_hidden = params[:show_hidden]
@glob = false
end

def render(data)
report = ''

if data.empty?
@files = get_directory(Dir.pwd)
else
@files = []
data.each do |dir|
@files.push(*get_directory(dir))
end
end

glob = @files.none? { |d| File.directory?(d) }

if display_format
block_total = 0
@files.each do |file|
block_total += File.stat(file).blocks
report << build_long_report(file)
end
puts "total #{block_total}" unless glob
else
@files.each do |file|
report << "#{file}\n"
end
end
puts report
end

def get_directory(dir)
if !File.file?(dir) && !File.directory?(dir)
$stderr.puts "ls: #{dir}: No such file or directory"
exit(1)
end

if Dir.exists?(dir)
Dir.chdir(dir)
files = show_hidden == true ? Dir.entries('.') : Dir.glob('*')
else
files = Dir.glob(dir)
end
end

def padding(files)
files.map { |f| File.stat(f).size.to_s.size }.max
end

def build_long_report(dir)
output = ''

uid = File.stat(dir).uid
gid = File.stat(dir).gid
mode = get_mode_string(dir)
links = File.stat(dir).nlink.to_s
size = File.stat(dir).size.to_s
mtime = File.stat(dir).mtime
mtime = mtime.strftime("%b %e %H:%M")
uname = Etc.getpwuid(uid).name
gname = Etc.getgrgid(gid).name
output << mode + ' '
output << links + ' '
output << uname + ' '
output << gname + ' '
output << size.rjust(padding(files)) + ' '
output << mtime + ' '
output << dir
output << "\n"
end

def get_mode_string(file)
read_idx = [0, 3, 6]
write_idx = [1, 4, 7]
exe_idx = [2, 5, 8]
mode_string = ''

if Dir.exists?(file)
mode_string << 'd'
else
mode_string << '-'
end

mode = File.stat(file).mode
mode_bits = mode.to_s(2).split(//).last(9)
mode_bits.each_with_index do |b, i|
if b == "1"
if read_idx.include?(i)
mode_string << 'r'
elsif write_idx.include?(i)
mode_string << 'w'
elsif exe_idx.include?(i)
mode_string << 'x'
end
else
mode_string << '-'
end
end
mode_string
end
end
end