summaryrefslogtreecommitdiff
path: root/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'controllers')
-rw-r--r--controllers/instance.rb18
-rw-r--r--controllers/interfaces/eval.rb8
-rw-r--r--controllers/interfaces/repl.rb25
-rw-r--r--controllers/session.rb12
4 files changed, 48 insertions, 15 deletions
diff --git a/controllers/instance.rb b/controllers/instance.rb
index fbe2348..a982261 100644
--- a/controllers/instance.rb
+++ b/controllers/instance.rb
@@ -31,10 +31,24 @@ module NanoBot
@session.state
end
- def eval(input, &block)
+ def boot(as: 'eval', &block)
@stream.callback = block if block && @stream.is_a?(Components::Stream)
- Interfaces::Eval.evaluate(input, @cartridge, @session)
+ Interfaces::REPL.boot(@cartridge, @session, as:)
+
+ return unless @stream.is_a?(Components::Stream)
+
+ @stream.finish
+ end
+
+ def prompt
+ Interfaces::REPL.prompt(@cartridge)
+ end
+
+ def eval(input, as: 'eval', &block)
+ @stream.callback = block if block && @stream.is_a?(Components::Stream)
+
+ Interfaces::Eval.evaluate(input, @cartridge, @session, as)
return unless @stream.is_a?(Components::Stream)
diff --git a/controllers/interfaces/eval.rb b/controllers/interfaces/eval.rb
index 851770b..5f472ad 100644
--- a/controllers/interfaces/eval.rb
+++ b/controllers/interfaces/eval.rb
@@ -10,13 +10,13 @@ module NanoBot
module Controllers
module Interfaces
module Eval
- def self.evaluate(input, cartridge, session)
- prefix = Logic::Cartridge::Affixes.get(cartridge, :eval, :output, :prefix)
- suffix = Logic::Cartridge::Affixes.get(cartridge, :eval, :output, :suffix)
+ def self.evaluate(input, cartridge, session, mode)
+ prefix = Logic::Cartridge::Affixes.get(cartridge, mode.to_sym, :output, :prefix)
+ suffix = Logic::Cartridge::Affixes.get(cartridge, mode.to_sym, :output, :suffix)
session.print(prefix) unless prefix.nil?
- session.evaluate_and_print(input, mode: 'eval')
+ session.evaluate_and_print(input, mode:)
session.print(suffix) unless suffix.nil?
end
diff --git a/controllers/interfaces/repl.rb b/controllers/interfaces/repl.rb
index ee6234b..fd16ea6 100644
--- a/controllers/interfaces/repl.rb
+++ b/controllers/interfaces/repl.rb
@@ -10,18 +10,26 @@ module NanoBot
module Controllers
module Interfaces
module REPL
+ def self.boot(cartridge, session, prefix = nil, suffix = nil, as: 'repl')
+ return unless Logic::Helpers::Hash.fetch(cartridge, %i[behaviors boot instruction])
+
+ prefix ||= Logic::Cartridge::Affixes.get(cartridge, as.to_sym, :output, :prefix)
+ suffix ||= Logic::Cartridge::Affixes.get(cartridge, as.to_sym, :output, :suffix)
+
+ session.print(prefix) unless prefix.nil?
+ session.boot(mode: as)
+ session.print(suffix) unless suffix.nil?
+ end
+
def self.start(cartridge, session)
prefix = Logic::Cartridge::Affixes.get(cartridge, :repl, :output, :prefix)
suffix = Logic::Cartridge::Affixes.get(cartridge, :repl, :output, :suffix)
- if Logic::Helpers::Hash.fetch(cartridge, %i[behaviors boot instruction])
- session.print(prefix) unless prefix.nil?
- session.boot(mode: 'repl')
- session.print(suffix) unless suffix.nil?
- session.print("\n")
- end
+ boot(cartridge, session, prefix, suffix)
+
+ session.print("\n") if Logic::Helpers::Hash.fetch(cartridge, %i[behaviors boot instruction])
- prompt = build_prompt(Logic::Helpers::Hash.fetch(cartridge, %i[interfaces repl prompt]))
+ prompt = self.prompt(cartridge)
Pry.config.prompt = Pry::Prompt.new(
'REPL',
@@ -40,7 +48,8 @@ module NanoBot
Pry.start
end
- def self.build_prompt(prompt)
+ def self.prompt(cartridge)
+ prompt = Logic::Helpers::Hash.fetch(cartridge, %i[interfaces repl prompt])
result = ''
if prompt.is_a?(Array)
diff --git a/controllers/session.rb b/controllers/session.rb
index 3d5d707..270b623 100644
--- a/controllers/session.rb
+++ b/controllers/session.rb
@@ -61,6 +61,8 @@ module NanoBot
@state[:history] << {
who: 'user',
+ mode: mode.to_s,
+ input: message,
message: Components::Adapter.apply(
:input, Logic::Cartridge::Interaction.input(@cartridge, mode.to_sym, message)
)
@@ -72,6 +74,9 @@ module NanoBot
end
def process(input, mode:)
+ prefix = Logic::Cartridge::Affixes.get(@cartridge, mode.to_sym, :output, :prefix)
+ suffix = Logic::Cartridge::Affixes.get(@cartridge, mode.to_sym, :output, :suffix)
+
interface = Logic::Helpers::Hash.fetch(@cartridge, [:interfaces, mode.to_sym]) || {}
streaming = Logic::Cartridge::Streaming.enabled?(@cartridge, mode.to_sym)
@@ -85,7 +90,7 @@ module NanoBot
updated_at = Time.now
if finished
- @state[:history] << Marshal.load(Marshal.dump(output))
+ event = Marshal.load(Marshal.dump(output))
output = Logic::Cartridge::Interaction.output(
@cartridge, mode.to_sym, output, streaming, finished
@@ -93,6 +98,11 @@ module NanoBot
output[:message] = Components::Adapter.apply(:output, output[:message])
+ event[:mode] = mode.to_s
+ event[:output] = "#{prefix}#{output[:message]}#{suffix}"
+
+ @state[:history] << event
+
self.print(output[:message]) unless streaming
ready = true