MatteoRagni/cas-rb

View on GitHub
bin/cas-console

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby

%w|pry colorize|.each do |g|
  begin
    puts "Please install #{g} gem"
    exit 1
  end unless require g
end
require_relative '../lib/Mr.CAS.rb'
require_relative 'graph2ascii.rb'

$0 = "Mr.CAS"

# Pry configuration
Pry.config.should_load_rc = false
Pry.config.prompt = [
  proc { |obj, nest_level, _| " > ".yellow },
  proc { |obj, nest_level, _| " + ".yellow }
]

include CAS
$x, $y, $z = CAS::vars :x, :y, :z

Pry.hooks.add_hook(:before_session, "print_banner") do |output, _, pry|
  BANNER = <<-BANNER

    #{"Mr.CAS = A very simple Computer Algebra System".green}

    Version: #{CAS::VERSION.join(".")} - Author: Matteo Ragni © 2016
    Licensed under MIT license  [https://github.com/MatteoRagni/cas-rb]

    Welcome to CAS cli. This is a Pry based console born to simplify
    debugging and testing, thus it contains all the feature of Pry
    like:

     * #{ "`_`".yellow } returns last evaluated command
     * #{ "`hist`".yellow } prints the command history
     * #{ "`show-doc CAS::Op#diff`".yellow } shows the documentation for a method
     * #{ "`stat CAS::Op#diff`".yellow } shows some statistics about methods

    There are three variables already defined for simplicity

     * #{ "`$x`, `$y`, `$z`".yellow } three `CAS::Variable`

    THIS IS A DEVELOPER PREVIEW CONSOLE ONLY

  BANNER
  output.puts BANNER

  pry.config.print = proc { |out, value|
    if value.class.include? CAS
      if value.respond_to? :puts_ascii
        # out.puts ("\n" + (value.puts_ascii(3)).green + "\n\n")
        out.puts "#{value}".green # TODO: solve this problem with sum
      else
        out.puts "#{value}".green
      end
    else
      out.puts "#{value.inspect}".cyan
    end
  }
  #pry.config.exception_handler = proc { |out, exception, _|
  #  if UserError === exception and SyntaxError === exception
  #    out.puts "SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}".orange
  #  else
  #    out.puts "#{exception.class}: #{exception.message}".red
  #    out.puts "from #{exception.backtrace.first}".red
  #  end
  #}
  pry.config.prompt_name = "CAS"
  pry.config.collision_warning = true

end

# CAS Exposed Methods
def abs(op); CAS::abs(op); end
def sqrt(op); CAS::sqrt(op); end
def sin(op); CAS::sin(op); end
def cos(op); CAS::cos(op); end
def tan(op); CAS::tan(op); end
def asin(op); CAS::asin(op); end
def acos(op); CAS::acos(op); end
def atan(op); CAS::atan(op); end
def exp(op); CAS::exp(op); end
def log(op); CAS::log(op); end
alias :arctan :atan
alias :arccos :acos
alias :arcsin :asin
alias :ln :log

# Starting Console
binding.pry

puts "Thank you for using Mr.CAS\nBye".cyan
exit(0)