mirror of
https://github.com/samsonjs/csc360-a1-shell.git
synced 2026-03-25 08:45:52 +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
|
||||
name, *value_parts = args.first.strip.split('=')
|
||||
if name.nil? || name.empty?
|
||||
logger.warn "#{RED}[ERROR]#{CLEAR} Invalid export command"
|
||||
logger.warn "#{red('[ERROR]')} Invalid export command"
|
||||
else
|
||||
ENV[name] = value_parts.join('=').gsub(/\$\w+/) { |m| ENV[m[1..]] || '' }
|
||||
end
|
||||
|
|
|
|||
|
|
@ -7,13 +7,13 @@ class Shell
|
|||
RED = "\033[1;31m".freeze
|
||||
WHITE = "\033[1;37m".freeze
|
||||
CLEAR = "\033[0;m".freeze
|
||||
end
|
||||
|
||||
def self.included(other)
|
||||
Colours.constants.each do |sym|
|
||||
next if sym == :CLEAR
|
||||
other.define_method(sym.to_s.downcase) do |text|
|
||||
"#{Colours.const_get(sym)}#{text}#{CLEAR}"
|
||||
def self.included(mod)
|
||||
Colours.constants.each do |sym|
|
||||
next if sym == :CLEAR
|
||||
mod.define_method(sym.to_s.downcase) do |*text|
|
||||
"#{Colours.const_get(sym)}#{text.join}#{CLEAR}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -19,19 +19,20 @@ class Shell
|
|||
if pid.nil?
|
||||
# no-op
|
||||
elsif (job = @jobs_by_pid[pid])
|
||||
puts "\n#{YELLOW}#{job.id}#{CLEAR}: " \
|
||||
"#{WHITE}(pid #{pid})#{CLEAR} " \
|
||||
"#{GREEN}#{job.cmd}#{CLEAR} " \
|
||||
"#{job.args.inspect}"
|
||||
time = Time.now.strftime('%T')
|
||||
job_text = yellow('job ', job.id, ' exited')
|
||||
args = job.args.inspect
|
||||
puts "\n[#{time}] #{job_text} #{white('(pid ', pid, ')')}: #{green(job.cmd)} #{args}"
|
||||
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
|
||||
Readline.refresh_line
|
||||
end
|
||||
end
|
||||
|
||||
def exec_command(cmd, args, background: false)
|
||||
unless (path = resolve_executable(cmd))
|
||||
warn "#{RED}[ERROR]#{CLEAR} command not found: #{cmd}"
|
||||
warn "#{red('[ERROR]')} command not found: #{cmd}"
|
||||
return -2
|
||||
end
|
||||
|
||||
|
|
@ -48,12 +49,12 @@ class Shell
|
|||
$CHILD_STATUS.exitstatus
|
||||
rescue Errno::ECHILD => e
|
||||
# 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
|
||||
end
|
||||
end
|
||||
rescue StandardError => e
|
||||
warn "#{RED}[ERROR]#{CLEAR} #{e.message} #{e.inspect}"
|
||||
warn "#{red('[ERROR]')} #{e.message} #{e.inspect}"
|
||||
-5
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -19,16 +19,16 @@ class Shell
|
|||
end
|
||||
|
||||
def log(message)
|
||||
@logs << Log.new(:info, "#{WHITE}[INFO]#{CLEAR} #{message}")
|
||||
@logs << Log.new(:info, "#{white('[INFO]')} #{message}")
|
||||
end
|
||||
alias info log
|
||||
|
||||
def warn(message)
|
||||
@logs << Log.new(:warning, "#{YELLOW}[WARN]#{CLEAR} #{message}")
|
||||
@logs << Log.new(:warning, "#{yellow('[WARN]')} #{message}")
|
||||
end
|
||||
|
||||
def error(message)
|
||||
@logs << Log.new(:error, "#{RED}[ERROR]#{CLEAR} #{message}")
|
||||
@logs << Log.new(:error, "#{red('[ERROR]')} #{message}")
|
||||
end
|
||||
|
||||
def verbose(message)
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ class Shell
|
|||
status = 0
|
||||
loop do
|
||||
print_logs
|
||||
print "#{RED}#{status}#{CLEAR} " unless status.zero?
|
||||
print "#{red(status)} " unless status.zero?
|
||||
line = Readline.readline(prompt(Dir.pwd), add_to_history)
|
||||
Readline::HISTORY.pop if line.nil? || line.strip.empty?
|
||||
status = process_command(line)
|
||||
|
|
@ -50,7 +50,7 @@ class Shell
|
|||
|
||||
# Looks like this: /path/to/somewhere%
|
||||
def prompt(pwd)
|
||||
"#{BLUE}#{pwd}#{WHITE}% #{CLEAR}"
|
||||
"#{blue(pwd)}#{white('%')} #{CLEAR}"
|
||||
end
|
||||
|
||||
def parse_options(args)
|
||||
|
|
@ -64,7 +64,7 @@ class Shell
|
|||
when '-v', '--verbose'
|
||||
options[:verbose] = true
|
||||
else
|
||||
logger.warn "#{RED}[ERROR]#{CLEAR} Unknown argument: #{arg}"
|
||||
logger.warn "#{red('[ERROR]')} Unknown argument: #{arg}"
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
|
@ -100,7 +100,7 @@ class Shell
|
|||
@job_control.exec_command(cmd, args)
|
||||
end
|
||||
rescue Errno => e
|
||||
warn "#{RED}[ERROR]#{CLEAR} #{e.message}"
|
||||
warn "#{red('[ERROR]')} #{e.message}"
|
||||
-1
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue