Class: FifthedSim::RollRepl
- Inherits:
-
Object
- Object
- FifthedSim::RollRepl
- Defined in:
- lib/fifthed_sim/roll_repl.rb
Instance Method Summary collapse
- #display_compile_error(err, cmd) ⇒ Object
- #display_roll(r) ⇒ Object
- #error_msg(msg) ⇒ Object
- #exit ⇒ Object
- #help ⇒ Object
- #info(cmd) ⇒ Object
-
#initialize(inspect = true, errors = false) ⇒ RollRepl
constructor
A new instance of RollRepl.
- #reroll ⇒ Object
- #roll(cmd) ⇒ Object
- #run(kill_on_interrupt = false) ⇒ Object
- #run_cmd(cmd) ⇒ Object
Constructor Details
#initialize(inspect = true, errors = false) ⇒ RollRepl
Returns a new instance of RollRepl.
3 4 5 6 |
# File 'lib/fifthed_sim/roll_repl.rb', line 3 def initialize(inspect = true, errors = false) @inspect = inspect @errors = errors end |
Instance Method Details
#display_compile_error(err, cmd) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/fifthed_sim/roll_repl.rb', line 83 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.}") end end |
#display_roll(r) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/fifthed_sim/roll_repl.rb', line 70 def display_roll(r) if @inspect puts " = " + r.value_equation(terminal: true) puts " => " + Rainbow(r.value.to_s).underline.bright.to_s else puts r.value.to_s end end |
#error_msg(msg) ⇒ Object
79 80 81 |
# File 'lib/fifthed_sim/roll_repl.rb', line 79 def error_msg(msg) puts Rainbow(msg).color(:red).bright end |
#exit ⇒ Object
99 100 101 102 |
# File 'lib/fifthed_sim/roll_repl.rb', line 99 def exit puts "Goodbye." exit! end |
#help ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/fifthed_sim/roll_repl.rb', line 104 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(cmd) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fifthed_sim/roll_repl.rb', line 21 def info(cmd) s = cmd.gsub(/info/, "").chomp if s.length > 0 run_command(s) end return error_msg("Have nothing to get info of") unless @last_roll lb = ->(x){Rainbow(x).color(:yellow).bright.to_s + ": "} puts (%i(max min percentile).map do |p| lb[p] + @last_roll.public_send(p).to_s end.inject{|m, x| m + ", " + x}) end |
#reroll ⇒ Object
33 34 35 36 |
# File 'lib/fifthed_sim/roll_repl.rb', line 33 def reroll return error_msg("Nothing to reroll") unless @last_roll display_roll(@last_roll.reroll) end |
#roll(cmd) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fifthed_sim/roll_repl.rb', line 58 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) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/fifthed_sim/roll_repl.rb', line 8 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) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/fifthed_sim/roll_repl.rb', line 38 def run_cmd(cmd) case cmd when /(quit|exit|stop)/ self.exit when "rr", "reroll" self.reroll when /info/ self.info(cmd) when "help" self.help when "inspect" toggle_inspect when "errors" toggle_errors else self.roll(cmd) end end |