class FifthedSim::RollRepl

Public Class Methods

new(inspect: true, errors: false, auto_info: false) click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 3
def initialize(inspect: true,
               errors: false,
               auto_info: false)
  @inspect = inspect
  @errors = errors
  @auto_info = auto_info
end

Public Instance Methods

display_compile_error(err, cmd) click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 90
def display_compile_error(err, cmd)
  if @errors
    error_msg("Could not read line: #{err.tree_cause}")
  else
    error_msg("Could not read line: #{err.message}")
  end
end
display_roll(r) click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 80
def display_roll(r)
  puts "  =  " + r.value_equation(terminal: true) if @inspect  
  puts "  => " + Rainbow(r.value.to_s).underline.bright.to_s
  print_last_info if @auto_info
end
error_msg(msg) click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 86
def error_msg(msg)
  puts Rainbow(msg).color(:red).bright
end
exit() click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 106
def exit
  puts "Goodbye."
  exit!
end
help() click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 111
def help
  cmd = ->(a){ Rainbow(a.to_s).bright.underline + ":" }
  puts %Q{COMMANDS:
  #{cmd[:help]} display this message
  #{cmd[:inspect]} toggle equation inspection
  #{cmd[:quit]} exit the roller
  #{cmd[:errors]} toggle in-depth compile errors for dice expressions
  #{cmd["arrow keys"]} navigate like GNU readline
  #{cmd[:info]} get info about the previous roll, or a roll on this line.
  #{cmd["rr, reroll"]} reroll the previous dice
  }
end
info_command(cmd) click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 24
def info_command(cmd)
  s = cmd.gsub(/info/, "").chomp
  if s.length > 0
    roll(s)
  end
  print_last_info
end
print_last_info() click to toggle source
reroll() click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 41
def reroll
  return error_msg("Nothing to reroll") unless @last_roll
  @last_roll = @last_roll.reroll
  display_roll(@last_roll)
end
roll(cmd) click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 68
def roll(cmd)
  r = DiceExpression(cmd)
  @last_roll = r
  display_roll(r)
rescue FifthedSim::Compiler::CompileError => e
  if e.char
    display_compile_error(e, cmd)
  else
    error_msg("Could not parse expression!")
  end
end
run(kill_on_interrupt = false) click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 11
def run(kill_on_interrupt = false)
  begin
    while buf = Readline.readline("> ", true)
      run_cmd(buf)
      kill_on_interrupt = false
    end
  rescue Interrupt
    self.exit if kill_on_interrupt
    puts "Control-C again or 'Exit' to quit"
    run(true)
  end
end
run_cmd(cmd) click to toggle source
# File lib/fifthed_sim/roll_repl.rb, line 47
def run_cmd(cmd)
  case cmd
  when /(quit|exit|stop)/
    self.exit
  when "rr", "reroll"
    self.reroll
  when "info"
    info_command(cmd)
  when "help"
    self.help
  when "inspect"
    toggle_inspect
  when "errors"
    toggle_errors
  when "autoinfo"
    toggle_auto_info
  else
    self.roll(cmd)
  end
end