mirror of
https://github.com/samsonjs/csc360-a1-shell.git
synced 2026-04-27 14:57:43 +00:00
Fix colour methods and use them
This commit is contained in:
parent
34150d64cb
commit
ec8baa2e1c
5 changed files with 23 additions and 22 deletions
|
|
@ -33,7 +33,7 @@ class Shell
|
||||||
# only supports one variable and doesn't support quoting
|
# only supports one variable and doesn't support quoting
|
||||||
name, *value_parts = args.first.strip.split('=')
|
name, *value_parts = args.first.strip.split('=')
|
||||||
if name.nil? || name.empty?
|
if name.nil? || name.empty?
|
||||||
logger.warn "#{RED}[ERROR]#{CLEAR} Invalid export command"
|
logger.warn "#{red('[ERROR]')} Invalid export command"
|
||||||
else
|
else
|
||||||
ENV[name] = value_parts.join('=').gsub(/\$\w+/) { |m| ENV[m[1..]] || '' }
|
ENV[name] = value_parts.join('=').gsub(/\$\w+/) { |m| ENV[m[1..]] || '' }
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,13 @@ class Shell
|
||||||
RED = "\033[1;31m".freeze
|
RED = "\033[1;31m".freeze
|
||||||
WHITE = "\033[1;37m".freeze
|
WHITE = "\033[1;37m".freeze
|
||||||
CLEAR = "\033[0;m".freeze
|
CLEAR = "\033[0;m".freeze
|
||||||
end
|
|
||||||
|
|
||||||
def self.included(other)
|
def self.included(mod)
|
||||||
Colours.constants.each do |sym|
|
Colours.constants.each do |sym|
|
||||||
next if sym == :CLEAR
|
next if sym == :CLEAR
|
||||||
other.define_method(sym.to_s.downcase) do |text|
|
mod.define_method(sym.to_s.downcase) do |*text|
|
||||||
"#{Colours.const_get(sym)}#{text}#{CLEAR}"
|
"#{Colours.const_get(sym)}#{text.join}#{CLEAR}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -19,19 +19,20 @@ class Shell
|
||||||
if pid.nil?
|
if pid.nil?
|
||||||
# no-op
|
# no-op
|
||||||
elsif (job = @jobs_by_pid[pid])
|
elsif (job = @jobs_by_pid[pid])
|
||||||
puts "\n#{YELLOW}#{job.id}#{CLEAR}: " \
|
time = Time.now.strftime('%T')
|
||||||
"#{WHITE}(pid #{pid})#{CLEAR} " \
|
job_text = yellow('job ', job.id, ' exited')
|
||||||
"#{GREEN}#{job.cmd}#{CLEAR} " \
|
args = job.args.inspect
|
||||||
"#{job.args.inspect}"
|
puts "\n[#{time}] #{job_text} #{white('(pid ', pid, ')')}: #{green(job.cmd)} #{args}"
|
||||||
else
|
else
|
||||||
warn "\n#{YELLOW}[WARN]#{CLEAR} No job found for child with PID #{pid}"
|
warn "\n#{yellow('[WARN]')} No job found for child with PID #{pid}"
|
||||||
end
|
end
|
||||||
|
Readline.refresh_line
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def exec_command(cmd, args, background: false)
|
def exec_command(cmd, args, background: false)
|
||||||
unless (path = resolve_executable(cmd))
|
unless (path = resolve_executable(cmd))
|
||||||
warn "#{RED}[ERROR]#{CLEAR} command not found: #{cmd}"
|
warn "#{red('[ERROR]')} command not found: #{cmd}"
|
||||||
return -2
|
return -2
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -48,12 +49,12 @@ class Shell
|
||||||
$CHILD_STATUS.exitstatus
|
$CHILD_STATUS.exitstatus
|
||||||
rescue Errno::ECHILD => e
|
rescue Errno::ECHILD => e
|
||||||
# FIXME: why does this happen? doesn't seem to be a real problem
|
# FIXME: why does this happen? doesn't seem to be a real problem
|
||||||
logger.verbose "#{YELLOW}#{e.message}#{CLEAR} but child was just forked 🧐"
|
logger.verbose "#{yellow(e.message)} but child was just forked 🧐"
|
||||||
0
|
0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
warn "#{RED}[ERROR]#{CLEAR} #{e.message} #{e.inspect}"
|
warn "#{red('[ERROR]')} #{e.message} #{e.inspect}"
|
||||||
-5
|
-5
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,16 +19,16 @@ class Shell
|
||||||
end
|
end
|
||||||
|
|
||||||
def log(message)
|
def log(message)
|
||||||
@logs << Log.new(:info, "#{WHITE}[INFO]#{CLEAR} #{message}")
|
@logs << Log.new(:info, "#{white('[INFO]')} #{message}")
|
||||||
end
|
end
|
||||||
alias info log
|
alias info log
|
||||||
|
|
||||||
def warn(message)
|
def warn(message)
|
||||||
@logs << Log.new(:warning, "#{YELLOW}[WARN]#{CLEAR} #{message}")
|
@logs << Log.new(:warning, "#{yellow('[WARN]')} #{message}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def error(message)
|
def error(message)
|
||||||
@logs << Log.new(:error, "#{RED}[ERROR]#{CLEAR} #{message}")
|
@logs << Log.new(:error, "#{red('[ERROR]')} #{message}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def verbose(message)
|
def verbose(message)
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ class Shell
|
||||||
status = 0
|
status = 0
|
||||||
loop do
|
loop do
|
||||||
print_logs
|
print_logs
|
||||||
print "#{RED}#{status}#{CLEAR} " unless status.zero?
|
print "#{red(status)} " unless status.zero?
|
||||||
line = Readline.readline(prompt(Dir.pwd), add_to_history)
|
line = Readline.readline(prompt(Dir.pwd), add_to_history)
|
||||||
Readline::HISTORY.pop if line.nil? || line.strip.empty?
|
Readline::HISTORY.pop if line.nil? || line.strip.empty?
|
||||||
status = process_command(line)
|
status = process_command(line)
|
||||||
|
|
@ -50,7 +50,7 @@ class Shell
|
||||||
|
|
||||||
# Looks like this: /path/to/somewhere%
|
# Looks like this: /path/to/somewhere%
|
||||||
def prompt(pwd)
|
def prompt(pwd)
|
||||||
"#{BLUE}#{pwd}#{WHITE}% #{CLEAR}"
|
"#{blue(pwd)}#{white('%')} #{CLEAR}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_options(args)
|
def parse_options(args)
|
||||||
|
|
@ -64,7 +64,7 @@ class Shell
|
||||||
when '-v', '--verbose'
|
when '-v', '--verbose'
|
||||||
options[:verbose] = true
|
options[:verbose] = true
|
||||||
else
|
else
|
||||||
logger.warn "#{RED}[ERROR]#{CLEAR} Unknown argument: #{arg}"
|
logger.warn "#{red('[ERROR]')} Unknown argument: #{arg}"
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -100,7 +100,7 @@ class Shell
|
||||||
@job_control.exec_command(cmd, args)
|
@job_control.exec_command(cmd, args)
|
||||||
end
|
end
|
||||||
rescue Errno => e
|
rescue Errno => e
|
||||||
warn "#{RED}[ERROR]#{CLEAR} #{e.message}"
|
warn "#{red('[ERROR]')} #{e.message}"
|
||||||
-1
|
-1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue